diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Models/Role.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Models/Role.cs index f2afd89..08e4fb4 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Models/Role.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Models/Role.cs @@ -1,5 +1,7 @@ namespace Elektrifikatsiya.Models; +// Higher up = Higher permission +// Or you can set the priority manually = public enum Role { Admin, diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs index 99fc256..175c25d 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Models/User.cs @@ -13,10 +13,12 @@ public class User public string PasswordHash { get; private set; } public string? SessionToken { get; set; } public DateTime LastLoginDate { get; set; } + public Role Role { get; set; } - public User(string name, string passwordHash) + public User(string name, string passwordHash, Role role) { Name = name; PasswordHash = passwordHash; + Role = role; } } \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthenticationService.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthenticationService.cs index ac34b36..84b1adf 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthenticationService.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthenticationService.cs @@ -1,5 +1,7 @@ using Elektrifikatsiya.Models; +using FluentResults; + namespace Elektrifikatsiya.Services; public interface IAuthenticationService @@ -8,7 +10,7 @@ public interface IAuthenticationService /// Gets the current user. /// /// to and the currently authenticated user. - Task GetUserAsync(); + Task> GetUserAsync(); /// /// Registers a new user. @@ -16,38 +18,38 @@ public interface IAuthenticationService /// The E-mail address of the user. /// A to . - Task RegisterUserAsync(string name, string password); + Task RegisterUserAsync(string name, string password); /// /// Logs a user in. /// /// The E-mail address of the user. /// A to . - Task<(bool Success, string? Message)> LoginUserAsync(string name, string password); + Task LoginUserAsync(string name, string password); /// /// Logs the user out. /// /// A to . - Task LogoutUserAsync(); + Task LogoutUserAsync(); /// /// Deletes the current user. /// /// The deletion token. /// A to and a that indicates if the operation was successful. - Task DeleteUserAsync(string deletionToken); + Task DeleteUserAsync(string deletionToken); /// /// Checks if a user exists. /// /// The E-mail address of the user. /// A to and a that indicates if the user exists. - Task UserExistsAsync(string name); + Task> UserExistsAsync(string name); /// /// Check if the current user is authenticated. /// /// A to and a that indicates if the user is authenticated. - Task IsAuthenticated(); + Task> IsAuthenticated(); } \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthorizationService.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthorizationService.cs index 223d9c8..30be3d0 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthorizationService.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Services/IAuthorizationService.cs @@ -1,5 +1,7 @@ using Elektrifikatsiya.Models; +using FluentResults; + namespace Elektrifikatsiya.Services; public interface IAuthorizationService @@ -9,5 +11,5 @@ public interface IAuthorizationService /// /// The minimum required role. /// A to and a that indicates if the user is authorized. - Task IsAuthorized(Role requiredRole); + Task> IsAuthorized(Role requiredRole); } \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthenticationService.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthenticationService.cs new file mode 100644 index 0000000..1a3c2ae --- /dev/null +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthenticationService.cs @@ -0,0 +1,43 @@ +using Elektrifikatsiya.Models; + +using FluentResults; + +namespace Elektrifikatsiya.Services.Implementations; + +public class AuthenticationService : IAuthenticationService +{ + public Task DeleteUserAsync(string deletionToken) + { + throw new NotImplementedException(); + } + + public Task> GetUserAsync() + { + throw new NotImplementedException(); + } + + public Task> IsAuthenticated() + { + throw new NotImplementedException(); + } + + public Task LoginUserAsync(string name, string password) + { + throw new NotImplementedException(); + } + + public Task LogoutUserAsync() + { + throw new NotImplementedException(); + } + + public Task RegisterUserAsync(string name, string password) + { + throw new NotImplementedException(); + } + + public Task> UserExistsAsync(string name) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthorizationService.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthorizationService.cs new file mode 100644 index 0000000..3a2f4c4 --- /dev/null +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/AuthorizationService.cs @@ -0,0 +1,29 @@ +using Elektrifikatsiya.Models; + +using FluentResults; + +namespace Elektrifikatsiya.Services.Implementations; + +public class AuthorizationService : IAuthorizationService +{ + private readonly IAuthenticationService authenticationService; + + public AuthorizationService(IAuthenticationService authenticationService) + { + this.authenticationService = authenticationService; + } + + public async Task> IsAuthorized(Role requiredRole) + { + Result userResult = await authenticationService.GetUserAsync(); + + if (userResult.IsSuccess && userResult.Value.Role <= requiredRole) + { + return Result.Ok(true); + } + else + { + return Result.Fail("Use doesn't have the required role!"); + } + } +} \ No newline at end of file