@@ -19,9 +19,9 @@ public class UserController : Controller
19
19
20
20
private readonly IUnitOfWork _unitOfWork ;
21
21
private readonly UserManager < IdentityUser > _userManager ;
22
- private readonly RoleManager < IdentityUser > _roleManager ;
22
+ private readonly RoleManager < IdentityRole > _roleManager ;
23
23
24
- public UserController ( IUnitOfWork unitOfWork , UserManager < IdentityUser > userManager , RoleManager < IdentityUser > roleManager )
24
+ public UserController ( IUnitOfWork unitOfWork , UserManager < IdentityUser > userManager , RoleManager < IdentityRole > roleManager )
25
25
{
26
26
_userManager = userManager ;
27
27
_unitOfWork = unitOfWork ;
@@ -38,9 +38,10 @@ public IActionResult RoleManagement(string userId)
38
38
RoleManagementVM roleManagementVM = new ( )
39
39
{
40
40
ApplicationUser = _unitOfWork . ApplicationUserRepository . Get ( u => u . Id == userId , includeProperties : "Company" ) ,
41
- RoleList = _roleManager . Roles . Select ( i => new SelectListItem {
41
+ RoleList = _roleManager . Roles . Select ( i => new SelectListItem
42
+ {
42
43
Text = i . Name ,
43
- Value = i . Name . ToString ( ) ,
44
+ Value = i . Name
44
45
} ) ,
45
46
CompanyList = _unitOfWork . CompanyRepository . GetAll ( ) . Select ( i => new SelectListItem
46
47
{
@@ -61,11 +62,11 @@ public IActionResult RoleManagement(RoleManagementVM roleManagementVM)
61
62
62
63
string oldRole = _userManager . GetRolesAsync ( _unitOfWork . ApplicationUserRepository . Get ( u => u . Id == roleManagementVM . ApplicationUser . Id ) ) . GetAwaiter ( ) . GetResult ( ) . FirstOrDefault ( ) ;
63
64
64
-
65
+ ApplicationUser applicationUser = _unitOfWork . ApplicationUserRepository . Get ( u => u . Id == roleManagementVM . ApplicationUser . Id ) ;
65
66
if ( roleManagementVM . ApplicationUser . Role != oldRole )
66
67
{
67
68
//Role is updated
68
- ApplicationUser applicationUser = _unitOfWork . ApplicationUserRepository . Get ( u => u . Id == roleManagementVM . ApplicationUser . Id ) ;
69
+
69
70
if ( roleManagementVM . ApplicationUser . Role == SD . Role_Company )
70
71
{
71
72
applicationUser . CompanyId = roleManagementVM . ApplicationUser . CompanyId ;
@@ -79,6 +80,14 @@ public IActionResult RoleManagement(RoleManagementVM roleManagementVM)
79
80
_userManager . RemoveFromRoleAsync ( applicationUser , oldRole ) . GetAwaiter ( ) . GetResult ( ) ;
80
81
_userManager . AddToRoleAsync ( applicationUser , roleManagementVM . ApplicationUser . Role ) . GetAwaiter ( ) . GetResult ( ) ;
81
82
}
83
+ else
84
+ {
85
+ if ( oldRole == SD . Role_Company || roleManagementVM . ApplicationUser . CompanyId != applicationUser . CompanyId ) {
86
+ applicationUser . CompanyId = roleManagementVM . ApplicationUser . CompanyId ;
87
+ _unitOfWork . ApplicationUserRepository . Update ( applicationUser ) ;
88
+ _unitOfWork . Save ( ) ;
89
+ }
90
+ }
82
91
83
92
TempData [ "Success" ] = "Role Updated." ;
84
93
@@ -93,15 +102,11 @@ public IActionResult RoleManagement(RoleManagementVM roleManagementVM)
93
102
[ HttpGet ]
94
103
public IActionResult GetAll ( )
95
104
{
96
- List < ApplicationUser > userList = _db . ApplicationUsers . Include ( "Company" ) . ToList ( ) ;
97
-
98
- var Roles = _db . Roles . ToList ( ) ;
99
- var userRole = _db . UserRoles . ToList ( ) ;
105
+ List < ApplicationUser > userList = _unitOfWork . ApplicationUserRepository . GetAll ( includeProperties : "Company" ) . ToList ( ) ;
100
106
101
107
foreach ( var user in userList )
102
108
{
103
- var roleId = userRole . FirstOrDefault ( u => u . UserId == user . Id ) . RoleId ;
104
- user . Role = Roles . FirstOrDefault ( u => u . Id == roleId ) . Name ;
109
+ user . Role = _userManager . GetRolesAsync ( user ) . GetAwaiter ( ) . GetResult ( ) . FirstOrDefault ( ) ;
105
110
106
111
if ( user . Company == null )
107
112
{
@@ -117,7 +122,7 @@ public IActionResult GetAll()
117
122
[ HttpPost ]
118
123
public IActionResult LockUnlock ( [ FromBody ] string id )
119
124
{
120
- var userObj = _db . ApplicationUsers . FirstOrDefault ( u=> u . Id == id ) ;
125
+ var userObj = _unitOfWork . ApplicationUserRepository . Get ( u=> u . Id == id ) ;
121
126
if ( userObj == null )
122
127
{
123
128
return Json ( new { success = false , message = "Error while locking/unlocking" } ) ;
@@ -131,7 +136,8 @@ public IActionResult LockUnlock([FromBody] string id)
131
136
userObj . LockoutEnd = DateTime . Now . AddYears ( 1000 ) ;
132
137
}
133
138
134
- _db . SaveChanges ( ) ;
139
+ _unitOfWork . ApplicationUserRepository . Update ( userObj ) ;
140
+ _unitOfWork . Save ( ) ;
135
141
return Json ( new { success = true , message = "Opration successfully" } ) ;
136
142
}
137
143
0 commit comments