public HttpResponseMessage CreateUser(CreateUserContract contract) { try { var settings = new RegisterationDetails { PortalSettings = PortalSettings, Email = contract.Email, FirstName = contract.FirstName, LastName = contract.LastName, UserName = contract.UserName, Password = contract.Password, Question = contract.Question, Answer = contract.Answer, Notify = contract.Notify, Authorize = contract.Authorize, RandomPassword = contract.RandomPassword, IgnoreRegistrationMode = true }; var userInfo = RegisterController.Instance.Register(settings); return(Request.CreateResponse(HttpStatusCode.OK, userInfo != null ? UserBasicDto.FromUserDetails(Components.UsersController.Instance.GetUserDetail(PortalId, userInfo.UserId)) : null)); } catch (Exception ex) { Logger.Error(ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
public override ConsoleResultModel Run() { var settings = new RegisterationDetails { PortalSettings = PortalSettings, Email = Email, FirstName = FirstName, LastName = LastName, UserName = Username, Password = Password, Notify = Notify, Authorize = Approved, RandomPassword = string.IsNullOrEmpty(Password), IgnoreRegistrationMode = true }; try { var userInfo = RegisterController.Instance.Register(settings); var lstResult = new List <UserModel> { new UserModel(UserController.Instance.GetUser(PortalId, userInfo.UserId)) }; return(new ConsoleResultModel(LocalizeString("UserCreated")) { Data = lstResult, Records = lstResult.Count }); } catch (Exception ex) { return(new ConsoleErrorResultModel(ex.Message)); } }
public bool insertInvitedUser(Users ob1,RegisterationDetails ob2,string hash) { int userid = returnUserId(); cmd = new SqlCommand("insertInvitedUser", con); SqlParameter sp_uid = new SqlParameter("@uid", userid); SqlParameter sp_pass = new SqlParameter("@pass", ob1.password); SqlParameter sp_email = new SqlParameter("@email", ob1.emailId); SqlParameter sp_ltl = new SqlParameter("@ltl", ob1.lastTimeLogin); SqlParameter sp_fname = new SqlParameter("@fname", ob2.firstName); SqlParameter sp_lname = new SqlParameter("@lname", ob2.lastName); SqlParameter sp_con = new SqlParameter("@con", ob2.contact); SqlParameter sp_dob = new SqlParameter("@dobb", ob2.dob); SqlParameter sp_doj = new SqlParameter("@dojj", ob2.doj); SqlParameter sp_status = new SqlParameter("@status", ob2.status); SqlParameter sp_sq = new SqlParameter("@secQuesId", ob2.secQuesId); SqlParameter sp_sa = new SqlParameter("@secAns", ob2.secAns); SqlParameter sp_hash = new SqlParameter("@hash",hash); cmd.Parameters.Add(sp_uid); cmd.Parameters.Add(sp_pass); cmd.Parameters.Add(sp_email); cmd.Parameters.Add(sp_ltl); cmd.Parameters.Add(sp_fname); cmd.Parameters.Add(sp_lname); cmd.Parameters.Add(sp_con); cmd.Parameters.Add(sp_dob); cmd.Parameters.Add(sp_doj); cmd.Parameters.Add(sp_status); cmd.Parameters.Add(sp_sq); cmd.Parameters.Add(sp_sa); cmd.Parameters.Add(sp_hash); cmd.CommandType = CommandType.StoredProcedure; int res = cmd.ExecuteNonQuery(); return true; }
//NOTE - While making modifications in this method, developer must refer to call tree in Register.ascx.cs. //Especially Validate and CreateUser methods. Register class inherits from UserModuleBase, which also contains bunch of logic. //This method can easily be modified to pass passowrd, display name, etc. //It is recommended to write unit tests. public UserBasicDto Register(RegisterationDetails registerationDetails) { var portalSettings = registerationDetails.PortalSettings; var username = registerationDetails.UserName; var email = registerationDetails.Email; Requires.NotNullOrEmpty("email", email); var disallowRegistration = !registerationDetails.IgnoreRegistrationMode && ((portalSettings.UserRegistration == (int)Globals.PortalRegistrationType.NoRegistration) || (portalSettings.UserRegistration == (int)Globals.PortalRegistrationType.PrivateRegistration)); if (disallowRegistration) { throw new Exception(Localization.GetString("RegistrationNotAllowed", Library.Constants.SharedResources)); } //initial creation of the new User object var newUser = new UserInfo { PortalID = portalSettings.PortalId, Email = email }; var cleanUsername = PortalSecurity.Instance.InputFilter(username, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoAngleBrackets | PortalSecurity.FilterFlag.NoMarkup); if (!cleanUsername.Equals(username)) { throw new ArgumentException(Localization.GetExceptionMessage("InvalidUserName", "The username specified is invalid.")); } var valid = UserController.Instance.IsValidUserName(username); if (!valid) { throw new ArgumentException(Localization.GetExceptionMessage("InvalidUserName", "The username specified is invalid.")); } //ensure this user doesn't exist if (!string.IsNullOrEmpty(username) && UserController.GetUserByName(portalSettings.PortalId, username) != null) { throw new Exception(Localization.GetString("RegistrationUsernameAlreadyPresent", Library.Constants.SharedResources)); } //set username as email if not specified newUser.Username = string.IsNullOrEmpty(username) ? email : username; if (!string.IsNullOrEmpty(registerationDetails.Password) && !registerationDetails.RandomPassword) { newUser.Membership.Password = registerationDetails.Password; } else { //Generate a random password for the user newUser.Membership.Password = UserController.GeneratePassword(); } newUser.Membership.PasswordConfirm = newUser.Membership.Password; //set other profile properties newUser.Profile.InitialiseProfile(portalSettings.PortalId); newUser.Profile.PreferredLocale = new Localization().CurrentUICulture; newUser.Profile.PreferredTimeZone = portalSettings.TimeZone; //derive display name from supplied firstname, lastname or from email if (!string.IsNullOrEmpty(registerationDetails.FirstName) && !string.IsNullOrEmpty(registerationDetails.LastName)) { newUser.DisplayName = registerationDetails.FirstName + " " + registerationDetails.LastName; newUser.FirstName = registerationDetails.FirstName; newUser.LastName = registerationDetails.LastName; } else { newUser.DisplayName = newUser.Email.Substring(0, newUser.Email.IndexOf("@", StringComparison.Ordinal)); } //read all the user account settings var settings = UserController.GetUserSettings(portalSettings.PortalId); //Verify Profanity filter if (this.GetBoolSetting(settings, "Registration_UseProfanityFilter")) { var portalSecurity = PortalSecurity.Instance; if (!portalSecurity.ValidateInput(newUser.Username, PortalSecurity.FilterFlag.NoProfanity) || !portalSecurity.ValidateInput(newUser.DisplayName, PortalSecurity.FilterFlag.NoProfanity)) { throw new Exception(Localization.GetString("RegistrationProfanityNotAllowed", Library.Constants.SharedResources)); } } //Email Address Validation var emailValidator = this.GetStringSetting(settings, "Security_EmailValidation"); if (!string.IsNullOrEmpty(emailValidator)) { var regExp = RegexUtils.GetCachedRegex(emailValidator, RegexOptions.IgnoreCase | RegexOptions.Multiline); var matches = regExp.Matches(newUser.Email); if (matches.Count == 0) { throw new Exception(Localization.GetString("RegistrationInvalidEmailUsed", Library.Constants.SharedResources)); } } //Excluded Terms Verification var excludeRegex = this.GetExcludeTermsRegex(settings); if (!string.IsNullOrEmpty(excludeRegex)) { var regExp = RegexUtils.GetCachedRegex(excludeRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline); var matches = regExp.Matches(newUser.Username); if (matches.Count > 0) { throw new Exception(Localization.GetString("RegistrationExcludedTermsUsed", Library.Constants.SharedResources)); } } //User Name Validation var userNameValidator = this.GetStringSetting(settings, "Security_UserNameValidation"); if (!string.IsNullOrEmpty(userNameValidator)) { var regExp = RegexUtils.GetCachedRegex(userNameValidator, RegexOptions.IgnoreCase | RegexOptions.Multiline); var matches = regExp.Matches(newUser.Username); if (matches.Count == 0) { throw new Exception(Localization.GetString("RegistrationInvalidUserNameUsed", Library.Constants.SharedResources)); } } //ensure unique username var user = UserController.GetUserByName(portalSettings.PortalId, newUser.Username); if (user != null) { if (this.GetBoolSetting(settings, "Registration_UseEmailAsUserName")) { throw new Exception(UserController.GetUserCreateStatus(UserCreateStatus.DuplicateEmail)); } var i = 1; string userName = null; while (user != null) { userName = newUser.Username + "0" + i.ToString(CultureInfo.InvariantCulture); user = UserController.GetUserByName(portalSettings.PortalId, userName); i++; } newUser.Username = userName; } //ensure unique display name if (this.GetBoolSetting(settings, "Registration_RequireUniqueDisplayName")) { user = UserController.Instance.GetUserByDisplayname(portalSettings.PortalId, newUser.DisplayName); if (user != null) { var i = 1; string displayName = null; while (user != null) { displayName = newUser.DisplayName + " 0" + i.ToString(CultureInfo.InvariantCulture); user = UserController.Instance.GetUserByDisplayname(portalSettings.PortalId, displayName); i++; } newUser.DisplayName = displayName; } } //Update display name format var displaynameFormat = this.GetStringSetting(settings, "Security_DisplayNameFormat"); if (!string.IsNullOrEmpty(displaynameFormat)) { newUser.UpdateDisplayName(displaynameFormat); } //membership is approved only for public registration newUser.Membership.Approved = (registerationDetails.IgnoreRegistrationMode || portalSettings.UserRegistration == (int)Globals.PortalRegistrationType.PublicRegistration) && registerationDetails.Authorize; newUser.Membership.PasswordQuestion = registerationDetails.Question; newUser.Membership.PasswordAnswer = registerationDetails.Answer; //final creation of user var createStatus = UserController.CreateUser(ref newUser, registerationDetails.Notify); //clear cache if (createStatus == UserCreateStatus.Success) { CachingProvider.Instance().Remove(string.Format(DataCache.PortalUserCountCacheKey, portalSettings.PortalId)); } if (createStatus != UserCreateStatus.Success) { throw new Exception(UserController.GetUserCreateStatus(createStatus)); } // if (registerationDetails.IgnoreRegistrationMode) // { // Mail.SendMail(newUser, MessageType.UserRegistrationPublic, portalSettings); // return UserBasicDto.FromUserInfo(newUser); // } //send notification to portal administrator of new user registration //check the receive notification setting first, but if register type is Private, we will always send the notification email. //because the user need administrators to do the approve action so that he can continue use the website. if (!registerationDetails.IgnoreRegistrationMode && (portalSettings.EnableRegisterNotification || portalSettings.UserRegistration == (int)Globals.PortalRegistrationType.PrivateRegistration)) { Mail.SendMail(newUser, MessageType.UserRegistrationAdmin, portalSettings); SendAdminNotification(newUser, portalSettings); } return(UserBasicDto.FromUserInfo(newUser)); }