public UserRegistrationResult RegisterUser(User user) { if (user == null) { throw new ArgumentException("当前用户为空"); } var result = new UserRegistrationResult(); if (IsRegistered(user.UserName)) { result.AddError("用户名已经存在"); return(result); } if (String.IsNullOrEmpty(user.Email)) { result.AddError("邮箱不能为空"); return(result); } //if (user.Email.IsValidEmail()) //{ // result.AddError("邮件格式错误"); // return result; //} //if (user.Password.IsNullOrEmpty()) //{ // result.AddError("密码不能为空"); // return result; //} InsertUser(user); return(result); }
/// <summary> /// Register user /// </summary> /// <param name="request">Request</param> /// <returns>Result</returns> public virtual UserRegistrationResult RegisterUser(UserRegistrationRequest request) { if (request == null) throw new ArgumentNullException("request"); if (request.User == null) throw new ArgumentException("Can't load current user"); var result = new UserRegistrationResult(); if (request.User.IsSearchEngineAccount()) { result.AddError("Search engine can't be registered"); return result; } if (request.User.IsBackgroundTaskAccount()) { result.AddError("Background task account can't be registered"); return result; } if (request.User.IsRegistered()) { result.AddError("Current user is already registered"); return result; } if (String.IsNullOrEmpty(request.Email)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailIsNotProvided")); return result; } if (!CommonHelper.IsValidEmail(request.Email)) { result.AddError(_localizationService.GetResource("Common.WrongEmail")); return result; } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.PasswordIsNotProvided")); return result; } if (_userSettings.UsernamesEnabled) { if (String.IsNullOrEmpty(request.Username)) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameIsNotProvided")); return result; } } //validate unique user if (_userService.GetUserByEmail(request.Email) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.EmailAlreadyExists")); return result; } if (_userSettings.UsernamesEnabled) { if (_userService.GetUserByUsername(request.Username) != null) { result.AddError(_localizationService.GetResource("Account.Register.Errors.UsernameAlreadyExists")); return result; } } //at this point request is valid request.User.Username = request.Username; request.User.Email = request.Email; request.User.PasswordFormat = request.PasswordFormat; switch (request.PasswordFormat) { case PasswordFormat.Clear: { request.User.Password = request.Password; } break; case PasswordFormat.Encrypted: { request.User.Password = _encryptionService.EncryptText(request.Password); } break; case PasswordFormat.Hashed: { string saltKey = _encryptionService.CreateSaltKey(5); request.User.PasswordSalt = saltKey; request.User.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey, _userSettings.HashedPasswordFormat); } break; default: break; } request.User.Active = request.IsApproved; //add to 'Registered' role var registeredRole = _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered); if (registeredRole == null) throw new CmsException("'Registered' role could not be loaded"); request.User.UserRoles.Add(registeredRole); //remove from 'Guests' role var guestRole = request.User.UserRoles.FirstOrDefault(cr => cr.SystemName == SystemUserRoleNames.Guests); if (guestRole != null) request.User.UserRoles.Remove(guestRole); _userService.UpdateUser(request.User); return result; }
public virtual UserRegistrationResult RegisterUser(UserRegistrationRequest request) { if (request == null) { throw new ArgumentNullException("request"); } var result = new UserRegistrationResult(); if (String.IsNullOrEmpty(request.Email)) { result.AddError("Email Address is missing!"); return(result); } if (!CommonHelper.IsValidEmail(request.Email)) { result.AddError("Email Address is not valid!"); return(result); } if (String.IsNullOrWhiteSpace(request.Password)) { result.AddError("Password is missing!"); return(result); } if (String.IsNullOrEmpty(request.Username)) { result.AddError("Username is missing!"); return(result); } //validate unique user if (GetUserByEmail(request.Email) != null) { result.AddError("Email Already Exists!"); return(result); } if (GetUserByUsername(request.Username) != null) { result.AddError("Username Already Exists!"); return(result); } //at this point request is valid request.User = new User(); request.User.UserName = request.Username; request.User.Email = request.Email; request.User.Password = request.Password; request.User.IsApproved = request.IsApproved; request.User.IsActive = request.IsActive; request.User.AddressLine1 = ""; request.User.AddressLine2 = ""; request.User.CityId = 0; request.User.CoverPictureId = 0; request.User.FirstName = ""; request.User.MiddleName = ""; request.User.ProfilePictureId = 0; request.User.SeoName = request.Username; request.User.UserGuid = Guid.NewGuid(); request.User.UserId = 1; request.User.CreatedOn = request.User.ModifiedOn = DateTime.Now; request.User.LastLoginDate = DateTime.Now; var registeredRole = _roleService.GetRoleByName("General"); if (registeredRole == null) { throw new Exception("'Registered' role could not be loaded"); } request.User.Roles.Add(registeredRole); Insert(request.User); result.User = request.User; return(result); }