| | 1 | | using System.Security.Claims; |
| | 2 | | using Syki.Back.Features.Cross.CreateUser; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Extensions; |
| | 5 | |
|
| | 6 | | public static class UserExtensions |
| | 7 | | { |
| | 8 | | public static Guid InstitutionId(this ClaimsPrincipal user) |
| | 9 | | { |
| 11389 | 10 | | return Guid.Parse(user.FindFirstValue("institution")!); |
| | 11 | | } |
| | 12 | |
|
| | 13 | | public static Guid Id(this ClaimsPrincipal user) |
| | 14 | | { |
| 8012 | 15 | | return Guid.Parse(user.FindFirstValue("sub")!); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public static Guid GetCourseCurriculumId(this ClaimsPrincipal user) |
| | 19 | | { |
| 51 | 20 | | return Guid.Parse(user.FindFirstValue("CourseCurriculumId")!); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static async Task<bool> IsOnlyInRole(this UserManager<SykiUser> userManager, SykiUser user, UserRole role) |
| | 24 | | { |
| 4 | 25 | | var adm = await userManager.IsInRoleAsync(user!, UserRole.Adm.ToString()); |
| 4 | 26 | | var student = await userManager.IsInRoleAsync(user!, UserRole.Student.ToString()); |
| 4 | 27 | | var teacher = await userManager.IsInRoleAsync(user!, UserRole.Teacher.ToString()); |
| 4 | 28 | | var academic = await userManager.IsInRoleAsync(user!, UserRole.Academic.ToString()); |
| | 29 | |
|
| 4 | 30 | | return role switch |
| 4 | 31 | | { |
| 2 | 32 | | UserRole.Academic => academic && !(adm || student || teacher), |
| 1 | 33 | | UserRole.Student => student && !(adm || academic || teacher), |
| 1 | 34 | | UserRole.Teacher => teacher && !(adm || student || academic), |
| 0 | 35 | | UserRole.Adm => adm && !(student || teacher || academic), |
| 0 | 36 | | _ => false |
| 4 | 37 | | }; |
| 4 | 38 | | } |
| | 39 | |
|
| | 40 | | public static bool IsAuditable(this PathString path) |
| | 41 | | { |
| 9104 | 42 | | return |
| 9104 | 43 | | path != "/login" && |
| 9104 | 44 | | path != "/skip-user-register" && |
| 9104 | 45 | | path != "/login/mfa" && |
| 9104 | 46 | | path != "/reset-password" && |
| 9104 | 47 | | path != "/users" && |
| 9104 | 48 | | path != "/reset-password-token"; |
| | 49 | | } |
| | 50 | | } |