public void Add(SectionModerator entry) { if (entry == null) { throw new ArgumentNullException(); } ctx.Set <SectionModerator>().Add(entry); ctx.SaveChanges(); }
public ActionResult GrantModeratorRights(ToModerator toModerator) { var entry = new SectionModerator { UserId = toModerator.UserId, SectionId = toModerator.SectionId, DateGranted = DateTime.Now }; User user = _repository.GetUserById(toModerator.UserId); user.RoleId = roleRepository.GetAllRoles().Single(x => x.Name == "Moderator").RoleId; _repository.UpdateUser(user); sectionModeratorsRepository.Add(entry); return(RedirectToAction("Index")); }
public void Delete(SectionModerator entry) { if (entry == null) { throw new ArgumentNullException(); } var entryToDelete = GetByPrimaryKey(entry.SectionId, entry.UserId); if (entryToDelete == null) { throw new ArgumentException ($"There is no entry for section with id = {entry.SectionId} and moderator id = {entry.UserId}"); } ctx.Set <SectionModerator>().Remove(entryToDelete); ctx.SaveChanges(); }
public void Update(SectionModerator entry) { if (entry == null) { throw new ArgumentNullException(); } var entryToUpdate = GetByPrimaryKey(entry.SectionId, entry.UserId); if (entryToUpdate == null) { throw new ArgumentException ($"There is no entry for section with id = {entry.SectionId} and moderator id = {entry.UserId}"); } entryToUpdate.DateGranted = entry.DateGranted; ctx.Entry(entryToUpdate).State = EntityState.Modified; ctx.SaveChanges(); }
public void UpdateUser(User user, bool updateNavigationProperties = false) { if (user == null) { throw new ArgumentNullException(); } var userToUpdate = GetUserById(user.UserId); if (userToUpdate == null) { throw new ArgumentException ($"User with Id = {user.UserId} does not exists"); } userToUpdate.Image = user.Image ?? userToUpdate.Image; userToUpdate.Reputation = user.Reputation ?? userToUpdate.Reputation; userToUpdate.Username = user.Username ?? userToUpdate.Username; userToUpdate.E_mail = user.E_mail ?? userToUpdate.E_mail; if (updateNavigationProperties) { userToUpdate.RoleId = user.RoleId; foreach (Topic topic in user.Topics) { Topic topicOrm = topicRepository.GetById(topic.TopicId); if (topicOrm == null) { topicRepository.Add(topic); } else { topicRepository.Update(topic); } } foreach (Message message in user.Messages) { Message messageOrm = messageRepository.GetById(message.MessageId); if (messageOrm == null) { messageRepository.Add(message); } else { messageRepository.Update(message); } } foreach (SectionModerator moderationInfoEntry in user.SectionModerators) { SectionModerator moderationInfoEntryCurrent = sectionModeratorsRepository .GetByPrimaryKey(moderationInfoEntry.SectionId, moderationInfoEntry.UserId); if (moderationInfoEntryCurrent == null) { sectionModeratorsRepository.Add(moderationInfoEntry); } else { sectionModeratorsRepository.Update(moderationInfoEntry); } } } ctx.Entry(userToUpdate).State = EntityState.Modified; ctx.SaveChanges(); }
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")); }