Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seggregate authorizeable interface #28

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ namespace CleanArchitecture.Application.Common.Security.Request;

public interface IAuthorizeableRequest<T> : IRequest<T>
{
Guid UserId { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CleanArchitecture.Application.Common.Security.Request;

public interface IUserAuthorizeableRequest<T> : IAuthorizeableRequest<T>
{
Guid UserId { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ namespace CleanArchitecture.Application.Reminders.Commands.SetReminder;

[Authorize(Permissions = Permission.Reminder.Set, Policies = Policy.SelfOrAdmin)]
public record SetReminderCommand(Guid UserId, Guid SubscriptionId, string Text, DateTime DateTime)
: IAuthorizeableRequest<ErrorOr<Reminder>>;
: IUserAuthorizeableRequest<ErrorOr<Reminder>>;
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public ErrorOr<Success> Authorize<T>(
CurrentUser currentUser,
string policy)
{
return policy switch
return request switch
{
Policy.SelfOrAdmin => SelfOrAdminPolicy(request, currentUser),
_ => Error.Unexpected(description: "Unknown policy name"),
IUserAuthorizeableRequest<T> userAuthorizeableRequest => UserPolicyEnforcer.Authorize(
userAuthorizeableRequest,
currentUser,
policy),
_ => Result.Success,
};
}

private static ErrorOr<Success> SelfOrAdminPolicy<T>(IAuthorizeableRequest<T> request, CurrentUser currentUser) =>
request.UserId == currentUser.Id || currentUser.Roles.Contains(Role.Admin)
? Result.Success
: Error.Unauthorized(description: "Requesting user failed policy requirement");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CleanArchitecture.Application.Common.Security.Policies;
using CleanArchitecture.Application.Common.Security.Request;
using CleanArchitecture.Application.Common.Security.Roles;
using CleanArchitecture.Infrastructure.Security.CurrentUserProvider;

using ErrorOr;

namespace CleanArchitecture.Infrastructure.Security.PolicyEnforcer;

public static class UserPolicyEnforcer
{
public static ErrorOr<Success> Authorize<T>(
IUserAuthorizeableRequest<T> request,
CurrentUser currentUser,
string policy)
{
return policy switch
{
Policy.SelfOrAdmin => SelfOrAdminPolicy(request, currentUser),
_ => Error.Unexpected(description: "Unknown policy name"),
};
}

private static ErrorOr<Success> SelfOrAdminPolicy<T>(IUserAuthorizeableRequest<T> request, CurrentUser currentUser) =>
request.UserId == currentUser.Id || currentUser.Roles.Contains(Role.Admin)
? Result.Success
: Error.Unauthorized(description: "Requesting user failed policy requirement");
}
Loading