public async Task <User> CreateAsync(User user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (user.Id > 0) { throw new ArgumentOutOfRangeException(nameof(user.Id)); } // Create alias user.Alias = _aliasCreator.Create(user.DisplayName); // transform meta data user.Data = await SerializeMetaDataAsync(user); var newUser = await _userRepository.InsertUpdateAsync(user); if (newUser != null) { // Ensure new users have an API key, update this after adding the user // so we can append the newly generated unique userId to the guid if (String.IsNullOrEmpty(newUser.ApiKey)) { newUser = await UpdateAsync(newUser); } CancelTokens(user); } return(newUser); }
async Task <string> ParseAlias(string input) { foreach (var handler in _broker.Pub <string>(this, "ParseCategoryAlias")) { return(await handler.Invoke(new Message <string>(input, this))); } // No subscription found, use default alias creator return(_aliasCreator.Create(input)); }
private async Task <string> ParseAlias(string input) { var handled = false; foreach (var handler in _broker.Pub <string>(this, "ParseLabelAlias")) { handled = true; input = await handler.Invoke(new Message <string>(input, this)); } // No subscription found, use default alias creator return(handled ? input : _aliasCreator.Create(input)); }
public async Task <ICommandResult <Models.SignUp> > CreateAsync(Models.SignUp model) { var result = new CommandResult <Models.SignUp>(); // Validate if (model.Id > 0) { return(result.Failed(new CommandError($"{nameof(model.Id)} cannot be greater than zero when creating a sign-up"))); } if (string.IsNullOrEmpty(model.Email)) { return(result.Failed("An email address is required!")); } // Do we have an email if (!string.IsNullOrEmpty(model.Email)) { // Validate email var validationResult = await _signUpValidator.ValidateEmailAsync(model.Email); if (!validationResult.Succeeded) { return(result.Failed(validationResult.Errors.ToArray())); } } // Do we have a company name? if (!string.IsNullOrEmpty(model.CompanyName)) { // Validate company name var validationResult = await _signUpValidator.ValidateCompanyNameAsync(model.CompanyName); if (!validationResult.Succeeded) { return(result.Failed(validationResult.Errors.ToArray())); } // Create company name alias model.CompanyNameAlias = _aliasCreator.Create(model.CompanyName); } // Create security token if (string.IsNullOrEmpty(model.SecurityToken)) { // Build simple security token model.SecurityToken = _keyGenerator.GenerateKey(o => { o.OnlyDigits = true; o.MaxLength = 6; }); } // Invoke SignUpCreating subscriptions foreach (var handler in _broker.Pub <Models.SignUp>(this, "SignUpCreating")) { model = await handler.Invoke(new Message <Models.SignUp>(model, this)); } var signUp = await _signUpStore.CreateAsync(model); if (signUp != null) { // Set unique session id, using primary key to ensure uniqueness if (string.IsNullOrEmpty(signUp.SessionId)) { // Set sessionId signUp.SessionId = _keyGenerator.GenerateKey(o => { o.OnlyDigits = false; o.UniqueIdentifier = signUp.Id.ToString(); o.MaxLength = 150; }); // Persist sessionId var updatedSignUp = await _signUpStore.UpdateAsync(signUp); if (updatedSignUp != null) { // Invoke SignUpCreated subscriptions foreach (var handler in _broker.Pub <Models.SignUp>(this, "SignUpCreated")) { updatedSignUp = await handler.Invoke(new Message <Models.SignUp>(updatedSignUp, this)); } // Return success return(result.Success(updatedSignUp)); } } } return(result.Failed(new CommandError("An unknown error occurred whilst attempting to create a sign-up"))); }
Task <string> ParseEntityAlias(string input) { return(Task.FromResult(_aliasCreator.Create(input))); }
public async Task <ICommandResultBase> ValidateCompanyNameAsync(string companyName) { var result = new CommandResultBase(); var error = "The company name is invalid or is already in use. Please try a different company name."; // We always need a company name if (string.IsNullOrEmpty(companyName)) { return(result.Failed("A company name is required!")); } // Validate characters var valid = true; foreach (var c in companyName) { // Not a letter, digit or space if (!c.IsLetterDigitOrSpace()) { valid = false; break; } } // Only allow letters digits and spaces if (valid == false) { return(result.Failed("The company name cannot contain special characters. Only letters, numbers and spaces are allowed. Please try a different company name.")); } // Create the company name alias to compare with our blacklist var companyNameAlias = _aliasCrator.Create(companyName); // We need an alias if (string.IsNullOrEmpty(companyNameAlias)) { return(result.Failed(error)); } // Does the company name appear in the blacklist? var blackList = GetBlackListedCompanyNames(); foreach (var item in blackList) { if (item.Equals(companyName, StringComparison.OrdinalIgnoreCase)) { return(result.Failed(error)); } if (item.Equals(companyNameAlias, StringComparison.OrdinalIgnoreCase)) { return(result.Failed(error)); } } // --------------- // Does the company name already exist? // We need company names to be unique as the // tenant RequestedPrefixUrl uses CompanyNameAlias // --------------- var signUps = await _signUpStore.QueryAsync() .Select <SignUpQueryParams>(q => { q.CompanyName.Or().Equals(companyName); q.CompanyNameAlias.Or().Equals(companyName); }) .ToList(); if (signUps?.Data != null) { return(result.Failed(error)); } return(result.Success()); }