public async Task <RegistrationResultModel> RegisterAsync(RegistrationRequestDto model) { var adminId = Guid.NewGuid().ToString(); model.Email = model.Email.ToLower(); CredentialsCreateResponse adminCredentialsCreationResult; try { adminCredentialsCreationResult = await _credentialsClient.Admins.CreateAsync( new AdminCredentialsCreateRequest { Login = model.Email, AdminId = adminId, Password = model.Password }); } catch (ClientApiException exception) when(exception.HttpStatusCode == HttpStatusCode.BadRequest) { return(new RegistrationResultModel { Error = ServicesError.InvalidEmailOrPasswordFormat }); } if (adminCredentialsCreationResult.Error == CredentialsError.LoginAlreadyExists) { return new RegistrationResultModel { Error = ServicesError.AlreadyRegistered } } ; var emailHash = GetHash(model.Email); if (adminCredentialsCreationResult.Error != CredentialsError.None) { const string errorMessage = "An error occurred while creating admin credentials."; _log.Error(errorMessage, context: $"adminUserId: {adminId}; email: {emailHash}; error: {adminCredentialsCreationResult.Error}"); throw new InvalidOperationException(errorMessage); } var registrationDateTime = DateTime.UtcNow; var result = await _adminUsersRepository.TryCreateAsync( new AdminUserEncrypted { AdminUserId = adminId, EmailHash = emailHash, RegisteredAt = registrationDateTime, IsActive = true, UseDefaultPermissions = false }); if (result) { _log.Info("Admin user created for an account.", context: $"adminUserId: {adminId}; email: {emailHash}"); } else { _log.Warning("Trying to create a duplicate admin user.", context: $"email: {emailHash}"); return(new RegistrationResultModel { Error = ServicesError.AlreadyRegistered }); } var adminProfileCreationResult = await _customerProfileClient.AdminProfiles.AddAsync( _mapper.Map <RegistrationRequestDto, AdminProfileRequest>(model, opt => opt.AfterMap((src, dest) => { dest.AdminId = Guid.Parse(adminId); })) ); await _permissionsService.CreateOrUpdatePermissionsAsync(adminId, model.Permissions); await _permissionsCache.SetAsync(adminId, model.Permissions.ToList()); if (adminProfileCreationResult.ErrorCode != AdminProfileErrorCodes.None) { _log.Error(message: "An error occurred while creating admin profile.", context: $"adminUserId: {adminId}; error: {adminProfileCreationResult.ErrorCode}"); } #region email verification code var emailVerificationCode = Guid.NewGuid().ToString(); var emailVerificationCodeEntity = await _emailVerificationCodeRepository.CreateOrUpdateAsync( adminId, emailVerificationCode); #endregion await _notificationsService.NotifyAdminCreatedAsync(new AdminCreatedEmailDto { AdminUserId = adminId, Email = model.Email, EmailVerificationCode = emailVerificationCode.ToBase64(), Password = model.Password, Name = $"{model.FirstName} {model.LastName}", Localization = model.Localization }); _log.Info(message: "Successfully generated AdminCreatedEmail", context: adminId); var adminUser = _mapper.Map <AdminUser>(adminProfileCreationResult.Data); adminUser.AdminUserId = adminId; adminUser.Permissions = model.Permissions.ToList(); adminUser.RegisteredAt = registrationDateTime; adminUser.UseDefaultPermissions = false; adminUser.IsActive = true; return(new RegistrationResultModel { Admin = adminUser }); }
public async Task <RegistrationResultModel> RegisterAsync( string email, string password, string firstName, string lastName, string phoneNumber, string company, string department, string jobTitle, IReadOnlyList <Permission> permissions) { var adminId = Guid.NewGuid().ToString(); email = email.ToLower(); CredentialsCreateResponse adminCredentialsCreationResult; try { adminCredentialsCreationResult = await _credentialsClient.Admins.CreateAsync( new AdminCredentialsCreateRequest { Login = email, AdminId = adminId, Password = password }); } catch (ClientApiException exception) when(exception.HttpStatusCode == HttpStatusCode.BadRequest) { return(new RegistrationResultModel { Error = ServicesError.InvalidEmailOrPasswordFormat }); } if (adminCredentialsCreationResult.Error == CredentialsError.LoginAlreadyExists) { return new RegistrationResultModel { Error = ServicesError.AlreadyRegistered } } ; var emailHash = GetHash(email); if (adminCredentialsCreationResult.Error != CredentialsError.None) { const string errorMessage = "An error occurred while creating admin credentials."; _log.Error(errorMessage, context: $"adminUserId: {adminId}; email: {emailHash}; error: {adminCredentialsCreationResult.Error}"); throw new InvalidOperationException(errorMessage); } var registrationDateTime = DateTime.UtcNow; var result = await _adminUsersRepository.TryCreateAsync( new AdminUserEncrypted { AdminUserId = adminId, EmailHash = emailHash, RegisteredAt = registrationDateTime, IsActive = true, UseDefaultPermissions = false }); if (result) { _log.Info("Admin user created for an account.", context: $"adminUserId: {adminId}; email: {emailHash}"); } else { _log.Warning("Trying to create a duplicate admin user.", context: $"email: {emailHash}"); return(new RegistrationResultModel { Error = ServicesError.AlreadyRegistered }); } var adminProfileCreationResult = await _customerProfileClient.AdminProfiles.AddAsync( new AdminProfileRequest { AdminId = Guid.Parse(adminId), Email = email, FirstName = firstName, LastName = lastName, Company = company, Department = department, JobTitle = jobTitle, PhoneNumber = phoneNumber }); await _permissionsService.CreateOrUpdatePermissionsAsync(adminId, permissions); await _permissionsCache.SetAsync(adminId, permissions.ToList()); if (adminProfileCreationResult.ErrorCode != AdminProfileErrorCodes.None) { _log.Error(message: "An error occurred while creating admin profile.", context: $"adminUserId: {adminId}; error: {adminProfileCreationResult.ErrorCode}"); } var fullName = $"{firstName} {lastName}"; await _notificationsService.NotifyAdminCreatedAsync(adminId, email, email, password, fullName); return(new RegistrationResultModel { Admin = new AdminUser { AdminUserId = adminId, Company = adminProfileCreationResult.Data.Company, Department = adminProfileCreationResult.Data.Department, Email = adminProfileCreationResult.Data.Email, FirstName = adminProfileCreationResult.Data.FirstName, LastName = adminProfileCreationResult.Data.LastName, JobTitle = adminProfileCreationResult.Data.JobTitle, PhoneNumber = adminProfileCreationResult.Data.PhoneNumber, Permissions = permissions.ToList(), RegisteredAt = registrationDateTime, UseDefaultPermissions = false, IsActive = true } }); }