ConfigurationService.cs 6.8 KB

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