-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f003da
commit 19b6022
Showing
4 changed files
with
80 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 73 additions & 57 deletions
130
Sharecode.Backend.Domain/Helper/SnippetAccessPermission.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,93 @@ | ||
using Sharecode.Backend.Domain.Dto.Snippet; | ||
using Sharecode.Backend.Domain.Entity.Snippet; | ||
using Sharecode.Backend.Domain.Enums; | ||
|
||
namespace Sharecode.Backend.Domain.Helper; | ||
|
||
public class SnippetAccessPermission(Guid snippetId, Guid accessorId, bool read, bool write, bool manage, bool snippetPublic) | ||
namespace Sharecode.Backend.Domain.Helper | ||
{ | ||
public class SnippetAccessPermission | ||
{ | ||
public Guid SnippetId { get; set; } | ||
public Guid AccessorId { get; set; } | ||
private bool Read { get; set; } | ||
private bool Write { get; set; } | ||
private bool Manage { get; set; } | ||
public bool IsPublicSnippet { get; set;} | ||
public bool IsPrivateSnippet => !IsPublicSnippet; | ||
|
||
public static SnippetAccessPermission NoPermission(Guid snippetId, Guid accessorId) => | ||
new SnippetAccessPermission(snippetId, accessorId, false, false, false, true); | ||
|
||
public static SnippetAccessPermission Error => | ||
new SnippetAccessPermission(Guid.Empty, Guid.Empty, false, false, false, true); | ||
|
||
public Guid SnippetId { get; } = snippetId; | ||
public Guid AccessorId { get; } = accessorId; | ||
private bool Read { get; } = read || write || manage; | ||
private bool Write { get; } = write || manage; | ||
private bool Manage { get; } = manage; | ||
// Constructor | ||
public SnippetAccessPermission(Guid snippetId, Guid accessorId, bool read, bool write, bool manage, bool snippetPublic) | ||
{ | ||
SnippetId = snippetId; | ||
AccessorId = accessorId; | ||
Read = read || write || manage; | ||
Write = write || manage; | ||
Manage = manage; | ||
IsPublicSnippet = snippetPublic; | ||
} | ||
|
||
public bool IsPublicSnippet => snippetPublic; | ||
public bool IsPrivateSnippet => !snippetPublic; | ||
public SnippetAccessPermission() | ||
{ | ||
|
||
} | ||
|
||
public bool Any() | ||
{ | ||
return Any(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage); | ||
} | ||
|
||
public bool Any(params SnippetAccess[] accesses) | ||
{ | ||
foreach (var access in accesses) | ||
// Factory method for creating SnippetAccessPermission with no permissions | ||
public static SnippetAccessPermission NoPermission(Guid snippetId, Guid accessorId) => | ||
new SnippetAccessPermission(snippetId, accessorId, false, false, false, true); | ||
|
||
// Factory method for creating SnippetAccessPermission with an error state | ||
public static SnippetAccessPermission Error => | ||
new SnippetAccessPermission(Guid.Empty, Guid.Empty, false, false, false, true); | ||
|
||
public bool Any() | ||
{ | ||
return Any(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage); | ||
} | ||
|
||
public bool Any(params SnippetAccess[] accesses) | ||
{ | ||
switch (access) | ||
foreach (var access in accesses) | ||
{ | ||
case SnippetAccess.Read when Read: | ||
case SnippetAccess.Write when Write: | ||
case SnippetAccess.Manage when Manage: | ||
return true; | ||
switch (access) | ||
{ | ||
case SnippetAccess.Read when Read: | ||
case SnippetAccess.Write when Write: | ||
case SnippetAccess.Manage when Manage: | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
public bool All() | ||
{ | ||
return All(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage); | ||
} | ||
public bool All() | ||
{ | ||
return All(SnippetAccess.Read, SnippetAccess.Write, SnippetAccess.Manage); | ||
} | ||
|
||
public bool All(params SnippetAccess[] accesses) | ||
{ | ||
foreach (var access in accesses) | ||
public bool All(params SnippetAccess[] accesses) | ||
{ | ||
switch (access) | ||
foreach (var access in accesses) | ||
{ | ||
case SnippetAccess.Read when !Read: | ||
case SnippetAccess.Write when !Write: | ||
case SnippetAccess.Manage when !Manage: | ||
return false; | ||
switch (access) | ||
{ | ||
case SnippetAccess.Read when !Read: | ||
case SnippetAccess.Write when !Write: | ||
case SnippetAccess.Manage when !Manage: | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
return true; | ||
} | ||
|
||
public SnippetAccessControlDto ToControlModel() | ||
{ | ||
return new SnippetAccessControlDto() | ||
public SnippetAccessControlDto ToControlModel() | ||
{ | ||
UserId = AccessorId, | ||
Manage = manage, | ||
Read = read, | ||
Write = write | ||
}; | ||
return new SnippetAccessControlDto() | ||
{ | ||
UserId = AccessorId, | ||
Manage = Manage, | ||
Read = Read, | ||
Write = Write | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters