public AccountRegistrationResult RegisterAccount(AccountRegistrationRequest request) { if (request == null) throw new ArgumentNullException("request"); if (request.Account == null) throw new ArgumentException("Can't load current account"); var result = new AccountRegistrationResult(); if (request.Account.IsRegistered()) { result.AddError("Current account is already registered"); return result; } if (String.IsNullOrEmpty(request.Email)) { result.AddError("Email Is Not Provided"); return result; } if (!CommonHelper.IsValidEmail(request.Email)) { result.AddError("Common.WrongEmail"); return result; } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError("Account.Register.Errors.PasswordIsNotProvided"); return result; } //validate unique user if (_accountService.GetAccountByEmail(request.Email) != null) { result.AddError("Account.Register.Errors.EmailAlreadyExists"); return result; } //at this point request is valid request.Account.Email = request.Email; request.Account.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.Account.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.Account.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.Account.PasswordSalt = saltKey; request.Account.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey); } break; default: break; } request.Account.Active = true; //add to 'Registered' role var registeredRole = _accountService.GetAccountRoleBySystemName(SystemAccountRoleNames.Registered); if (registeredRole == null) throw new Exception("'Registered' role could not be loaded"); request.Account.AccountRoles.Add(registeredRole); //remove from 'Guests' role var guestRole = request.Account.AccountRoles.FirstOrDefault(cr => cr.SystemName == SystemAccountRoleNames.Guests); if (guestRole != null) request.Account.AccountRoles.Remove(guestRole); _accountService.UpdateAccount(request.Account); return result; }
public AccountRegistrationResult RegisterAccount(AccountRegistrationRequest request) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Account == null) { throw new ArgumentException("Can't load current account"); } var result = new AccountRegistrationResult(); if (request.Account.IsRegistered()) { result.AddError("Current account is already registered"); return(result); } if (String.IsNullOrEmpty(request.Email)) { result.AddError("Email Is Not Provided"); return(result); } if (!CommonHelper.IsValidEmail(request.Email)) { result.AddError("Common.WrongEmail"); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError("Account.Register.Errors.PasswordIsNotProvided"); return(result); } //validate unique user if (_accountService.GetAccountByEmail(request.Email) != null) { result.AddError("Account.Register.Errors.EmailAlreadyExists"); return(result); } //at this point request is valid request.Account.Email = request.Email; request.Account.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.Account.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.Account.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.Account.PasswordSalt = saltKey; request.Account.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey); } break; default: break; } request.Account.Active = true; //add to 'Registered' role var registeredRole = _accountService.GetAccountRoleBySystemName(SystemAccountRoleNames.Registered); if (registeredRole == null) { throw new Exception("'Registered' role could not be loaded"); } request.Account.AccountRoles.Add(registeredRole); //remove from 'Guests' role var guestRole = request.Account.AccountRoles.FirstOrDefault(cr => cr.SystemName == SystemAccountRoleNames.Guests); if (guestRole != null) { request.Account.AccountRoles.Remove(guestRole); } _accountService.UpdateAccount(request.Account); return(result); }