ConfigurationService.cs 5.0 KB

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