| | | 1 | | using Dapper; |
| | | 2 | | using Syki.Back.Cache; |
| | | 3 | | using Syki.Back.Auth.Roles; |
| | | 4 | | using Syki.Back.Domain.Identity; |
| | | 5 | | using Syki.Back.Database.Identity; |
| | | 6 | | |
| | | 7 | | namespace Syki.Back.Database; |
| | | 8 | | |
| | | 9 | | public partial class SykiDbContext |
| | | 10 | | { |
| | 3084 | 11 | | public DbSet<MagicLink> WebMagicLinks { get; set; } |
| | 2618 | 12 | | public DbSet<ResetPasswordToken> ResetPasswordTokens { get; set; } |
| | | 13 | | |
| | 2546 | 14 | | public DbSet<SsoConfiguration> WebSsoConfigurations { get; set; } |
| | 2546 | 15 | | public DbSet<SsoAllowedDomain> WebSsoAllowedDomains { get; set; } |
| | | 16 | | |
| | 2546 | 17 | | public DbSet<UserSocialLogin> UserSocialLogins { get; set; } |
| | | 18 | | |
| | | 19 | | private static void ConfigureIdentity(ModelBuilder modelBuilder) |
| | | 20 | | { |
| | 4 | 21 | | modelBuilder.ApplyConfiguration(new MagicLinkDbConfig()); |
| | 4 | 22 | | modelBuilder.ApplyConfiguration(new ResetPasswordTokenDbConfig()); |
| | | 23 | | |
| | 4 | 24 | | modelBuilder.ApplyConfiguration(new SykiRoleDbConfig()); |
| | 4 | 25 | | modelBuilder.ApplyConfiguration(new SykiUserDbConfig()); |
| | 4 | 26 | | modelBuilder.ApplyConfiguration(new SykiUserRoleDbConfig()); |
| | 4 | 27 | | modelBuilder.ApplyConfiguration(new SykiRoleClaimDbConfig()); |
| | 4 | 28 | | modelBuilder.ApplyConfiguration(new SykiUserClaimDbConfig()); |
| | 4 | 29 | | modelBuilder.ApplyConfiguration(new SykiUserTokenDbConfig()); |
| | 4 | 30 | | modelBuilder.ApplyConfiguration(new SykiUserLoginDbConfig()); |
| | | 31 | | |
| | 4 | 32 | | modelBuilder.ApplyConfiguration(new InstitutionRoleDbConfig()); |
| | | 33 | | |
| | 4 | 34 | | modelBuilder.ApplyConfiguration(new SsoConfigurationDbConfig()); |
| | 4 | 35 | | modelBuilder.ApplyConfiguration(new SsoAllowedDomainDbConfig()); |
| | | 36 | | |
| | 4 | 37 | | modelBuilder.ApplyConfiguration(new UserSocialLoginDbConfig()); |
| | | 38 | | |
| | 4 | 39 | | modelBuilder.ApplyConfiguration(new DataProtectionKeyDbConfig()); |
| | 4 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<SykiRole> GetUserRole(int userId, int institutionId) |
| | | 43 | | { |
| | 282 | 44 | | var userRole = await UserRoles.Where(x => x.UserId == userId && x.InstitutionId == institutionId).FirstAsync(); |
| | | 45 | | |
| | 282 | 46 | | return await Roles.Where(x => x.Id == userRole.RoleId).FirstAsync(); |
| | 282 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<SykiRole> GetDirectorRole() |
| | | 50 | | { |
| | 292 | 51 | | return await Cache.GetOrCreateAsync( |
| | 292 | 52 | | key: $"{CacheKeys.GetDirectorRole}", |
| | 292 | 53 | | state: this, |
| | 292 | 54 | | options: new() { Expiration = TimeSpan.FromDays(100) }, |
| | 292 | 55 | | factory: async (state, ct) => |
| | 292 | 56 | | { |
| | 2 | 57 | | return await state.Roles.AsNoTracking() |
| | 2 | 58 | | .Where(x => x.OwnerId == null && x.NormalizedName == SykiDefaultRoles.Director.NormalizedName) |
| | 2 | 59 | | .FirstAsync(ct); |
| | 2 | 60 | | } |
| | 292 | 61 | | ); |
| | 292 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<SykiRole> GetTeacherRole() |
| | | 65 | | { |
| | 30 | 66 | | return await Cache.GetOrCreateAsync( |
| | 30 | 67 | | key: $"{CacheKeys.GetTeacherRole}", |
| | 30 | 68 | | state: this, |
| | 30 | 69 | | options: new() { Expiration = TimeSpan.FromDays(100) }, |
| | 30 | 70 | | factory: async (state, ct) => |
| | 30 | 71 | | { |
| | 2 | 72 | | return await state.Roles.AsNoTracking() |
| | 2 | 73 | | .Where(x => x.OwnerId == null && x.NormalizedName == SykiDefaultRoles.Teacher.NormalizedName) |
| | 2 | 74 | | .FirstAsync(ct); |
| | 2 | 75 | | } |
| | 30 | 76 | | ); |
| | 30 | 77 | | } |
| | | 78 | | |
| | | 79 | | public async Task<SykiRole> GetStudentRole() |
| | | 80 | | { |
| | 6 | 81 | | return await Cache.GetOrCreateAsync( |
| | 6 | 82 | | key: $"{CacheKeys.GetStudentRole}", |
| | 6 | 83 | | state: this, |
| | 6 | 84 | | options: new() { Expiration = TimeSpan.FromDays(100) }, |
| | 6 | 85 | | factory: async (state, ct) => |
| | 6 | 86 | | { |
| | 2 | 87 | | return await state.Roles.AsNoTracking() |
| | 2 | 88 | | .Where(x => x.OwnerId == null && x.NormalizedName == SykiDefaultRoles.Student.NormalizedName) |
| | 2 | 89 | | .FirstAsync(ct); |
| | 2 | 90 | | } |
| | 6 | 91 | | ); |
| | 6 | 92 | | } |
| | | 93 | | |
| | | 94 | | public async Task<SsoConfiguration?> GetActiveSsoConfigForSchemeAsync(Guid publicId) |
| | | 95 | | { |
| | | 96 | | const string sql = @" |
| | | 97 | | SELECT |
| | | 98 | | id, |
| | | 99 | | external_id, |
| | | 100 | | authority, |
| | | 101 | | client_id, |
| | | 102 | | client_secret, |
| | | 103 | | updated_at |
| | | 104 | | FROM |
| | | 105 | | syki.sso_configurations |
| | | 106 | | WHERE |
| | | 107 | | public_id = @PublicId AND is_active = true |
| | | 108 | | LIMIT 1 |
| | | 109 | | "; |
| | | 110 | | |
| | 0 | 111 | | return await Database.GetDbConnection().QueryFirstOrDefaultAsync<SsoConfiguration?>(sql, new { PublicId = public |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | public async Task<bool> EmailRequiresSsoAsync(string email) |
| | | 115 | | { |
| | | 116 | | const string sql = @" |
| | | 117 | | SELECT |
| | | 118 | | count(1) > 0 |
| | | 119 | | FROM |
| | | 120 | | syki.sso_allowed_domains d |
| | | 121 | | INNER JOIN |
| | | 122 | | syki.sso_configurations c ON c.id = d.sso_configuration_id |
| | | 123 | | WHERE |
| | | 124 | | d.domain = @Domain |
| | | 125 | | AND |
| | | 126 | | c.is_active = true |
| | | 127 | | AND |
| | | 128 | | c.require_sso = true |
| | | 129 | | "; |
| | | 130 | | |
| | 0 | 131 | | var domain = email.Split('@').Last().ToLowerInvariant(); |
| | 0 | 132 | | return await Database.GetDbConnection().QuerySingleAsync<bool>(sql, new { domain }); |
| | 0 | 133 | | } |
| | | 134 | | } |