コード例 #1
0
        public async Task <ActionResult> RevokeRole(GrantRoleVM revokeRoleModel)
        {
            var uInfo = await userService.FindUserByIdAsync(revokeRoleModel.UserId);

            if (uInfo == null)
            {
                ModelState.AddModelError("UserId", "The provided user does not exist.");
                return(PartialView("_AjaxValidation", "Could not remove the user from the role."));
            }

            if (!userService.IsInRole(revokeRoleModel.UserId, revokeRoleModel.RoleName))
            {
                ModelState.AddModelError("RoleName", "The user is not a member of that role.");
                return(PartialView("_AjaxValidation", "Could not remove the user from the role."));
            }

            var result = await userService.RemoveFromRoleAsync(revokeRoleModel.UserId, revokeRoleModel.RoleName);

            if (!result.Succeded)
            {
                foreach (var err in result.Errors)
                {
                    ModelState.AddModelError($"RevokeRoleAction", err);
                }

                return(PartialView("_AjaxValidation", "Could not remove the user from the role."));
            }

            return(Json(new { location = Url.Action("Dashboard") }));
        }
コード例 #2
0
        public async Task <ActionResult> GrantRole(GrantRoleVM grantRoleModel)
        {
            var uInfo = await userService.FindUserByIdAsync(grantRoleModel.UserId);

            if (uInfo == null)
            {
                ModelState.AddModelError("UserId", "The provided user does not exist.");
                return(PartialView("_AjaxValidation", "Could not add the user to the role."));
            }

            if (userService.IsInRole(grantRoleModel.UserId, grantRoleModel.RoleName))
            {
                ModelState.AddModelError("UserAlreadyInRole", "The user is already a member of that role.");
                return(PartialView("_AjaxValidation", "Could not add the user to the role."));
            }

            var result = await userService.AddToRoleAsync(grantRoleModel.UserId, grantRoleModel.RoleName);

            if (!result.Succeded)
            {
                foreach (var err in result.Errors)
                {
                    ModelState.AddModelError("GrantRoleAction", err);
                }
                return(PartialView("_AjaxValidation", "Could not add the user to the role."));
            }

            return(Json(new { location = Url.Action("Dashboard") }));
        }