ConfigurationService.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Security.Principal;
  3. using System.Threading.Tasks;
  4. using BO.AppServer.Metadata.Enums;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Quadarax.Foundation.Core.Business;
  9. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  10. namespace BO.AppServer.Business.Services
  11. {
  12. public class ConfigurationService : AbstractService
  13. {
  14. private IServiceProvider _serviceProvider;
  15. public Configuration.Configuration Configuration { get; }
  16. public ConfigurationService(IOptions<Configuration.Configuration> config,
  17. IServiceProvider serviceProvider,
  18. IPrincipal currentPrincipal,
  19. ILoggerFactory logger) : base(currentPrincipal, logger)
  20. {
  21. if (config == null)
  22. throw new ArgumentNullException(nameof(config));
  23. Configuration = config.Value;
  24. _serviceProvider = serviceProvider;
  25. }
  26. public async Task<ResultPlain> Install()
  27. {
  28. using (var scope = _serviceProvider.CreateScope())
  29. {
  30. var srvUsers = scope.ServiceProvider.GetRequiredService<UserService>();
  31. await srvUsers.EnsureRolesAsync();
  32. await EnsureUser(srvUsers, Configuration.System.AccountSystem.Name,
  33. Configuration.System.AccountSystem.Password,
  34. Configuration.System.AccountSystem.Email,
  35. Configuration.System.AccountSystem.CultureLcid, new[]
  36. {
  37. RoleEnum.System,
  38. });
  39. await EnsureUser(srvUsers, Configuration.System.AccountConsole.Name,
  40. Configuration.System.AccountConsole.Password,
  41. Configuration.System.AccountConsole.Email,
  42. Configuration.System.AccountConsole.CultureLcid, new[]
  43. {
  44. RoleEnum.System,
  45. RoleEnum.Console,
  46. RoleEnum.SuperAdmin
  47. });
  48. await EnsureUser(srvUsers, Configuration.System.AccountAdmin.Name,
  49. Configuration.System.AccountAdmin.Password,
  50. Configuration.System.AccountAdmin.Email,
  51. Configuration.System.AccountAdmin.CultureLcid, new[]
  52. {
  53. RoleEnum.User,
  54. RoleEnum.SuperAdmin
  55. });
  56. }
  57. return new ResultPlain();
  58. }
  59. protected async Task EnsureUser(UserService srvUsers, string userName, string userPassword, string userEmail, int prefferedLCID,
  60. RoleEnum[] roles)
  61. {
  62. if (!(await srvUsers.ExistsUserByNameAsync(userName)).Value)
  63. await srvUsers.CreateUserAsync(userName,
  64. userPassword,
  65. userEmail,
  66. prefferedLCID, roles);
  67. else
  68. await srvUsers.EnusreUserRolesAsync(userName, roles);
  69. }
  70. }
  71. }