/// <summary> /// Removes a rule /// </summary> /// <param name="content"></param> /// <param name="ruleType"></param> /// <param name="ruleValue"></param> public Attempt <OperationResult> RemoveRule(IContent content, string ruleType, string ruleValue) { var evtMsgs = EventMessagesFactory.Get(); PublicAccessEntry entry; using (var scope = ScopeProvider.CreateScope()) { entry = _publicAccessRepository.GetMany().FirstOrDefault(x => x.ProtectedNodeId == content.Id); if (entry == null) { return(Attempt <OperationResult> .Fail()); // causes rollback // causes rollback } var existingRule = entry.Rules.FirstOrDefault(x => x.RuleType == ruleType && x.RuleValue == ruleValue); if (existingRule == null) { return(Attempt <OperationResult> .Fail()); // causes rollback // causes rollback } entry.RemoveRule(existingRule); var savingNotifiation = new PublicAccessEntrySavingNotification(entry, evtMsgs); if (scope.Notifications.PublishCancelable(savingNotifiation)) { scope.Complete(); return(OperationResult.Attempt.Cancel(evtMsgs)); } _publicAccessRepository.Save(entry); scope.Complete(); scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation)); } return(OperationResult.Attempt.Succeed(evtMsgs)); }
/// <summary> /// Saves the entry /// </summary> /// <param name="entry"></param> public Attempt <OperationResult> Save(PublicAccessEntry entry) { var evtMsgs = EventMessagesFactory.Get(); using (var scope = ScopeProvider.CreateScope()) { var savingNotifiation = new PublicAccessEntrySavingNotification(entry, evtMsgs); if (scope.Notifications.PublishCancelable(savingNotifiation)) { scope.Complete(); return(OperationResult.Attempt.Cancel(evtMsgs)); } _publicAccessRepository.Save(entry); scope.Complete(); scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation)); } return(OperationResult.Attempt.Succeed(evtMsgs)); }
/// <summary> /// Adds a rule /// </summary> /// <param name="content"></param> /// <param name="ruleType"></param> /// <param name="ruleValue"></param> /// <returns></returns> public Attempt <OperationResult <OperationResultType, PublicAccessEntry> > AddRule(IContent content, string ruleType, string ruleValue) { var evtMsgs = EventMessagesFactory.Get(); PublicAccessEntry entry; using (var scope = ScopeProvider.CreateScope()) { entry = _publicAccessRepository.GetMany().FirstOrDefault(x => x.ProtectedNodeId == content.Id); if (entry == null) { return(OperationResult.Attempt.Cannot <PublicAccessEntry>(evtMsgs)); // causes rollback } var existingRule = entry.Rules.FirstOrDefault(x => x.RuleType == ruleType && x.RuleValue == ruleValue); if (existingRule == null) { entry.AddRule(ruleValue, ruleType); } else { //If they are both the same already then there's nothing to update, exit return(OperationResult.Attempt.Succeed(evtMsgs, entry)); } var savingNotifiation = new PublicAccessEntrySavingNotification(entry, evtMsgs); if (scope.Notifications.PublishCancelable(savingNotifiation)) { scope.Complete(); return(OperationResult.Attempt.Cancel(evtMsgs, entry)); } _publicAccessRepository.Save(entry); scope.Complete(); scope.Notifications.Publish(new PublicAccessEntrySavedNotification(entry, evtMsgs).WithStateFrom(savingNotifiation)); } return(OperationResult.Attempt.Succeed(evtMsgs, entry)); }