public UserRoleEditViewModel Update(UserRoleEditViewModel UserRole)
        {
            UserRole _UserRole = UserRoleRepo.Update(UserRole.ToModel());

            unitOfWork.commit();
            return(_UserRole.ToEditableViewModel());
        }
        public async Task <IActionResult> Delete(UserRoleEditViewModel viewmodel)
        {
            var user = await _userdbcontext.Users.FirstOrDefaultAsync(u => u.Id == viewmodel.UserId);

            var result = await _userManager.RemoveFromRoleAsync(user, viewmodel.Role).ConfigureAwait(false);

            if (result.Succeeded)
            {
                return(RedirectToAction("Details", "Users", new { id = viewmodel.UserId }));
            }

            return(View(viewmodel));
        }
        public async Task <IActionResult> Delete(Guid userid, Guid roleid)
        {
            var userrole = await _userdbcontext.UserRoles.Include(o => o.Role).FirstOrDefaultAsync(u => u.UserId == userid && u.RoleId == roleid);

            var viewmodel = new UserRoleEditViewModel
            {
                UserId = userrole.UserId,
                RoleId = userrole.RoleId,
                Role   = userrole.Role.Name,
            };

            return(View(viewmodel));
        }
Пример #4
0
        public ActionResult GetRolesEditList(int userId)
        {
            var rolesResult = _roleProvider.GetRolesForUser(userId);

            if (rolesResult.IsSuccess())
            {
                var roles      = rolesResult.GetSuccessResult();
                var rolesModel = new UserRoleEditViewModel
                {
                    Roles  = roles,
                    UserId = userId
                };
                return(PartialView("UserRoleEditList", rolesModel));
            }
            return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
        }
Пример #5
0
        public async Task <ActionResult> ManageRole(string id, UserRoleEditViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ApplicationUser user = await _userManager.FindByIdAsync(id);

                    if (user != null)
                    {
                        string userInformation = string.Format("Username: {0}, Surname: {1}, Firstname: {2}, Middlename: {3}, Employeenumber: {4}  ", user.UserName, user.Surname, user.FirstName, user.MiddleName, user.EmployeeNumber);

                        //changes to roles are not tracked therefore every role has to be checked for updates
                        foreach (var role in viewModel.UserRoles)
                        {
                            if (await _userManager.IsInRoleAsync(user, role.RoleName) & !role.HasRole)
                            {
                                IdentityResult roleResult = await _userManager.RemoveFromRoleAsync(user, role.RoleName);

                                if (!roleResult.Succeeded)
                                {
                                    _logger.LogError(LoggingEvents.UserConfiguration, LoggingErrorText.removeRoleFailed, role.RoleName, userInformation, _userManager.GetUserName(User), GetDataErrors.GetErrors(roleResult));
                                }
                            }
                            else if (role.HasRole & !(await _userManager.IsInRoleAsync(user, role.RoleName)))
                            {
                                IdentityResult roleResult = await _userManager.AddToRoleAsync(user, role.RoleName);

                                if (!roleResult.Succeeded)
                                {
                                    _logger.LogError(LoggingEvents.UserConfiguration, LoggingErrorText.addRoleFailed, role.RoleName, userInformation, _userManager.GetUserName(User), GetDataErrors.GetErrors(roleResult));
                                }
                            }
                        }
                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(View(viewModel));
            }
            return(View(viewModel));
        }
Пример #6
0
        public async Task <IActionResult> EditRoles(int id)
        {
            User user = await userManager.FindByIdAsync(id.ToString());

            if (user == null)
            {
                return(BadRequest());
            }

            UserRoleEditViewModel model = new UserRoleEditViewModel
            {
                IsUserLocked = await userManager.IsLockedOutAsync(user),
                User         = this.userService.Roles(id),
                Roles        = this.userService.AllRoles()
            };

            return(View(model));
        }
Пример #7
0
        public async Task <ActionResult> ManageRole(string id)
        {
            UserRoleEditViewModel viewModel = new UserRoleEditViewModel();

            if (!String.IsNullOrEmpty(id))
            {
                ApplicationUser user = await _userManager.FindByIdAsync(id);

                if (user != null)
                {
                    viewModel.Id   = user.Id;
                    viewModel.Name = user.Surname + ", " + user.UserName + " " + user.MiddleName;

                    //add all rolenames default to false
                    viewModel.UserRoles = _roleManager.Roles.Select(r => new UserRolesViewModel
                    {
                        Id       = r.Id,
                        RoleName = r.Name,
                        HasRole  = false
                    }).ToList();

                    //get all user's roles
                    IList <string> userRolesList = new List <string>();
                    userRolesList = await _userManager.GetRolesAsync(user);

                    //populate HasRole boolean field in the view model if the user has the role because we want to display all roles
                    //marking the ones which the user currently has
                    foreach (var userrole in viewModel.UserRoles)
                    {
                        if (userRolesList.Contains(userrole.RoleName))
                        {
                            userrole.HasRole = true;
                        }
                    }
                    return(View("ManageRole", viewModel));
                }
                else
                {
                    _logger.LogError(LoggingEvents.UserConfiguration, LoggingErrorText.retrieveUserFailed, id, _userManager.GetUserName(User));
                }
            }
            return(RedirectToAction("Index"));
        }
Пример #8
0
 public void Put(UserRoleEditViewModel model)
 {
     UserRoleService.Update(model);
 }