public async Task <ActionResult> RemoveModerator(int id) { var cmd = new RemoveModeratorByRecordIDCommand(id, true).SetUserContext(User); var response = await cmd.Execute(); if (response.Success) { return(RedirectToAction("SubverseModerators")); } else { ModelState.AddModelError("", response.Message); if (response.Response.SubverseModerator != null) { var model = new SubverseModerator() { ID = response.Response.SubverseModerator.ID, Subverse = response.Response.SubverseModerator.Subverse, UserName = response.Response.SubverseModerator.UserName, Power = response.Response.SubverseModerator.Power }; SetNavigationViewModel(model.Subverse); return(View("~/Views/Subverses/Admin/RemoveModerator.cshtml", model)); } else { //bail return(RedirectToAction("SubverseModerators")); } } }
public async Task <ActionResult> AddModerator([Bind("ID,Subverse,UserName,Power")] SubverseModerator subverseAdmin) { if (!ModelState.IsValid) { return(View(subverseAdmin)); } // check if caller can add mods, if not, deny posting if (!ModeratorPermission.HasPermission(User, subverseAdmin.Subverse, Domain.Models.ModeratorAction.InviteMods)) { return(RedirectToAction("Index", "Home")); } subverseAdmin.UserName = subverseAdmin.UserName.TrimSafe(); Subverse subverseModel = null; //lots of premature retuns so wrap the common code var sendFailureResult = new Func <string, ActionResult>(errorMessage => { ViewBag.SubverseModel = subverseModel; ViewBag.SubverseName = subverseAdmin.Subverse; ViewBag.SelectedSubverse = string.Empty; ModelState.AddModelError(string.Empty, errorMessage); SetNavigationViewModel(subverseAdmin.Subverse); return(View("~/Views/Subverses/Admin/AddModerator.cshtml", new SubverseModeratorViewModel { UserName = subverseAdmin.UserName, Power = subverseAdmin.Power } )); }); // prevent invites to the current moderator if (User.Identity.Name.IsEqual(subverseAdmin.UserName)) { return(sendFailureResult("Can not add yourself as a moderator")); } string originalRecipientUserName = UserHelper.OriginalUsername(subverseAdmin.UserName); // prevent invites to the current moderator if (String.IsNullOrEmpty(originalRecipientUserName)) { return(sendFailureResult("User can not be found")); } // get model for selected subverse subverseModel = DataCache.Subverse.Retrieve(subverseAdmin.Subverse); if (subverseModel == null) { return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound))); } if ((subverseAdmin.Power < 1 || subverseAdmin.Power > 4) && subverseAdmin.Power != 99) { return(sendFailureResult("Only powers levels 1 - 4 and 99 are supported currently")); } //check current mod level and invite level and ensure they are a lower level var currentModLevel = ModeratorPermission.Level(User, subverseModel.Name); if (subverseAdmin.Power <= (int)currentModLevel && currentModLevel != Domain.Models.ModeratorLevel.Owner) { return(sendFailureResult("Sorry, but you can only add moderators that are a lower level than yourself")); } int maximumOwnedSubs = VoatSettings.Instance.MaximumOwnedSubs; // check if the user being added is not already a moderator of 10 subverses var currentlyModerating = _db.SubverseModerator.Where(a => a.UserName == originalRecipientUserName).ToList(); SubverseModeratorViewModel tmpModel; if (currentlyModerating.Count <= maximumOwnedSubs) { // check that user is not already moderating given subverse var isAlreadyModerator = _db.SubverseModerator.FirstOrDefault(a => a.UserName == originalRecipientUserName && a.Subverse == subverseAdmin.Subverse); if (isAlreadyModerator == null) { // check if this user is already invited var userModeratorInvitations = _db.ModeratorInvitation.Where(i => i.Recipient.ToLower() == originalRecipientUserName.ToLower() && i.Subverse.ToLower() == subverseModel.Name.ToLower()); if (userModeratorInvitations.Any()) { return(sendFailureResult("Sorry, the user is already invited to moderate this subverse")); } // send a new moderator invitation ModeratorInvitation modInv = new ModeratorInvitation { CreatedBy = User.Identity.Name, CreationDate = Repository.CurrentDate, Recipient = originalRecipientUserName, Subverse = subverseAdmin.Subverse, Power = subverseAdmin.Power }; _db.ModeratorInvitation.Add(modInv); _db.SaveChanges(); int invitationId = modInv.ID; var invitationBody = new StringBuilder(); //v/{subverse}/about/moderatorinvitations/accept/{invitationId} string acceptInviteUrl = VoatUrlFormatter.BuildUrlPath(this.HttpContext, new PathOptions(true, true), $"/v/{subverseModel.Name}/about/moderatorinvitations/accept/{invitationId}"); invitationBody.Append("Hello,"); invitationBody.Append(Environment.NewLine); invitationBody.Append($"@{User.Identity.Name} invited you to moderate v/" + subverseAdmin.Subverse + "."); invitationBody.Append(Environment.NewLine); invitationBody.Append(Environment.NewLine); invitationBody.Append($"Please visit the following link if you want to accept this invitation: {acceptInviteUrl}"); invitationBody.Append(Environment.NewLine); invitationBody.Append(Environment.NewLine); invitationBody.Append("Thank you."); var cmd = new SendMessageCommand(new Domain.Models.SendMessage() { Sender = $"v/{subverseAdmin.Subverse}", Recipient = originalRecipientUserName, Subject = $"v/{subverseAdmin.Subverse} moderator invitation", Message = invitationBody.ToString() }, true).SetUserContext(User); await cmd.Execute(); return(RedirectToAction("SubverseModerators")); } else { return(sendFailureResult("Sorry, the user is already moderating this subverse")); } } else { return(sendFailureResult("Sorry, the user is already moderating a maximum of " + maximumOwnedSubs + " subverses")); } }
public async Task <ActionResult> AcceptModeratorInvitation(int invitationId) { int maximumOwnedSubs = VoatSettings.Instance.MaximumOwnedSubs; //TODO: These errors are not friendly - please update to redirect or something // check if there is an invitation for this user with this id var userInvitation = _db.ModeratorInvitation.Find(invitationId); if (userInvitation == null) { return(ErrorView(new ErrorViewModel() { Title = "Moderator Invite Not Found", Description = "The moderator invite is no longer valid", Footer = "Where did it go?" })); //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // check if logged in user is actually the invited user if (!User.Identity.Name.IsEqual(userInvitation.Recipient)) { return(ErrorView(ErrorViewModel.GetErrorViewModel(HttpStatusCode.Unauthorized))); //return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); } // check if user is over modding limits var amountOfSubsUserModerates = _db.SubverseModerator.Where(s => s.UserName.ToLower() == User.Identity.Name.ToLower()); if (amountOfSubsUserModerates.Any()) { if (amountOfSubsUserModerates.Count() >= maximumOwnedSubs) { return(ErrorView(new ErrorViewModel() { Title = "Maximum Moderation Level Exceeded", Description = $"Sorry, you can not own or moderate more than {maximumOwnedSubs} subverses.", Footer = "That's too bad" })); } } // check if subverse exists var subverse = _db.Subverse.FirstOrDefault(s => s.Name.ToLower() == userInvitation.Subverse.ToLower()); if (subverse == null) { return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.SubverseNotFound))); //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // check if user is already a moderator of this sub var userModerating = _db.SubverseModerator.Where(s => s.Subverse.ToLower() == userInvitation.Subverse.ToLower() && s.UserName.ToLower() == User.Identity.Name.ToLower()); if (userModerating.Any()) { _db.ModeratorInvitation.Remove(userInvitation); _db.SaveChanges(); return(ErrorView(new ErrorViewModel() { Title = "You = Moderator * 2?", Description = "You are currently already a moderator of this subverse", Footer = "How much power do you want?" })); //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // add user as moderator as specified in invitation var subAdm = new SubverseModerator { Subverse = subverse.Name, UserName = UserHelper.OriginalUsername(userInvitation.Recipient), Power = userInvitation.Power, CreatedBy = UserHelper.OriginalUsername(userInvitation.CreatedBy), CreationDate = Repository.CurrentDate }; _db.SubverseModerator.Add(subAdm); // notify sender that user has accepted the invitation var message = new Domain.Models.SendMessage() { Sender = $"v/{subverse.Name}", Subject = $"Moderator invitation for v/{subverse.Name} accepted", Recipient = userInvitation.CreatedBy, Message = $"User {User.Identity.Name} has accepted your invitation to moderate subverse v/{subverse.Name}." }; var cmd = new SendMessageCommand(message).SetUserContext(User); await cmd.Execute(); //clear mod cache CacheHandler.Instance.Remove(CachingKey.SubverseModerators(subverse.Name)); // delete the invitation from database _db.ModeratorInvitation.Remove(userInvitation); _db.SaveChanges(); return(RedirectToAction("Update", "SubverseModeration", new { subverse = subverse.Name })); //return Update(subverse.Name); }
public RemoveModeratorCommand(SubverseModerator model) { _model = model; }
public AddModeratorCommand(SubverseModerator model) { _model = model; }
public Dictionary <string, SubverseModerator> InitializeSubverseModerators(string subName) { Dictionary <string, SubverseModerator> _subMods = new Dictionary <string, SubverseModerator>(); using (var context = new VoatDataContext()) { var sub = context.Subverse.Add(new Subverse() { Name = subName, Title = "v/modPerms", Description = "Test Mod Perms", SideBar = "Test Mod Perms", //Type = "link", IsAnonymized = false, CreationDate = DateTime.UtcNow.AddDays(-7), }); context.SaveChanges(); var modName = ""; SubverseModerator mod = null; modName = "system"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 1, UserName = modName, Subverse = subName, CreatedBy = "PuttItOut", CreationDate = DateTime.UtcNow.AddDays(100) //we want to ensure no-one can remove system at L1 }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "Creator"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 1, UserName = modName, Subverse = subName, CreatedBy = null, CreationDate = null }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L1-0"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 1, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow.AddDays(-100) }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L1-1"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 1, UserName = modName, Subverse = subName, CreatedBy = "L1-0", CreationDate = DateTime.UtcNow }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L2-0"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 2, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow.AddDays(-10) }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L2-1"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 2, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L3-0"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 3, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow.AddDays(-10) }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L3-1"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 3, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L4-0"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 4, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow.AddDays(-10) }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L4-1"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 4, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L99-0"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 99, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow.AddDays(-10) }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); modName = "L99-1"; mod = context.SubverseModerator.Add(new SubverseModerator() { Power = 99, UserName = modName, Subverse = subName, CreatedBy = "Creator", CreationDate = DateTime.UtcNow }).Entity; context.SaveChanges(); _subMods.Add(modName, mod); } return(_subMods); }