Example #1
0
        public IActionResult EditRoles(EditRolesModel model)
        {
            string roleNmae = model.Rolename;

            //验证权限
            if (!authorizerHelper.IsUserPower(UserContext.CurrentUser.UserId))
            {
                return(Json(new StatusMessageData(StatusMessageType.Error, "选中的用户中有人的权限比您高!")));
            }


            Roles role = rolesService.GetFullRoles(model.Rolename);

            role.ConnectToUser    = model.ConnectToUser;
            role.Description      = model.Description;
            role.FriendlyRolename = model.FriendlyRolename;
            role.IsBuiltin        = model.IsBuiltin;
            role.IsEnabled        = model.IsEnabled;
            role.IsPublic         = model.IsPublic;
            role.Applicationid    = model.Applicationid;
            role.RoleImage        = model.RoleImage;

            bool result = rolesService.EditRoles(role);

            if (result)
            {
                return(Json(new StatusMessageData(StatusMessageType.Success, "操作成功!")));
            }
            return(Json(new StatusMessageData(StatusMessageType.Error, "操作失败!")));
        }
        public async Task <ActionResult <IdentityResult> > EditUserRoles([FromForm] EditRolesModel model)
        {
            var result = await _rolesService.EditUserRolesAsync(model);

            if (!result.Succeeded)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Example #3
0
        public IActionResult _EditRoles(string roleName)
        {
            Roles          role  = rolesService.GetFullRoles(roleName);
            EditRolesModel model = new EditRolesModel()
            {
                ConnectToUser    = role.ConnectToUser,
                Description      = role.Description,
                FriendlyRolename = role.FriendlyRolename,
                IsBuiltin        = role.IsBuiltin,
                IsEnabled        = role.IsEnabled,
                IsPublic         = role.IsPublic,
                Rolename         = role.Rolename,
                Applicationid    = role.Applicationid,
                RoleImage        = role.RoleImage
            };

            return(View(model));
        }
Example #4
0
        public async Task <IActionResult> EditRoles(EditRolesModel roleadd)
        {
            try
            {
                CovenantUser user = await _context.GetUserByUsername(roleadd.UserName);

                IEnumerable <string> userRoles = (await _context.GetUserRoles(user.Id)).Select(UR =>
                {
                    Task <IdentityRole> t = _context.GetRole(UR.RoleId);
                    t.Wait();
                    return(t.Result.Name);
                });

                IEnumerable <string> userRolesRemain = userRoles.Where(UR => roleadd.Rolenames.Contains(UR));
                foreach (string rolename in roleadd.Rolenames)
                {
                    // Selected role that has not been added, must add
                    if (!userRolesRemain.Contains(rolename))
                    {
                        IdentityRole role = await _context.GetRoleByName(rolename);

                        await _context.CreateUserRole(_userManager, user.Id, role.Id);
                    }
                }

                IEnumerable <string> userRolesNotRemain = userRoles.Where(UR => !roleadd.Rolenames.Contains(UR));
                foreach (string rolename in userRolesNotRemain)
                {
                    // Did not select role that is already added, must remove
                    IdentityRole role = await _context.GetRoleByName(rolename);

                    await _context.DeleteUserRole(_userManager, user.Id, role.Id);
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
        /// <inheritdoc/>
        public async Task <EditUserRolesResultModel> EditUserRolesAsync(EditRolesModel model)
        {
            User user = await _userManager.FindByIdAsync(model.UserId);

            var userRoles = await _userManager.GetRolesAsync(user);

            var addResult = await _userManager.AddToRolesAsync(user, model.Roles.Except(userRoles));

            var removeResult = await _userManager.RemoveFromRolesAsync(user, userRoles.Except(model.Roles));

            var result = new EditUserRolesResultModel();

            result.Succeeded = false;
            result.Errors.AddRange(addResult.Errors);
            result.Errors.AddRange(removeResult.Errors);

            if (addResult.Succeeded && removeResult.Succeeded)
            {
                result.Succeeded = true;
            }

            return(result);
        }