-
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
6 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
backend/src/Logitar.Cms.Web/Authorization/UserAuthorizationHandler.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Logitar.Cms.Contracts.Users; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Logitar.Cms.Web.Authorization; | ||
|
||
public class UserAuthorizationHandler : AuthorizationHandler<UserAuthorizationRequirement> | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
||
public UserAuthorizationHandler(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserAuthorizationRequirement requirement) | ||
{ | ||
HttpContext? httpContext = _httpContextAccessor.HttpContext; | ||
if (httpContext != null) | ||
{ | ||
User? user = httpContext.GetUser(); | ||
if (user == null) | ||
{ | ||
context.Fail(new AuthorizationFailureReason(this, "The actor must be an authenticated user.")); | ||
} | ||
else | ||
{ | ||
context.Succeed(requirement); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
backend/src/Logitar.Cms.Web/Authorization/UserAuthorizationRequirement.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Logitar.Cms.Web.Authorization; | ||
|
||
public class UserAuthorizationRequirement : IAuthorizationRequirement; |
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,6 @@ | ||
namespace Logitar.Cms.Web.Constants; | ||
|
||
public static class Policies | ||
{ | ||
public const string User = nameof(User); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Logitar.Cms.Contracts; | ||
using Logitar.Cms.Contracts.Users; | ||
|
||
namespace Logitar.Cms.Web.Controllers; | ||
|
||
public record UserProfile | ||
{ | ||
public string Username { get; set; } | ||
|
||
public DateTime? PasswordChangedOn { get; set; } | ||
|
||
public string? EmailAddress { get; set; } | ||
|
||
public string? FirstName { get; set; } | ||
public string? MiddleName { get; set; } | ||
public string? LastName { get; set; } | ||
public string? FullName { get; set; } | ||
|
||
public Locale? Locale { get; set; } | ||
|
||
public string? Picture { get; set; } | ||
|
||
public DateTime CreatedOn { get; set; } | ||
public DateTime UpdatedOn { get; set; } | ||
public DateTime AuthenticatedOn { get; set; } | ||
|
||
public UserProfile() : this(string.Empty) | ||
{ | ||
} | ||
|
||
public UserProfile(string username) | ||
{ | ||
Username = username; | ||
} | ||
|
||
public UserProfile(User user) : this(user.UniqueName) | ||
{ | ||
PasswordChangedOn = user.PasswordChangedOn; | ||
|
||
EmailAddress = user.Email?.Address; | ||
|
||
FirstName = user.FirstName; | ||
MiddleName = user.MiddleName; | ||
LastName = user.LastName; | ||
FullName = user.FullName; | ||
|
||
Locale = user.Locale; | ||
|
||
Picture = user.Picture; | ||
|
||
CreatedOn = user.CreatedOn; | ||
UpdatedOn = user.UpdatedOn; | ||
AuthenticatedOn = user.AuthenticatedOn ?? user.UpdatedOn; | ||
} | ||
} |
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