using System; using System.Security.Principal; using System.Threading.Tasks; 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(); } 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); } } }