public async Task CreateTenant(CreateTenantInputDto inputDto) { if ((await _tenantManager.FindByTenancyNameAsync(inputDto.TenancyName)).IsNotNull()) { throw new HozaruException(string.Format("Toko Id {0} tidak bisa digunakan. Silahkan gunakan Toko Id lain.", inputDto.TenancyName)); } var district = _districtRepository.Get(inputDto.DistrictId); Validate.Found(district, "Kecamatan"); var tenant = new Tenant(inputDto.TenancyName, inputDto.Name, inputDto.WhatsappNumber, inputDto.Address, inputDto.Phone, district); var defaultEdition = await _editionManager.FindByNameAsync(EditionManager.DefaultEditionName); if (defaultEdition != null) { tenant.EditionId = defaultEdition.Id; } var createTenantResult = await _tenantManager.CreateAsync(tenant); createTenantResult.CheckErrors(); await CurrentUnitOfWork.SaveChangesAsync(); //To get new tenant's id. CurrentUnitOfWork.DisableFilter(HozaruDataFilters.MayHaveTenant); CurrentUnitOfWork.DisableFilter(HozaruDataFilters.MustHaveTenant); CurrentUnitOfWork.EnableFilter(HozaruDataFilters.MayHaveTenant); using (CurrentUnitOfWork.SetFilterParameter(HozaruDataFilters.MayHaveTenant, HozaruDataFilters.Parameters.TenantId, tenant.Id)) { IList <Role> createdRoles = await _roleAppService.CreateDefaultRoles(tenant.Id); foreach (var role in createdRoles) { await _roleManager.GrantAllPermissionsAsync(role); } await CurrentUnitOfWork.SaveChangesAsync(); //Create admin user for the tenant var adminUser = User.CreateTenantAdminUser(tenant, inputDto.AdminEmailAddress, inputDto.Password, inputDto.FirstName, inputDto.LastName); var createUserResult = await _userManager.CreateAsync(adminUser); createUserResult.CheckErrors(); await CurrentUnitOfWork.SaveChangesAsync(); //To get admin user's id var adminRole = _roleManager.Roles.Single(r => r.Name == "Admin"); //Assign admin user to role! var addRoleResult = await _userManager.AddToRoleAsync(adminUser.Id, adminRole.Name); addRoleResult.CheckErrors(); //await _tenantConfigurationAppService.CreateNew(tenant); await CurrentUnitOfWork.SaveChangesAsync(); } _apiKeyService.CreateApiKey(tenant.Id); }
public void OnCreateUpdateApiKey_whenPassingApiKey_ShouldAddOrModifyApiKey() { mockIApiKeyDataStore.Setup(repo => repo.Get(_apiKey.Id)).Returns(_apiKey); mockIApiKeyDataStore.Setup(repo => repo.Upsert(_apiKey)).Verifiable(); var result = _IApiKeyService.CreateApiKey(_apiKey.Id, DataSource.WEB); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(bool)); Assert.IsTrue(result); Assert.IsNotNull(_IApiKeyService.GetApiKey(_apiKey.Id)); Assert.AreEqual(_apiKey.Token, _IApiKeyService.GetApiKey(_apiKey.Id).Token); }
public async Task <IActionResult> OnPostAsync() { try { if (ModelState.IsValid) { var user = await _userManager.GetUserAsync(HttpContext.User); var isSucceed = false; if (user != null) { DataSource source = DataSource.ASCII; var skipProfileIds = _config["SkipProfile:SenderId"]; if (skipProfileIds != null && skipProfileIds.Contains(user.SenderId)) { source = DataSource.GESMES; } isSucceed = _apiKeyService.CreateApiKey(user.SenderId, source); ApiKey = _apiKeyService.GetApiKey(user.SenderId); } if (!isSucceed) { ModelState.AddModelError("ApiKeyError", "Failed to generate a new key"); } } } catch (Exception e) { ModelState.AddModelError("ApiKeyError", "Failed to generate a new key"); return(Page()); } return(Page()); }
public async Task <IActionResult> CreateApiKey() { var newKey = await apiKeyService.CreateApiKey(); return(Ok(newKey)); }