-
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
Showing
21 changed files
with
303 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace fim_queueing_admin.Auth; | ||
|
||
public class UserAccessHandler : AuthorizationHandler<UserAccessRequirement> | ||
{ | ||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserAccessRequirement requirement) | ||
{ | ||
// If user does not have the claim, get out of here | ||
if (!context.User.HasClaim(c => c.Type == ClaimTypes.AccessLevel)) return Task.CompletedTask; | ||
|
||
// Get their level | ||
var accessLevel = context.User.FindFirst(c => c.Type == ClaimTypes.AccessLevel)!.Value; | ||
|
||
// Succeed if the level has the necessary permission | ||
if (Action.ActionMap[accessLevel].Contains(requirement.Action)) context.Succeed(requirement); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace fim_queueing_admin.Auth; | ||
|
||
public class UserAccessLevel | ||
{ | ||
public const string ReadOnly = "2687ba63-c15a-47fa-bbc3-b9c4802a6fd6"; | ||
public const string Editor = "3c3aac31-0349-4b90-bafb-3344bd4c8333"; | ||
public const string Admin = "9ab85fca-724e-42d5-a4a2-cd1dff1f45e2"; | ||
} | ||
|
||
public static class Action | ||
{ | ||
public const string ViewDisplay = nameof(ViewDisplay); | ||
public const string ManageDisplay = nameof(ManageDisplay); | ||
public const string ViewEvent = nameof(ViewEvent); | ||
public const string ManageEvent = nameof(ManageEvent); | ||
public const string CreateEvent = nameof(CreateEvent); | ||
public const string ViewAlert = nameof(ViewAlert); | ||
public const string ManageAlert = nameof(ManageAlert); | ||
public const string CreateAlert = nameof(CreateAlert); | ||
public const string ViewCart = nameof(ViewCart); | ||
public const string ManageCart = nameof(ManageCart); | ||
public const string CreateCart = nameof(CreateCart); | ||
public const string ViewUser = nameof(ViewUser); | ||
public const string ManageUser = nameof(ManageUser); | ||
public const string Admin = nameof(Admin); | ||
|
||
internal static readonly Dictionary<string, string[]> ActionMap = new() | ||
{ | ||
{ | ||
UserAccessLevel.ReadOnly, | ||
[ViewDisplay, ViewEvent, ViewAlert, ViewCart] | ||
}, | ||
{ | ||
UserAccessLevel.Editor, | ||
[ | ||
ViewDisplay, ManageDisplay, ViewEvent, ManageEvent, | ||
ViewAlert, ManageAlert, CreateAlert, ViewCart, ManageCart | ||
] | ||
}, | ||
{ | ||
UserAccessLevel.Admin, | ||
typeof(Action).GetFields().Select(f => (string)f.GetValue(null)!).ToArray() // All permissions | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace fim_queueing_admin.Auth; | ||
|
||
public class UserAccessRequirement(string action) : IAuthorizationRequirement | ||
{ | ||
public string Action { get; } = action; | ||
} | ||
|
||
public class AuthorizeOperationAttribute(string action) : AuthorizeAttribute, | ||
IAuthorizationRequirementData | ||
{ | ||
public IEnumerable<IAuthorizationRequirement> GetRequirements() | ||
{ | ||
return new[] { new UserAccessRequirement(action) }; | ||
} | ||
} |
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
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 was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.