ConfigurationService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }
  68. return new ResultPlain();
  69. }
  70. public async Task<ResultPlain> InstallDev()
  71. {
  72. using (var scope = _serviceProvider.CreateScope())
  73. {
  74. var srvCatalogues = scope.ServiceProvider.GetRequiredService<CatalogueService>();
  75. var srvUsers = scope.ServiceProvider.GetRequiredService<UserService>();
  76. var srvWorkspaces = scope.ServiceProvider.GetRequiredService<WorkspaceService>();
  77. var task0 = srvCatalogues.CreateBillingPlanAsync(new BillingPlanCDto()
  78. {
  79. Name = "Unlimited",
  80. Code = CS_BP_DEV_CODE,
  81. Description = "This plan is only for testing purpose",
  82. PstinitialCredits = 1000,
  83. PstcreditsPerItem = 1,
  84. Pstwatermark = true,
  85. Pstquality = 1,
  86. PstprocessProfile = "PRC01",
  87. Psttest = true,
  88. Price = 1,
  89. Currency = "EUR",
  90. IsCustom = false,
  91. IsHidden = false,
  92. IsEnabled = true
  93. }, true);
  94. var task1 = srvCatalogues.CreateMimeTypeAsync(new MimeTypeCDto()
  95. {
  96. Mimetype1 = "text/plain",
  97. Description = "Plain text file",
  98. Extensions = "txt",
  99. IsEnabled = true
  100. }, true);
  101. var task2 = srvCatalogues.CreateMimeTypeAsync(new MimeTypeCDto()
  102. {
  103. Mimetype1 = "application/octet-stream",
  104. Description = "Binary files",
  105. Extensions = "bin",
  106. IsEnabled = true
  107. }, true);
  108. var task3 = EnsureUser(srvUsers, CS_USR_DEV_NAME, CS_USR_DEV_PWD, CS_USR_DEV_EML, 1029,
  109. new[] { RoleEnum.User, RoleEnum.Admin });
  110. Task.WhenAll(task0, task1, task2, task3)
  111. .ContinueWith(async (t) =>
  112. {
  113. if (await srvWorkspaces.ExistsWorkspace(WorkspaceIdentificationTypeEnum.Name, CS_WRK_DEV_NAME))
  114. return await srvWorkspaces.GetWorkspace(WorkspaceIdentificationTypeEnum.Name, CS_WRK_DEV_NAME);
  115. return await srvWorkspaces.CreateWorkspaceAsync(new WorkspaceCDto()
  116. {
  117. Name = CS_WRK_DEV_NAME,
  118. Apipassword = CS_WRK_DEV_APIPWD,
  119. BillingPlanCode = CS_BP_DEV_CODE,
  120. AssignedUserName = CS_USR_DEV_NAME
  121. });
  122. }).Wait();
  123. }
  124. return new ResultPlain();
  125. }
  126. public void LogInfo(string message)
  127. {
  128. Log.LogInformation(message);
  129. }
  130. protected async Task EnsureUser(UserService srvUsers, string userName, string userPassword, string userEmail, int prefferedLCID,
  131. RoleEnum[] roles)
  132. {
  133. if (!(srvUsers.ExistsUserByNameAsync(userName).Result.Value))
  134. await srvUsers.CreateUserAsync(userName,
  135. userPassword,
  136. userEmail,
  137. prefferedLCID, roles);
  138. else
  139. await srvUsers.EnusreUserRolesAsync(userName, roles);
  140. }
  141. }
  142. }