| | | 1 | | using CShells.Features; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Identity.HostedServices; |
| | | 4 | | using Elsa.Identity.Options; |
| | | 5 | | using Elsa.PackageManifest.Generator.Hints; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Identity.ShellFeatures; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Feature that initializes an admin user from configuration if provided. |
| | | 13 | | /// </summary> |
| | | 14 | | [ShellFeature( |
| | | 15 | | DisplayName = "Default Admin User Initialization", |
| | | 16 | | Description = "Initializes a default admin user from configuration if provided", |
| | | 17 | | DependsOn = ["Identity"])] |
| | | 18 | | [UsedImplicitly] |
| | | 19 | | public class DefaultAdminUserFeature : IShellFeature |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets or sets the admin user name. Must be explicitly configured to enable user bootstrap. |
| | | 23 | | /// </summary> |
| | | 24 | | [ManifestSetting( |
| | | 25 | | DisplayName = "Admin User Name", |
| | | 26 | | Description = "User name for the default admin account to bootstrap.", |
| | | 27 | | Category = "Bootstrap", |
| | | 28 | | RestartRequired = true)] |
| | 0 | 29 | | public string AdminUserName { get; set; } = ""; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the admin user password. Must be explicitly configured to enable user bootstrap. |
| | | 33 | | /// </summary> |
| | | 34 | | [ManifestSetting( |
| | | 35 | | DisplayName = "Admin Password", |
| | | 36 | | Description = "Password for the default admin account to bootstrap.", |
| | | 37 | | Category = "Bootstrap", |
| | | 38 | | Secret = true, |
| | | 39 | | Sensitive = true, |
| | | 40 | | RestartRequired = true)] |
| | 0 | 41 | | public string AdminPassword { get; set; } = ""; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the admin role name. |
| | | 45 | | /// </summary> |
| | | 46 | | [ManifestSetting( |
| | | 47 | | DisplayName = "Admin Role Name", |
| | | 48 | | Description = "Role assigned to the default admin account.", |
| | | 49 | | Category = "Bootstrap", |
| | | 50 | | DefaultValue = "admin", |
| | | 51 | | RestartRequired = true)] |
| | 0 | 52 | | public string AdminRoleName { get; set; } = "admin"; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets or sets the admin role permissions. |
| | | 56 | | /// </summary> |
| | | 57 | | [ManifestSetting( |
| | | 58 | | DisplayName = "Admin Role Permissions", |
| | | 59 | | Description = "Permissions assigned to the default admin role.", |
| | | 60 | | Category = "Bootstrap", |
| | | 61 | | DefaultValue = "*", |
| | | 62 | | RestartRequired = true)] |
| | 0 | 63 | | public ICollection<string> AdminRolePermissions { get; set; } = ["*"]; |
| | | 64 | | |
| | | 65 | | public void ConfigureServices(IServiceCollection services) |
| | | 66 | | { |
| | 0 | 67 | | services.Configure<DefaultAdminUserOptions>(options => |
| | 0 | 68 | | { |
| | 0 | 69 | | options.AdminUserName = AdminUserName; |
| | 0 | 70 | | options.AdminPassword = AdminPassword; |
| | 0 | 71 | | options.AdminRoleName = AdminRoleName; |
| | 0 | 72 | | options.AdminRolePermissions = AdminRolePermissions; |
| | 0 | 73 | | }); |
| | 0 | 74 | | services.AddBackgroundTask<AdminUserInitializer>(); |
| | 0 | 75 | | } |
| | | 76 | | } |