| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik <[email protected]> |
| | | 2 | | // |
| | | 3 | | // SPDX-License-Identifier: AGPL-3.0-or-later |
| | | 4 | | |
| | | 5 | | using Api.Auth.Handlers; |
| | | 6 | | using Api.Auth.Models; |
| | | 7 | | using Api.Auth.Utils; |
| | | 8 | | using Api.Database; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | using Microsoft.EntityFrameworkCore.Metadata.Internal; |
| | | 11 | | |
| | | 12 | | namespace Api.Auth; |
| | | 13 | | |
| | | 14 | | public static class Setup |
| | | 15 | | { |
| | | 16 | | public static void RegisterServices(IServiceCollection services) |
| | 1 | 17 | | { |
| | 1 | 18 | | services.AddScoped<ICurrentServiceId, CurrentServiceId>(); |
| | 1 | 19 | | services.AddScoped<ICurrentUserId, CurrentUserId>(); |
| | 1 | 20 | | services.AddHttpContextAccessor(); |
| | 1 | 21 | | services.AddSingleton<IAuthorizationPolicyProvider, PermissionPolicyProvider>(); |
| | 1 | 22 | | } |
| | | 23 | | |
| | 1 | 24 | | public static void MapEndpoints(IEndpointRouteBuilder route) { } |
| | | 25 | | |
| | | 26 | | public static async Task SeedDb(PGContext db, CancellationToken ct) |
| | 1 | 27 | | { |
| | 1 | 28 | | var instanceService = await db.RemoteServices.FindAsync( |
| | 1 | 29 | | [RemoteService.TheInstanceServiceId], |
| | 1 | 30 | | cancellationToken: ct |
| | 1 | 31 | | ); |
| | | 32 | | |
| | 1 | 33 | | if (instanceService is null) |
| | 1 | 34 | | { |
| | 1 | 35 | | var now = Now(); |
| | 1 | 36 | | instanceService = new RemoteService() |
| | 1 | 37 | | { |
| | 1 | 38 | | Id = RemoteService.TheInstanceServiceId, |
| | 1 | 39 | | MetadataAddedAt = now, |
| | 1 | 40 | | MetadataUpdatedAt = now, |
| | 1 | 41 | | IsInstanceService = true, |
| | 1 | 42 | | DefaultInstanceWidePermisssion = UserPermissionBits.All, |
| | 1 | 43 | | Name = RemoteService.TheInstanceServiceName, |
| | 1 | 44 | | }; |
| | 1 | 45 | | await db.RemoteServices.AddAsync(instanceService, ct); |
| | 1 | 46 | | await db.SaveChangesAsync(ct); |
| | 1 | 47 | | } |
| | 0 | 48 | | else if ( |
| | 0 | 49 | | instanceService.IsInstanceService != true |
| | 0 | 50 | | || instanceService.Name != RemoteService.TheInstanceServiceName |
| | 0 | 51 | | || instanceService.DefaultInstanceWidePermisssion != UserPermissionBits.All |
| | 0 | 52 | | ) |
| | 0 | 53 | | { |
| | 0 | 54 | | instanceService.Name = RemoteService.TheInstanceServiceName; |
| | 0 | 55 | | instanceService.IsInstanceService = true; |
| | 0 | 56 | | instanceService.DefaultInstanceWidePermisssion = UserPermissionBits.All; |
| | 0 | 57 | | instanceService.MetadataUpdatedAt = Now(); |
| | | 58 | | |
| | 0 | 59 | | await db.SaveChangesAsync(ct); |
| | 0 | 60 | | } |
| | 1 | 61 | | } |
| | | 62 | | } |