using System; using System.Security.Principal; using System.Threading.Tasks; using BO.AppServer.Metadata.Dto; using BO.AppServer.Metadata.Enums; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Quadarax.Foundation.Core.Business; using Quadarax.Foundation.Core.Data.Interface.Entity.Dto; namespace BO.AppServer.Business.Services { public class ConfigurationService : AbstractService { private IServiceProvider _serviceProvider; public Configuration.Configuration Configuration { get; } public ConfigurationService(IOptions config, IServiceProvider serviceProvider, IPrincipal currentPrincipal, ILoggerFactory logger) : base(currentPrincipal, logger) { if (config == null) throw new ArgumentNullException(nameof(config)); Configuration = config.Value; _serviceProvider = serviceProvider; } public async Task Install() { using (var scope = _serviceProvider.CreateScope()) { var srvUsers = scope.ServiceProvider.GetRequiredService(); await srvUsers.EnsureRolesAsync(); await EnsureUser(srvUsers, Configuration.System.AccountSystem.Name, Configuration.System.AccountSystem.Password, Configuration.System.AccountSystem.Email, Configuration.System.AccountSystem.CultureLcid, new[] { RoleEnum.System, }); await EnsureUser(srvUsers, Configuration.System.AccountConsole.Name, Configuration.System.AccountConsole.Password, Configuration.System.AccountConsole.Email, Configuration.System.AccountConsole.CultureLcid, new[] { RoleEnum.System, RoleEnum.Console, RoleEnum.SuperAdmin }); await EnsureUser(srvUsers, Configuration.System.AccountAdmin.Name, Configuration.System.AccountAdmin.Password, Configuration.System.AccountAdmin.Email, Configuration.System.AccountAdmin.CultureLcid, new[] { RoleEnum.User, RoleEnum.SuperAdmin }); } return new ResultPlain(); } public async Task InstallDev() { using (var scope = _serviceProvider.CreateScope()) { var srvCatalogues = scope.ServiceProvider.GetRequiredService(); var srvUsers = scope.ServiceProvider.GetRequiredService(); var srvWorkspaces = scope.ServiceProvider.GetRequiredService(); await srvCatalogues.CreateBillingPlanAsync(new BillingPlanCDto() { Name = "Unlimited", Code = "UNLIMITED", Description = "This plan is only for testing purpose", PstinitialCredits = 1000, PstcreditsPerItem = 1, Pstwatermark = true, Pstquality = 1, PstprocessProfile = "PRC01", Psttest = true, Price = 1, Currency = "EUR", IsCustom = false, IsHidden = false, IsEnabled = true }, true); await srvCatalogues.CreateMimeTypeAsync(new MimeTypeCDto() { Mimetype1 = "text/plain", Description = "Plain text file", Extensions = "txt", IsEnabled = true }, true); await EnsureUser(srvUsers, "test", "password","test@bo.com",1029, new[] { RoleEnum.User, RoleEnum.Admin }); await srvWorkspaces.CreateWorkspaceAsync(new WorkspaceCDto() { Name = "TestWorkspace", Apipassword = "test", BillingPlanCode = "UNLIMITED", AssignedUserName = "test" }); } return new ResultPlain(); } protected async Task EnsureUser(UserService srvUsers, string userName, string userPassword, string userEmail, int prefferedLCID, RoleEnum[] roles) { if (!(await srvUsers.ExistsUserByNameAsync(userName)).Value) await srvUsers.CreateUserAsync(userName, userPassword, userEmail, prefferedLCID, roles); else await srvUsers.EnusreUserRolesAsync(userName, roles); } } }