ConfigurationService.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Principal;
  4. using System.Threading.Tasks;
  5. using BO.AppServer.Metadata.Dto;
  6. using BO.AppServer.Metadata.Enums;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using Microsoft.Extensions.Logging;
  9. using Microsoft.Extensions.Options;
  10. using Quadarax.Foundation.Core.Business;
  11. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  12. namespace BO.AppServer.Business.Services
  13. {
  14. public class ConfigurationService : AbstractService
  15. {
  16. private IServiceProvider _serviceProvider;
  17. public Configuration.Configuration Configuration { get; }
  18. public ConfigurationService(IOptions<Configuration.Configuration> config,
  19. IServiceProvider serviceProvider,
  20. IPrincipal currentPrincipal,
  21. ILoggerFactory logger) : base(currentPrincipal, logger)
  22. {
  23. if (config == null)
  24. throw new ArgumentNullException(nameof(config));
  25. Configuration = config.Value;
  26. _serviceProvider = serviceProvider;
  27. }
  28. public async Task<ResultPlain> Install()
  29. {
  30. using (var scope = _serviceProvider.CreateScope())
  31. {
  32. var srvUsers = scope.ServiceProvider.GetRequiredService<UserService>();
  33. await srvUsers.EnsureRolesAsync();
  34. await EnsureUser(srvUsers, Configuration.System.AccountSystem.Name,
  35. Configuration.System.AccountSystem.Password,
  36. Configuration.System.AccountSystem.Email,
  37. Configuration.System.AccountSystem.CultureLcid, new[]
  38. {
  39. RoleEnum.System,
  40. });
  41. await EnsureUser(srvUsers, Configuration.System.AccountConsole.Name,
  42. Configuration.System.AccountConsole.Password,
  43. Configuration.System.AccountConsole.Email,
  44. Configuration.System.AccountConsole.CultureLcid, new[]
  45. {
  46. RoleEnum.System,
  47. RoleEnum.Console,
  48. RoleEnum.SuperAdmin
  49. });
  50. await EnsureUser(srvUsers, Configuration.System.AccountAdmin.Name,
  51. Configuration.System.AccountAdmin.Password,
  52. Configuration.System.AccountAdmin.Email,
  53. Configuration.System.AccountAdmin.CultureLcid, new[]
  54. {
  55. RoleEnum.User,
  56. RoleEnum.SuperAdmin
  57. });
  58. }
  59. return new ResultPlain();
  60. }
  61. public async Task<ResultPlain> InstallDev()
  62. {
  63. using (var scope = _serviceProvider.CreateScope())
  64. {
  65. var srvCatalogues = scope.ServiceProvider.GetRequiredService<CatalogueService>();
  66. var srvUsers = scope.ServiceProvider.GetRequiredService<UserService>();
  67. var srvWorkspaces = scope.ServiceProvider.GetRequiredService<WorkspaceService>();
  68. var task0 = srvCatalogues.CreateBillingPlanAsync(new BillingPlanCDto()
  69. {
  70. Name = "Unlimited",
  71. Code = "UNLIMITED",
  72. Description = "This plan is only for testing purpose",
  73. PstinitialCredits = 1000,
  74. PstcreditsPerItem = 1,
  75. Pstwatermark = true,
  76. Pstquality = 1,
  77. PstprocessProfile = "PRC01",
  78. Psttest = true,
  79. Price = 1,
  80. Currency = "EUR",
  81. IsCustom = false,
  82. IsHidden = false,
  83. IsEnabled = true
  84. }, true);
  85. var task1 = srvCatalogues.CreateMimeTypeAsync(new MimeTypeCDto()
  86. {
  87. Mimetype1 = "text/plain",
  88. Description = "Plain text file",
  89. Extensions = "txt",
  90. IsEnabled = true
  91. }, true);
  92. var task2 = srvCatalogues.CreateMimeTypeAsync(new MimeTypeCDto()
  93. {
  94. Mimetype1 = "text/plain",
  95. Description = "Plain text file",
  96. Extensions = "txt",
  97. IsEnabled = true
  98. }, true);
  99. var task3 = EnsureUser(srvUsers, "test", "password", "test@bo.com", 1029,
  100. new[] { RoleEnum.User, RoleEnum.Admin });
  101. Task.WhenAll(task0, task1, task2, task3)
  102. .ContinueWith(async (t) => await srvWorkspaces.CreateWorkspaceAsync(new WorkspaceCDto()
  103. {
  104. Name = "TestWorkspace",
  105. Apipassword = "test",
  106. BillingPlanCode = "UNLIMITED",
  107. AssignedUserName = "test"
  108. })).Wait();
  109. }
  110. return new ResultPlain();
  111. }
  112. protected async Task EnsureUser(UserService srvUsers, string userName, string userPassword, string userEmail, int prefferedLCID,
  113. RoleEnum[] roles)
  114. {
  115. if (!(srvUsers.ExistsUserByNameAsync(userName).Result.Value))
  116. await srvUsers.CreateUserAsync(userName,
  117. userPassword,
  118. userEmail,
  119. prefferedLCID, roles);
  120. else
  121. await srvUsers.EnusreUserRolesAsync(userName, roles);
  122. }
  123. }
  124. }