/// <summary> /// Creates the user. /// It also populates the userAccountInfo table. /// </summary> /// <param name="model">The model.</param> /// <param name="membershipProviderName">Name of the membership provider.</param> /// <param name="isApproved">if set to <c>true</c> [is approved].</param> /// <returns> /// The username that the application assigns to the user. /// </returns> private string CreateUser(UserAddModel model, string membershipProviderName, bool isApproved = true) { var userId = Guid.NewGuid(); model.UserName = userId.ToString(); try { var password = Membership.GeneratePassword(12, 5); MembershipCreateStatus createStatus; Membership.Providers[membershipProviderName].CreateUser(model.UserName, password, model.Email, null, null, isApproved, userId, out createStatus); this.userAccountInfoRepository.CreateUserAccountInfo(userId); if (createStatus == MembershipCreateStatus.Success) { return(model.UserName); } } catch (Exception e) { var logInfo = new LogInformation { ClassName = "AccountManager", MethodName = "CreateUser", ArgumentsPassedIn = new string[] { JsonConvert.SerializeObject(model), membershipProviderName }, ErrorMessage = e.Message }; this.logger.Error(JsonConvert.SerializeObject(logInfo)); } return(null); }
/// <summary> /// Updates the user account. /// </summary> /// <param name="model">The model.</param> /// <param name="isApproved">if set to <c>true</c> [is approved].</param> /// <returns> /// An AccountTransactionStatus struct. /// </returns> public AccountTransactionStatus UpdateUserAccount(UserAddModel model, bool isApproved = true) { // Try to find user. AccountTransactionStatus status = new AccountTransactionStatus { HasUpdatedMembership = false, HasUpdatedStateAssignments = false, HasUpdatedRoles = false, HasUpdatedProfile = false }; var container = this.FindUser(model.UserName); if (container.IsEmpty) { var logInfo = new LogInformation { ClassName = "AccountManager", MethodName = "UpdateUserAccount", ArgumentsPassedIn = new string[] { JsonConvert.SerializeObject(model), isApproved.ToString() }, ErrorMessage = "User was not found or does not exist." }; this.logger.Error(JsonConvert.SerializeObject(logInfo)); } else { // perform updates status.HasUpdatedMembership = this.UpdateUser(container.User, model, container.Providers.Membership, isApproved); status.HasUpdatedProfile = this.UpdateUserProfile(model.UserName, model); status.HasUpdatedRoles = this.UpdateUserRoles(model.UserName, model.RoleSelections, container.Providers.Role); status.HasUpdatedStateAssignments = this.UpdateUserStateAssignments(model.UserName, model.CheckboxStateSelections, model.RadioSelectedState, container.Providers.Role); } return(status); }
/// <summary> /// Maps the profile to user add model. /// </summary> /// <param name="profile">The profile.</param> /// <param name="model">The model.</param> public static void MapProfileToUserAddModel(UserProfile profile, UserAddModel model) { model.FirstName = profile.FirstName; model.LastName = profile.LastName; model.AccountType = profile.AccountType; model.Title = profile.Title; model.Organization = profile.Organization; }
/// <summary> /// Maps the user add model to profile. /// </summary> /// <param name="model">The model.</param> /// <param name="profile">The profile.</param> public static void MapUserAddModelToProfile(UserAddModel model, UserProfile profile) { profile.FirstName = model.FirstName; profile.LastName = model.LastName; profile.AccountType = model.AccountType; profile.Title = model.Title; profile.Organization = model.Organization; }
/// <summary> /// Populates the user add model with account container. /// </summary> /// <param name="model">The model.</param> /// <param name="container">The container.</param> public static void PopulateUserAddModelWithAccountContainer(UserAddModel model, AccountContainer container) { // update user information model.UserName = container.User.UserName; // update profile information MapProfileToUserAddModel(container.Profile, model); // update roles model.RoleSelections.ForEach(x => x.IsChecked = container.Roles.Contains(x.RoleName)); // update state assignments var selectedStates = container.StateAssignments.Select(x => x.StateId); model.CheckboxStateSelections.ForEach(x => x.IsChecked = selectedStates.Contains(x.StateId)); }
/// <summary> /// Creates the user account. /// An account has up to four components: /// 1] Membership: credentials. /// 2] Profile. /// 3] Roles. /// 4] When applicable, state assignments. /// </summary> /// <param name="model">The model.</param> /// <param name="isApproved">if set to <c>true</c> [is approved].</param> /// <returns> /// An AccountTransactionStatus struct. /// </returns> public AccountTransactionStatus CreateUserAccount(UserAddModel model, bool isApproved = true) { var application = Application.EvalEngine; AccountTransactionStatus status = new AccountTransactionStatus { HasUpdatedMembership = false, HasUpdatedStateAssignments = false, HasUpdatedRoles = false, HasUpdatedProfile = false }; var providersNames = GetProviderNamesFor(application); var userName = this.CreateUser(model, providersNames.Membership, isApproved); if (userName != null) { status.HasUpdatedMembership = true; status.HasUpdatedProfile = this.UpdateUserProfile(userName, model); status.HasUpdatedRoles = this.UpdateUserRoles(userName, model.RoleSelections, providersNames.Role); status.HasUpdatedStateAssignments = this.UpdateUserStateAssignments(userName, model.CheckboxStateSelections, model.RadioSelectedState, providersNames.Role); } return(status); }
/// <summary> /// Updates the user. /// </summary> /// <param name="user">The user.</param> /// <param name="model">The model.</param> /// <param name="membershipProviderName">Name of the membership provider.</param> /// <param name="isApproved">if set to <c>true</c> [is approved].</param> /// <returns> /// <c>true</c> if the update was successful; <c>false</c>, otherwise. /// </returns> private bool UpdateUser(MembershipUser user, UserAddModel model, string membershipProviderName, bool isApproved = true) { try { user.IsApproved = isApproved; user.Email = model.Email; Membership.Providers[membershipProviderName].UpdateUser(user); return(true); } catch (Exception e) { var logInfo = new LogInformation { ClassName = "AccountManager", MethodName = "UpdateUser", ArgumentsPassedIn = new string[] { JsonConvert.SerializeObject(user), JsonConvert.SerializeObject(model), membershipProviderName }, ErrorMessage = e.Message }; this.logger.Error(JsonConvert.SerializeObject(logInfo)); return(false); } }
/// <summary> /// Creates the user profile. /// </summary> /// <param name="userName">Name of the user.</param> /// <param name="model">The model.</param> /// <returns>True if the user profile is created successfully; false otherwise.</returns> private bool UpdateUserProfile(string userName, UserAddModel model) { try { // add profile information var profile = UserProfile.GetUserProfile(userName); MapUserAddModelToProfile(model, profile); profile.Save(); return(true); } catch (Exception e) { var logInfo = new LogInformation { ClassName = "AccountManager", MethodName = "UpdateUserProfile", ArgumentsPassedIn = new string[] { userName, JsonConvert.SerializeObject(model) }, ErrorMessage = e.Message }; this.logger.Error(JsonConvert.SerializeObject(logInfo)); return(false); } }