| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<Configuration.Configuration> 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<ResultPlain> Install()
- {
- using (var scope = _serviceProvider.CreateScope())
- {
- var srvUsers = scope.ServiceProvider.GetRequiredService<UserService>();
- 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);
- }
- }
- }
|