コード例 #1
0
        public async Task <IActionResult> Promote(PromoteUserInputModel input)
        {
            if (!ModelState.IsValid)
            {
                return(View(input));
            }

            if (!await userService.PromoteUserAsync(input))
            {
                return(Redirect(GlobalConstants.redirectError));
            }

            return(Redirect(GlobalConstants.redirectHome));
        }
コード例 #2
0
        public async Task <bool> PromoteUserAsync(PromoteUserInputModel input)
        {
            if (!await dbContext.Users.AnyAsync(user => user.UserName == input.Name))
            {
                return(false);
            }

            var userFromDb = await dbContext.Users.SingleOrDefaultAsync(user => user.UserName == input.Name);

            if (await userManager.IsInRoleAsync(userFromDb, GlobalConstants.adminRoleName))
            {
                return(false);
            }

            await userManager.RemoveFromRoleAsync(userFromDb, GlobalConstants.userRoleName);

            await userManager.AddToRoleAsync(userFromDb, GlobalConstants.adminRoleName);

            return(true);
        }
コード例 #3
0
        public async Task <IActionResult> Promote(PromoteUserInputModel promoteUserInput)
        {
            var getUser = this.userManager.Users.Where(x => x.UserName == promoteUserInput.UserName).Select(x => x).FirstOrDefault();
            var result1 = await this.userManager.RemoveFromRoleAsync(getUser, GlobalConstants.UserRoleName);

            if (result1.Succeeded)
            {
                var result2 = await this.userManager.AddToRoleAsync(getUser, GlobalConstants.ModeratorRoleName);

                if (result2.Succeeded)
                {
                    return(this.Redirect("/Administration/Dashboard/Index"));
                }
            }

            foreach (var error in result1.Errors)
            {
                this.ModelState.AddModelError(string.Empty, error.Description);
            }

            return(this.Redirect("/Administration/Dashboard/Index"));
        }
コード例 #4
0
        public IActionResult Promote(PromoteUserInputModel model)
        {
            var user = userService.ChangeUserRole(model.Username, model.Role);

            return(View());
        }