private static void CreateYAFUser(string sUsername, string sPassword, string email) { if (!UserMembershipHelper.UserExists(sUsername, email)) { YafMembershipProvider mb = (YafMembershipProvider)System.Web.Security.Membership.Providers["YafMembershipProvider"]; int?forumUserID = 0; if (!mb.ValidateUser(sUsername, sPassword)) { MembershipCreateStatus status; MembershipUser forumUser = mb.CreateUser(sUsername, sPassword, email, "question", "answer", true, null, out status); // create the user in the YAF DB as well as sync roles... forumUserID = RoleMembershipHelper.CreateForumUser(forumUser, 1); RoleMembershipHelper.SetupUserRoles(1, sUsername); RoleMembershipHelper.AddUserToRole(sUsername, "Registered"); // create empty profile just so they have one YafUserProfile userProfile = YafUserProfile.GetProfile(sUsername); userProfile.Homepage = "fwd.com"; // setup their inital profile information userProfile.Save(); } else { return; } } }
/// <summary> /// Handles click on save button. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> protected void Save_Click([NotNull] object sender, [NotNull] EventArgs e) { // go through all roles displayed on page for (var i = 0; i < this.UserGroups.Items.Count; i++) { // get current item var item = this.UserGroups.Items[i]; // get role ID from it var roleID = int.Parse(((Label)item.FindControl("GroupID")).Text); // get role name var roleName = this.GetRepository <Group>().List(boardId: this.PageContext.PageBoardID, groupId: roleID) .FirstOrDefault().Name; // is user supposed to be in that role? var isChecked = ((CheckBox)item.FindControl("GroupMember")).Checked; // save user in role this.Get <IDbFunction>().Query.usergroup_save(this.CurrentUserID, roleID, isChecked); // empty out access table(s) this.GetRepository <Active>().DeleteAll(); this.GetRepository <ActiveAccess>().DeleteAll(); // update roles if this user isn't the guest if (UserMembershipHelper.IsGuestUser(this.CurrentUserID)) { continue; } // get user's name var userName = UserMembershipHelper.GetUserNameFromID(this.CurrentUserID); // add/remove user from roles in membership provider if (isChecked && !RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.AddUserToRole(userName, roleName); } else if (!isChecked && RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.RemoveUserFromRole(userName, roleName); } // Clearing cache with old permisssions data... this.Get <IDataCache>().Remove(Constants.Cache.ActiveUserLazyData.FormatWith(this.CurrentUserID)); } // update forum moderators cache just in case something was changed... this.Get <IDataCache>().Remove(Constants.Cache.ForumModerators); // clear the cache for this user... this.Get <IRaiseEvent>().Raise(new UpdateUserEvent(this.CurrentUserID)); this.BindData(); }
/// <summary> /// Handles click on save button. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> protected void Save_Click(object sender, EventArgs e) { // go through all roles displayed on page for (int i = 0; i < this.UserGroups.Items.Count; i++) { // get current item RepeaterItem item = this.UserGroups.Items[i]; // get role ID from it int roleID = int.Parse(((Label)item.FindControl("GroupID")).Text); // get role name string roleName = string.Empty; using (DataTable dt = DB.group_list(this.PageContext.PageBoardID, roleID)) { foreach (DataRow row in dt.Rows) { roleName = (string)row["Name"]; } } // is user supposed to be in that role? bool isChecked = ((CheckBox)item.FindControl("GroupMember")).Checked; // save user in role DB.usergroup_save(this.CurrentUserID, roleID, isChecked); // update roles if this user isn't the guest if (!UserMembershipHelper.IsGuestUser(this.CurrentUserID)) { // get user's name string userName = UserMembershipHelper.GetUserNameFromID(this.CurrentUserID); // add/remove user from roles in membership provider if (isChecked && !RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.AddUserToRole(userName, roleName); } else if (!isChecked && RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.RemoveUserFromRole(userName, roleName); } // Clearing cache with old permisssions data... this.PageContext.Cache.Remove(YafCache.GetBoardCacheKey(Constants.Cache.ActiveUserLazyData.FormatWith(this.CurrentUserID))); } } // update forum moderators cache just in case something was changed... this.PageContext.Cache.Remove(YafCache.GetBoardCacheKey(Constants.Cache.ForumModerators)); // clear the cache for this user... UserMembershipHelper.ClearCacheForUserId(this.CurrentUserID); this.BindData(); }
/// <summary> /// Updates the user <paramref name="role" />. /// </summary> /// <param name="role">The <paramref name="role" />.</param> /// <param name="yafUserId">The YAF user id.</param> /// <param name="userName">Name of the user.</param> /// <param name="addRole">if set to true [add role].</param> public static void UpdateUserRole(RoleInfo role, int yafUserId, string userName, bool addRole) { // save user in role LegacyDb.usergroup_save(yafUserId, role.RoleID, addRole); if (addRole && !RoleMembershipHelper.IsUserInRole(userName, role.RoleName)) { RoleMembershipHelper.AddUserToRole(userName, role.RoleName); } else if (!addRole && RoleMembershipHelper.IsUserInRole(userName, role.RoleName)) { RoleMembershipHelper.RemoveUserFromRole(userName, role.RoleName); } }
/// <summary> /// Updates the user <paramref name="role" />. /// </summary> /// <param name="role">The <paramref name="role" />.</param> /// <param name="yafUserId">The YAF user id.</param> /// <param name="userName">Name of the user.</param> /// <param name="addRole">if set to true [add role].</param> public static void UpdateUserRole(RoleInfo role, int yafUserId, string userName, bool addRole) { // save user in role BoardContext.Current.GetRepository <UserGroup>().Save(yafUserId, role.RoleID, addRole); if (addRole && !RoleMembershipHelper.IsUserInRole(userName, role.RoleName)) { RoleMembershipHelper.AddUserToRole(userName, role.RoleName); } else if (!addRole && RoleMembershipHelper.IsUserInRole(userName, role.RoleName)) { RoleMembershipHelper.RemoveUserFromRole(userName, role.RoleName); } }
/// <summary> /// Creates the forum. /// </summary> /// <returns> /// The create forum. /// </returns> private bool CreateForum() { if (this.InstallUpgradeService.IsForumInstalled) { this.ShowErrorMessage("Forum is already installed."); return(false); } if (this.TheForumName.Text.Length == 0) { this.ShowErrorMessage("You must enter a forum name."); return(false); } if (this.ForumEmailAddress.Text.Length == 0) { this.ShowErrorMessage("You must enter a forum email address."); return(false); } MembershipUser user; if (this.UserChoice.SelectedValue == "create") { if (this.UserName.Text.Length == 0) { this.ShowErrorMessage("You must enter the admin user name,"); return(false); } if (this.AdminEmail.Text.Length == 0) { this.ShowErrorMessage("You must enter the administrators email address."); return(false); } if (this.Password1.Text.Length == 0) { this.ShowErrorMessage("You must enter a password."); return(false); } if (this.Password1.Text != this.Password2.Text) { this.ShowErrorMessage("The passwords must match."); return(false); } // create the admin user... MembershipCreateStatus status; user = this.Get <MembershipProvider>() .CreateUser( this.UserName.Text, this.Password1.Text, this.AdminEmail.Text, this.SecurityQuestion.Text, this.SecurityAnswer.Text, true, null, out status); if (status != MembershipCreateStatus.Success) { this.ShowErrorMessage( "Create Admin User Failed: {0}".FormatWith(this.GetMembershipErrorMessage(status))); return(false); } } else { // try to get data for the existing user... user = UserMembershipHelper.GetUser(this.ExistingUserName.Text.Trim()); if (user == null) { this.ShowErrorMessage( "Existing user name is invalid and does not represent a current user in the membership store."); return(false); } } try { var prefix = Config.CreateDistinctRoles && Config.IsAnyPortal ? "YAF " : string.Empty; // add administrators and registered if they don't already exist... if (!RoleMembershipHelper.RoleExists("{0}Administrators".FormatWith(prefix))) { RoleMembershipHelper.CreateRole("{0}Administrators".FormatWith(prefix)); } if (!RoleMembershipHelper.RoleExists("{0}Registered".FormatWith(prefix))) { RoleMembershipHelper.CreateRole("{0}Registered".FormatWith(prefix)); } if (!RoleMembershipHelper.IsUserInRole(user.UserName, "{0}Administrators".FormatWith(prefix))) { RoleMembershipHelper.AddUserToRole(user.UserName, "{0}Administrators".FormatWith(prefix)); } // logout administrator... FormsAuthentication.SignOut(); // init forum... this.InstallUpgradeService.InitializeForum( this.TheForumName.Text, this.TimeZones.SelectedValue, this.Culture.SelectedValue, this.ForumEmailAddress.Text, this.ForumBaseUrlMask.Text, user.UserName, user.Email, user.ProviderUserKey); } catch (Exception x) { this.ShowErrorMessage(x.Message); return(false); } return(true); }
/// <summary> /// The create board. /// </summary> /// <param name="adminName">The admin name.</param> /// <param name="adminPassword">The admin password.</param> /// <param name="adminEmail">The admin email.</param> /// <param name="adminPasswordQuestion">The admin password question.</param> /// <param name="adminPasswordAnswer">The admin password answer.</param> /// <param name="boardName">The board name.</param> /// <param name="boardMembershipAppName">The board membership app name.</param> /// <param name="boardRolesAppName">The board roles app name.</param> /// <param name="createUserAndRoles">The create user and roles.</param> protected bool CreateBoard( [NotNull] string adminName, [NotNull] string adminPassword, [NotNull] string adminEmail, [NotNull] string adminPasswordQuestion, [NotNull] string adminPasswordAnswer, [NotNull] string boardName, [NotNull] string boardMembershipAppName, [NotNull] string boardRolesAppName, bool createUserAndRoles) { // Store current App Names string currentMembershipAppName = this.Get <MembershipProvider>().ApplicationName; string currentRolesAppName = this.Get <RoleProvider>().ApplicationName; if (boardMembershipAppName.IsSet() && boardRolesAppName.IsSet()) { // Change App Names for new board this.Get <MembershipProvider>().ApplicationName = boardMembershipAppName; this.Get <MembershipProvider>().ApplicationName = boardRolesAppName; } int newBoardID; DataTable cult = StaticDataHelper.Cultures(); string langFile = "english.xml"; foreach (DataRow drow in cult.Rows.Cast <DataRow>().Where(drow => drow["CultureTag"].ToString() == this.Culture.SelectedValue)) { langFile = (string)drow["CultureFile"]; } if (createUserAndRoles) { // Create new admin users MembershipCreateStatus createStatus; MembershipUser newAdmin = this.Get <MembershipProvider>() .CreateUser( adminName, adminPassword, adminEmail, adminPasswordQuestion, adminPasswordAnswer, true, null, out createStatus); if (createStatus != MembershipCreateStatus.Success) { this.PageContext.AddLoadMessage( "Create User Failed: {0}".FormatWith(this.GetMembershipErrorMessage(createStatus)), MessageTypes.Error); return(false); } // Create groups required for the new board RoleMembershipHelper.CreateRole("Administrators"); RoleMembershipHelper.CreateRole("Registered"); // Add new admin users to group RoleMembershipHelper.AddUserToRole(newAdmin.UserName, "Administrators"); // Create Board newBoardID = this.DbCreateBoard( boardName, boardMembershipAppName, boardRolesAppName, langFile, newAdmin); } else { // new admin MembershipUser newAdmin = UserMembershipHelper.GetUser(); // Create Board newBoardID = this.DbCreateBoard( boardName, boardMembershipAppName, boardRolesAppName, langFile, newAdmin); } if (newBoardID > 0 && Config.MultiBoardFolders) { // Successfully created the new board string boardFolder = this.Server.MapPath(Path.Combine(Config.BoardRoot, "{0}/".FormatWith(newBoardID))); // Create New Folders. if (!Directory.Exists(Path.Combine(boardFolder, "Images"))) { // Create the Images Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images")); // Create Sub Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Avatars")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Categories")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Forums")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Emoticons")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Medals")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Ranks")); } if (!Directory.Exists(Path.Combine(boardFolder, "Themes"))) { Directory.CreateDirectory(Path.Combine(boardFolder, "Themes")); // Need to copy default theme to the Themes Folder } if (!Directory.Exists(Path.Combine(boardFolder, "Uploads"))) { Directory.CreateDirectory(Path.Combine(boardFolder, "Uploads")); } } // Return application name to as they were before. this.Get <MembershipProvider>().ApplicationName = currentMembershipAppName; this.Get <RoleProvider>().ApplicationName = currentRolesAppName; return(true); }
/// <summary> /// Handles click on save button. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The e. /// </param> protected void Save_Click([NotNull] object sender, [NotNull] EventArgs e) { var addedRoles = new List <string>(); var removedRoles = new List <string>(); // get user's name var userName = UserMembershipHelper.GetUserNameFromID(this.CurrentUserID); var user = UserMembershipHelper.GetUser(userName); // go through all roles displayed on page for (var i = 0; i < this.UserGroups.Items.Count; i++) { // get current item var item = this.UserGroups.Items[i]; // get role ID from it var roleID = int.Parse(item.FindControlAs <Label>("GroupID").Text); // get role name var roleName = this.GetRepository <Group>().GetById(roleID).Name; // is user supposed to be in that role? var isChecked = item.FindControlAs <CheckBox>("GroupMember").Checked; // save user in role this.GetRepository <UserGroup>().Save(this.CurrentUserID, roleID, isChecked); // empty out access table(s) this.GetRepository <Active>().DeleteAll(); this.GetRepository <ActiveAccess>().DeleteAll(); // update roles if this user isn't the guest if (UserMembershipHelper.IsGuestUser(this.CurrentUserID)) { continue; } // add/remove user from roles in membership provider if (isChecked && !RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.AddUserToRole(userName, roleName); addedRoles.Add(roleName); } else if (!isChecked && RoleMembershipHelper.IsUserInRole(userName, roleName)) { RoleMembershipHelper.RemoveUserFromRole(userName, roleName); removedRoles.Add(roleName); } // Clearing cache with old permissions data... this.Get <IDataCache>().Remove(string.Format(Constants.Cache.ActiveUserLazyData, this.CurrentUserID)); } if (this.SendEmail.Checked) { // send notification to user if (addedRoles.Any()) { this.Get <ISendNotification>().SendRoleAssignmentNotification(user, addedRoles); } if (removedRoles.Any()) { this.Get <ISendNotification>().SendRoleUnAssignmentNotification(user, removedRoles); } } // update forum moderators cache just in case something was changed... this.Get <IDataCache>().Remove(Constants.Cache.ForumModerators); // clear the cache for this user... this.Get <IRaiseEvent>().Raise(new UpdateUserEvent(this.CurrentUserID)); this.BindData(); }
/// <summary> /// The create board. /// </summary> /// <param name="adminName">The admin name.</param> /// <param name="adminPassword">The admin password.</param> /// <param name="adminEmail">The admin email.</param> /// <param name="adminPasswordQuestion">The admin password question.</param> /// <param name="adminPasswordAnswer">The admin password answer.</param> /// <param name="boardName">The board name.</param> /// <param name="boardMembershipAppName">The board membership app name.</param> /// <param name="boardRolesAppName">The board roles app name.</param> /// <param name="createUserAndRoles">The create user and roles.</param> /// <returns>Returns if the board was created or not</returns> protected bool CreateBoard( [NotNull] string adminName, [NotNull] string adminPassword, [NotNull] string adminEmail, [NotNull] string adminPasswordQuestion, [NotNull] string adminPasswordAnswer, [NotNull] string boardName, [NotNull] string boardMembershipAppName, [NotNull] string boardRolesAppName, bool createUserAndRoles) { // Store current App Names var currentMembershipAppName = this.Get <MembershipProvider>().ApplicationName; var currentRolesAppName = this.Get <RoleProvider>().ApplicationName; if (boardMembershipAppName.IsSet() && boardRolesAppName.IsSet()) { // Change App Names for new board this.Get <MembershipProvider>().ApplicationName = boardMembershipAppName; this.Get <MembershipProvider>().ApplicationName = boardRolesAppName; } int newBoardId; var cult = StaticDataHelper.Cultures(); var langFile = "english.xml"; cult.Where(dataRow => dataRow.CultureTag == this.Culture.SelectedValue) .ForEach(row => langFile = row.CultureFile); if (createUserAndRoles) { // Create new admin users var newAdmin = this.Get <MembershipProvider>() .CreateUser( adminName, adminPassword, adminEmail, adminPasswordQuestion, adminPasswordAnswer, true, null, out var createStatus); if (createStatus != MembershipCreateStatus.Success) { this.PageContext.AddLoadMessage( $"Create User Failed: {this.GetMembershipErrorMessage(createStatus)}", MessageTypes.danger); return(false); } // Create groups required for the new board RoleMembershipHelper.CreateRole("Administrators"); RoleMembershipHelper.CreateRole("Registered"); // Add new admin users to group RoleMembershipHelper.AddUserToRole(newAdmin.UserName, "Administrators"); // Create Board newBoardId = this.DbCreateBoard( boardName, boardMembershipAppName, boardRolesAppName, langFile, newAdmin); } else { // new admin var newAdmin = UserMembershipHelper.GetUser(); // Create Board newBoardId = this.DbCreateBoard( boardName, boardMembershipAppName, boardRolesAppName, langFile, newAdmin); } if (newBoardId > 0 && Config.MultiBoardFolders) { // Successfully created the new board var boardFolder = this.Server.MapPath(Path.Combine(Config.BoardRoot, $"{newBoardId}/")); // Create New Folders. if (!Directory.Exists(Path.Combine(boardFolder, "Images"))) { // Create the Images Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images")); // Create Sub Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Avatars")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Categories")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Forums")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Medals")); } if (!Directory.Exists(Path.Combine(boardFolder, "Uploads"))) { Directory.CreateDirectory(Path.Combine(boardFolder, "Uploads")); } } // Return application name to as they were before. this.Get <MembershipProvider>().ApplicationName = currentMembershipAppName; this.Get <RoleProvider>().ApplicationName = currentRolesAppName; return(true); }
/// <summary> /// The create board. /// </summary> /// <param name="adminName"> /// The admin name. /// </param> /// <param name="adminPassword"> /// The admin password. /// </param> /// <param name="adminEmail"> /// The admin email. /// </param> /// <param name="adminPasswordQuestion"> /// The admin password question. /// </param> /// <param name="adminPasswordAnswer"> /// The admin password answer. /// </param> /// <param name="boardName"> /// The board name. /// </param> /// <param name="boardMembershipAppName"> /// The board membership app name. /// </param> /// <param name="boardRolesAppName"> /// The board roles app name. /// </param> /// <param name="createUserAndRoles"> /// The create user and roles. /// </param> /// <exception cref="ApplicationException"> /// </exception> protected void CreateBoard( string adminName, string adminPassword, string adminEmail, string adminPasswordQuestion, string adminPasswordAnswer, string boardName, string boardMembershipAppName, string boardRolesAppName, bool createUserAndRoles) { // Store current App Names string currentMembershipAppName = PageContext.CurrentMembership.ApplicationName; string currentRolesAppName = PageContext.CurrentRoles.ApplicationName; if (boardMembershipAppName.IsSet() && boardRolesAppName.IsSet()) { // Change App Names for new board PageContext.CurrentMembership.ApplicationName = boardMembershipAppName; PageContext.CurrentMembership.ApplicationName = boardRolesAppName; } int newBoardID = 0; System.Data.DataTable cult = StaticDataHelper.Cultures(); string langFile = "english.xml"; foreach (System.Data.DataRow drow in cult.Rows) { if (drow["CultureTag"].ToString() == this.Culture.SelectedValue) { langFile = (string)drow["CultureFile"]; } } if (createUserAndRoles) { // Create new admin users MembershipCreateStatus createStatus; MembershipUser newAdmin = PageContext.CurrentMembership.CreateUser( adminName, adminPassword, adminEmail, adminPasswordQuestion, adminPasswordAnswer, true, null, out createStatus); if (createStatus != MembershipCreateStatus.Success) { PageContext.AddLoadMessage("Create User Failed: {0}".FormatWith(this.GetMembershipErrorMessage(createStatus))); throw new ApplicationException("Create User Failed: {0}".FormatWith(this.GetMembershipErrorMessage(createStatus))); } // Create groups required for the new board RoleMembershipHelper.CreateRole("Administrators"); RoleMembershipHelper.CreateRole("Registered"); // Add new admin users to group RoleMembershipHelper.AddUserToRole(newAdmin.UserName, "Administrators"); // Create Board newBoardID = DB.board_create(newAdmin.UserName, newAdmin.Email, newAdmin.ProviderUserKey, boardName, this.Culture.SelectedItem.Value, langFile, boardMembershipAppName, boardRolesAppName); } else { // new admin MembershipUser newAdmin = UserMembershipHelper.GetUser(); // Create Board newBoardID = DB.board_create(newAdmin.UserName, newAdmin.Email, newAdmin.ProviderUserKey, boardName, this.Culture.SelectedItem.Value, langFile, boardMembershipAppName, boardRolesAppName); } if (newBoardID > 0 && Config.MultiBoardFolders) { // Successfully created the new board string boardFolder = Server.MapPath(Path.Combine(Config.BoardRoot, newBoardID.ToString() + "/")); // Create New Folders. if (!Directory.Exists(Path.Combine(boardFolder, "Images"))) { // Create the Images Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images")); // Create Sub Folders Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Avatars")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Categories")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Forums")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Emoticons")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Medals")); Directory.CreateDirectory(Path.Combine(boardFolder, "Images\\Ranks")); } if (!Directory.Exists(Path.Combine(boardFolder, "Themes"))) { Directory.CreateDirectory(Path.Combine(boardFolder, "Themes")); // Need to copy default theme to the Themes Folder } if (!Directory.Exists(Path.Combine(boardFolder, "Uploads"))) { Directory.CreateDirectory(Path.Combine(boardFolder, "Uploads")); } } // Return application name to as they were before. YafContext.Current.CurrentMembership.ApplicationName = currentMembershipAppName; YafContext.Current.CurrentRoles.ApplicationName = currentRolesAppName; }