示例#1
0
        public async Task <ActionResult> UpdateRole(string Id, [FromBody] RoleChangeViewModel model)
        {
            if (Id == null || model.Role == null)
            {
                ModelState.AddModelError("", "UserId or roleName is null");
                Log.Warning("UserId or roleName is null");
                return(BadRequest(ModelState));
            }
            var result = await userService.UpdateUserRoleAsync(Id, model.Role);

            if (result.Succeeded)
            {
                Log.Information($"Role for UserId: {Id} was been changed onto {model.Role}");
                return(Ok());
            }
            else
            {
                ModelState.AddModelError(result.Property, result.Message);
                Log.Error("Updating a role was been failed");
                return(BadRequest(ModelState));
            }
        }
示例#2
0
        public async Task <IActionResult> Edit(string userId)
        {
            // получаем пользователя
            User user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                // получем список ролей пользователя
                var userRoles = await _userManager.GetRolesAsync(user);

                var allRoles = _roleManager.Roles.ToList();
                RoleChangeViewModel model = new RoleChangeViewModel
                {
                    UserId    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(View(model));
            }

            return(NotFound());
        }