src/fursvp.domain/Authorization/WriteAuthorization/WriteAuthorizeEvent.cs
/// <summary>
/// Performs the authorization check for a state change and throws an exception if the check fails.
/// </summary>
/// <param name="oldState">The initial state of the Event.</param>
/// <param name="newState">The new state of the Event.</param>
Performs the authorization check for a state change and throws an exception if the check fails.
Performs the authorization check for a state change and throws an exception if the check fails .
public void WriteAuthorize(Event oldState, Event newState)
        {
            var actingMember = (oldState ?? newState)?.Members?.FirstOrDefault(m => m.EmailAddress == UserAccessor.User?.EmailAddress);
            if (newState == null)
            {
                Assert.That(actingMember?.IsAuthor == true, "An event can be deleted only by its author.");
            }
            if (oldState == null)
            {
                foreach (var memberState in newState.Members)
                {
                    WriteAuthorizeMember.WriteAuthorize(null, null, memberState, newState, actingMember);
                }
            }
            if (oldState != null && newState != null)
            {
                foreach (var memberState in oldState.Members.FullJoin(newState.Members, m => m.Id, m => m.Id, (old, @new) => new { old, @new }))
                {
                    WriteAuthorizeMember.WriteAuthorize(memberState.old, oldState, memberState.@new, newState, actingMember);
                }
                if (actingMember?.IsAuthor == true || actingMember?.IsOrganizer == true)
                {
                    return;
                }
                Assert.That(oldState.StartsAtUtc == newState.StartsAtUtc, nameof(oldState.StartsAtUtc) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.EndsAtUtc == newState.EndsAtUtc, nameof(oldState.EndsAtUtc) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.TimeZoneId == newState.TimeZoneId, nameof(oldState.StartsAtUtc) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.Name == newState.Name, nameof(oldState.Name) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.OtherDetails == newState.OtherDetails, nameof(oldState.OtherDetails) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.Location == newState.Location, nameof(oldState.Location) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.RsvpOpen == newState.RsvpOpen, nameof(oldState.RsvpOpen) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.RsvpClosesAtUtc == newState.RsvpClosesAtUtc, nameof(oldState.RsvpClosesAtUtc) + " can only be altered by an event's Author or Organizer.");
                Assert.That(oldState.IsPublished == newState.IsPublished, nameof(oldState.IsPublished) + " can only be altered by an event's Author or Organizer.");
                foreach (var formPrompt in oldState.Form.FullJoin(newState.Form, f => f.Id, f => f.Id, (old, @new) => new { old, @new }))
                {
                    Assert.That(formPrompt.old != null && formPrompt.@new != null, "Form can only be altered by an event's Author or Organizer.");
                    Assert.That(formPrompt.old.Prompt == formPrompt.@new.Prompt, "Form can only be altered by an event's Author or Organizer.");
                    Assert.That(formPrompt.old.Required == formPrompt.@new.Required, "Form can only be altered by an event's Author or Organizer.");
                    Assert.That(formPrompt.old.Behavior == formPrompt.@new.Behavior, "Form can only be altered by an event's Author or Organizer.");
                    Assert.That(formPrompt.old.SortOrder == formPrompt.@new.SortOrder, "Form can only be altered by an event's Author or Organizer.");
                    var oldOptions = formPrompt.old?.Options ?? Enumerable.Empty<string>();
                    var newOptions = formPrompt.@new?.Options ?? Enumerable.Empty<string>();
                    foreach (var option in oldOptions.FullJoin(newOptions, s => s, s => s, (old, @new) => new { old, @new }))
                    {
                        Assert.That(option.old != null && option.@new != null, "Form can only be altered by an event's Author or Organizer.");
                    }
                }
            }
        }
public void WriteAuthorize ( Event oldState , Event newState ) { var actingMember = ( oldState ?? newState ) ? . Members ? . FirstOrDefault ( m => m . EmailAddress == UserAccessor . User ? . EmailAddress ) ; if ( newState == null ) { Assert . That ( actingMember ? . IsAuthor == true , " " ) ; } if ( oldState == null ) { foreach ( var memberState in newState . Members ) { WriteAuthorizeMember . WriteAuthorize ( null , null , memberState , newState , actingMember ) ; } } if ( oldState != null && newState != null ) { foreach ( var memberState in oldState . Members . FullJoin ( newState . Members , m => m . Id , m => m . Id , ( old , @new ) => new { old , @new } ) ) { WriteAuthorizeMember . WriteAuthorize ( memberState . old , oldState , memberState . @new , newState , actingMember ) ; } if ( actingMember ? . IsAuthor == true || actingMember ? . IsOrganizer == true ) { return ; } Assert . That ( oldState . StartsAtUtc == newState . StartsAtUtc , nameof ( oldState . StartsAtUtc ) + " " ) ; Assert . That ( oldState . EndsAtUtc == newState . EndsAtUtc , nameof ( oldState . EndsAtUtc ) + " " ) ; Assert . That ( oldState . TimeZoneId == newState . TimeZoneId , nameof ( oldState . StartsAtUtc ) + " " ) ; Assert . That ( oldState . Name == newState . Name , nameof ( oldState . Name ) + " " ) ; Assert . That ( oldState . OtherDetails == newState . OtherDetails , nameof ( oldState . OtherDetails ) + " " ) ; Assert . That ( oldState . Location == newState . Location , nameof ( oldState . Location ) + " " ) ; Assert . That ( oldState . RsvpOpen == newState . RsvpOpen , nameof ( oldState . RsvpOpen ) + " " ) ; Assert . That ( oldState . RsvpClosesAtUtc == newState . RsvpClosesAtUtc , nameof ( oldState . RsvpClosesAtUtc ) + " " ) ; Assert . That ( oldState . IsPublished == newState . IsPublished , nameof ( oldState . IsPublished ) + " " ) ; foreach ( var formPrompt in oldState . Form . FullJoin ( newState . Form , f => f . Id , f => f . Id , ( old , @new ) => new { old , @new } ) ) { Assert . That ( formPrompt . old != null && formPrompt . @new != null , " " ) ; Assert . That ( formPrompt . old . Prompt == formPrompt . @new . Prompt , " " ) ; Assert . That ( formPrompt . old . Required == formPrompt . @new . Required , " " ) ; Assert . That ( formPrompt . old . Behavior == formPrompt . @new . Behavior , " " ) ; Assert . That ( formPrompt . old . SortOrder == formPrompt . @new . SortOrder , " " ) ; var oldOptions = formPrompt . old ? . Options ?? Enumerable . Empty < string > ( ) ; var newOptions = formPrompt . @new ? . Options ?? Enumerable . Empty < string > ( ) ; foreach ( var option in oldOptions . FullJoin ( newOptions , s => s , s => s , ( old , @new ) => new { old , @new } ) ) { Assert . That ( option . old != null && option . @new != null , " " ) ; } } } }
Performs the authorization check for a state change and throws an exception if the check fails.
Performs the authorization check for a state change and throws an exception if the check fails .
// Deletion // New event // Assert that the old form and new form are equivalent.
oldState
Event
The initial state of the Event.
The initial state of the Event .
newState
Event
The new state of the Event.
The new state of the Event .