-
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
109 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Api.Entities; | ||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Api.Authorization; | ||
|
||
public class AuthorizeMembersAttribute: AuthorizeAttribute | ||
{ | ||
public AuthorizeMembersAttribute(params UserMemberType[] memberTypes) | ||
{ | ||
Roles = string.Join(",", memberTypes); | ||
} | ||
} |
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,31 @@ | ||
using System.Security.Claims; | ||
using Api.Context; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Api.Authorization; | ||
|
||
public class ClaimsTransformation(AppDbContext dbContext): IClaimsTransformation | ||
{ | ||
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) | ||
{ | ||
var sub = principal.Claims.SingleOrDefault(c => c.Type == "user_id"); | ||
if (sub is null) | ||
{ | ||
return principal; | ||
} | ||
|
||
var user = await dbContext.Users.SingleOrDefaultAsync(u => u.FirebaseId == sub.Value); | ||
if (user is null) | ||
{ | ||
return principal; | ||
} | ||
|
||
var ci = new ClaimsIdentity(); | ||
ci.AddClaim(new Claim(ClaimTypes.Role, user.MemberType.ToString())); | ||
|
||
principal.AddIdentity(ci); | ||
|
||
return principal; | ||
} | ||
} |
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