public async Task ThrowIfUserExists(string email) { logger.ScopeTrace($"Check if user exists '{email}', Route '{RouteBinding.Route}'."); ValidateEmail(email); if (await tenantRepository.ExistsAsync <User>(await User.IdFormat(new User.IdKey { TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName, Email = email }))) { throw new UserExistsException($"User '{email}' already exists."); } }
public async Task <User> CreateUser(string email, string password, bool changePassword = false, List <Claim> claims = null, string tenantName = null, string trackName = null, bool checkUserAndPasswordPolicy = true, bool confirmAccount = true, bool emailVerified = false, bool disableAccount = false) { logger.ScopeTrace($"Creating user '{email}', Route '{RouteBinding?.Route}'."); email = email?.ToLower(); ValidateEmail(email); var user = new User { UserId = Guid.NewGuid().ToString(), ConfirmAccount = confirmAccount, EmailVerified = emailVerified, DisableAccount = disableAccount }; var userIdKey = new User.IdKey { TenantName = tenantName ?? RouteBinding.TenantName, TrackName = trackName ?? RouteBinding.TrackName, Email = email?.ToLower() }; await user.SetIdAsync(userIdKey); await secretHashLogic.AddSecretHashAsync(user, password); if (claims?.Count() > 0) { user.Claims = claims.ToClaimAndValues(); } if (checkUserAndPasswordPolicy) { if (await tenantRepository.ExistsAsync <User>(await User.IdFormat(userIdKey))) { throw new UserExistsException($"User '{email}' already exists."); } await ValidatePasswordPolicy(email, password); } user.ChangePassword = changePassword; await tenantRepository.CreateAsync(user); logger.ScopeTrace($"User '{email}' created, with user id '{user.UserId}'."); return(user); }