示例#1
0
        public async Task <IActionResult> Edit(string userId)
        {
            var currentUserRoles = await _usersController.GetUserRolesAsync(User);

            if (!currentUserRoles.Contains("Moderator") && !currentUserRoles.Contains("Admin"))
            {
                return(NotFound());
            }

            var user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                var userRoles = await _userManager.GetRolesAsync(user);

                var allRoles = _roleManager.Roles.ToList();

                var moderatedByUserSections = new List <Section>();

                foreach (var sm in _dbContext.SectionModerators)
                {
                    if (sm.Moderator.Equals(user))
                    {
                        moderatedByUserSections.Add(sm.Section);
                    }
                }

                var model = new ChangeRolesViewModel(user.Id, userRoles, allRoles, moderatedByUserSections, GetAvailableSections(currentUserRoles));

                return(View(model));
            }

            return(NotFound());
        }
示例#2
0
        public async Task <ActionResult> EditRoles(string userId)
        {
            log.Info(nameof(AdminController.EditRoles));

            try
            {
                var user = await userManager.FindByIdAsync(userId);

                if (user != null)
                {
                    var userRoles = await userManager.GetRolesAsync(user);

                    var allRoles = roleManager.Roles.ToList();
                    ChangeRolesViewModel model = new ChangeRolesViewModel
                    {
                        UserId    = user.Id,
                        UserEmail = user.Email,
                        UserRoles = userRoles,
                        AllRoles  = allRoles
                    };

                    return(View(model));
                }

                log.Warn(nameof(AdminController.EditRoles) + " user is null");

                return(NotFound());
            }
            catch (Exception e)
            {
                log.Error(e);

                return(BadRequest());
            }
        }
示例#3
0
        private bool IsValid(ChangeRolesViewModel model, out IList <string> errorMessages)
        {
            bool isValid = true;
            var  errors  = new List <string>();

            if (_userManager.FindByIdAsync(model.UserId).Result == null)
            {
                errors.Add("User not found.");
                isValid = false;
            }

            if (model.UserRoles.Count == 0)
            {
                errors.Add("Choose at least one role.");
                isValid = false;
            }

            if (model.UserRoles.Contains("Moderator") && model.ModeratedByUserSections.Count == 0)
            {
                errors.Add("Choose at least one moderatable section.");
                isValid = false;
            }

            errorMessages = errors;

            return(isValid);
        }
        public async Task <IActionResult> Edit(string userId)
        {
            try
            {
                var userRoles = await this.roleService.GetAllRolesForUser(userId);

                var model = new ChangeRolesViewModel
                {
                    UserId    = userRoles.UserId,
                    UserEmail = userRoles.UserEmail,
                    UserRoles = userRoles.UserRoles,
                    AllRoles  = userRoles.AllRoles
                };

                return(View(model));
            }
            catch (NullReferenceException)
            {
                return(NotFound());;
            }
        }
示例#5
0
        public async Task <IActionResult> Edit(string userId)
        {
            IdentityUser user = await _userManager.FindByIdAsync(userId);

            if (user != null)
            {
                var userRoles = await _userManager.GetRolesAsync(user);

                var allRoles = _roleManager.Roles.Where(x => x.Name != "superAdmin").ToList();
                ChangeRolesViewModel model = new ChangeRolesViewModel
                {
                    UserId    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(View(model));
            }

            return(NotFound());
        }
示例#6
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();
                ChangeRolesViewModel model = new ChangeRolesViewModel
                {
                    UserId    = user.Id,
                    UserEmail = user.Email,
                    UserRoles = userRoles,
                    AllRoles  = allRoles
                };
                return(View(model));
            }

            return(NotFound());
        }
示例#7
0
        public async Task <IActionResult> Edit(ChangeRolesViewModel model)
        {
            var user = await _userManager.FindByIdAsync(model.UserId);

            IList <string> errorMessages = new List <string>();

            if (!IsValid(model, out errorMessages))
            {
                var viewModel = new ErrorsViewModel
                {
                    Title     = "Roles validation error",
                    Text      = "Validation errors occurred while editing user roles",
                    Errors    = errorMessages,
                    GoBackUrl = Url.Action("Edit", "Roles", new { userId = model.UserId })
                };

                return(View("Errors", viewModel));
            }

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

            var userRoles = await _userManager.GetRolesAsync(user);

            bool userWasModerator = userRoles.Contains("Moderator");

            if (userWasModerator && model.UserRoles.Contains("Moderator")) // only sections were changed
            {
                var removedReferences = _dbContext.SectionModerators
                                        .Where(sm =>
                                               !model.ModeratedByUserSections.Contains(sm.SectionId) &&
                                               model.UserId.Equals(sm.ModeratorId)).ToList();
                _dbContext.SectionModerators.RemoveRange(removedReferences);

                var userModeratableSections = _dbContext.SectionModerators.Where(sm => sm.ModeratorId.Equals(model.UserId)).Select(sm => sm.SectionId).ToList();

                foreach (var sectionId in model.ModeratedByUserSections)
                {
                    if (!userModeratableSections.Contains(sectionId))
                    {
                        _dbContext.SectionModerators.Add(new SectionModerator {
                            ModeratorId = model.UserId, SectionId = sectionId
                        });
                    }
                }
            }
            else if (!userWasModerator && model.UserRoles.Contains("Moderator")) // user assigned as moderator
            {
                foreach (var section in _dbContext.Sections)
                {
                    if (model.ModeratedByUserSections.Contains(section.Id))
                    {
                        var sectionModerator = new SectionModerator
                        {
                            ModeratorId = user.Id,
                            SectionId   = section.Id,
                        };

                        _dbContext.SectionModerators.Add(sectionModerator);
                    }
                }
            }
            else if (userWasModerator && !model.UserRoles.Contains("Moderator")) // user removed from moderators
            {
                var moderatableByUserSections = _dbContext.SectionModerators.Where(sm => sm.ModeratorId.Equals(user.Id));
                _dbContext.SectionModerators.RemoveRange(moderatableByUserSections);
            }

            _dbContext.SaveChanges();

            var addedRoles   = model.UserRoles.Except(userRoles);
            var removedRoles = userRoles.Except(model.UserRoles);

            await _userManager.AddToRolesAsync(user, addedRoles);

            await _userManager.RemoveFromRolesAsync(user, removedRoles);

            return(View("Success"));
        }