/// <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="boardName">The board 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 boardName, bool createUserAndRoles) { 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) { var user = new AspNetUsers { Id = Guid.NewGuid().ToString(), ApplicationId = this.Get <BoardSettings>().ApplicationId, UserName = adminName, LoweredUserName = adminName, Email = adminEmail, IsApproved = true }; // Create new admin users var result = this.Get <IAspNetUsersHelper>().Create(user, adminPassword); if (!result.Succeeded) { this.PageContext.AddLoadMessage( $"Create User Failed: {result.Errors.FirstOrDefault()}", MessageTypes.danger); return(false); } // Create groups required for the new board AspNetRolesHelper.CreateRole("Administrators"); AspNetRolesHelper.CreateRole("Registered"); // Add new admin users to group AspNetRolesHelper.AddUserToRole(user, "Administrators"); // Create Board newBoardId = this.DbCreateBoard( boardName, langFile, user); } else { // new admin var newAdmin = this.Get <IAspNetUsersHelper>().GetUser(); // Create Board newBoardId = this.DbCreateBoard( boardName, langFile, newAdmin); } if (newBoardId <= 0 || !Config.MultiBoardFolders) { return(true); } // 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(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 user = this.Get <IAspNetUsersHelper>().GetMembershipUserById(this.CurrentUserID); // 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 (this.Get <IAspNetUsersHelper>().IsGuestUser(this.CurrentUserID)) { continue; } // add/remove user from roles in membership provider if (isChecked && !AspNetRolesHelper.IsUserInRole(user, roleName)) { AspNetRolesHelper.AddUserToRole(user, roleName); addedRoles.Add(roleName); } else if (!isChecked && AspNetRolesHelper.IsUserInRole(user, roleName)) { AspNetRolesHelper.RemoveUserFromRole(user.Id, 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> /// Saves the click. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void SaveClick([NotNull] object sender, [NotNull] EventArgs e) { if (!ValidationHelper.IsValidInt(this.PMLimit.Text.Trim())) { this.PageContext.AddLoadMessage( this.GetText("ADMIN_EDITGROUP", "MSG_VALID_NUMBER"), MessageTypes.warning); return; } if (!ValidationHelper.IsValidInt(this.Priority.Text.Trim())) { this.PageContext.AddLoadMessage(this.GetText("ADMIN_EDITGROUP", "MSG_INTEGER"), MessageTypes.warning); return; } if (!ValidationHelper.IsValidInt(this.UsrAlbums.Text.Trim())) { this.PageContext.AddLoadMessage( this.GetText("ADMIN_EDITGROUP", "MSG_ALBUM_NUMBER"), MessageTypes.warning); return; } if (!ValidationHelper.IsValidInt(this.UsrSigChars.Text.Trim())) { this.PageContext.AddLoadMessage( this.GetText("ADMIN_EDITGROUP", "MSG_SIG_NUMBER"), MessageTypes.warning); return; } if (!ValidationHelper.IsValidInt(this.UsrAlbumImages.Text.Trim())) { this.PageContext.AddLoadMessage( this.GetText("ADMIN_EDITGROUP", "MSG_TOTAL_NUMBER"), MessageTypes.warning); return; } // Role long roleId = 0; // get role ID from page's parameter if (this.Get <HttpRequestBase>().QueryString.Exists("i")) { roleId = long.Parse(this.Get <HttpRequestBase>().QueryString.GetFirstOrDefault("i")); } // get new and old name var roleName = this.Name.Text.Trim(); var oldRoleName = string.Empty; // if we are editing existing role, get it's original name if (roleId != 0) { // get the current role name in the DB var groups = this.GetRepository <Group>().List(boardId: this.PageContext.PageBoardID); groups.ForEach(group => oldRoleName = group.Name); } // save role and get its ID if it's new (if it's old role, we get it anyway) roleId = this.GetRepository <Group>().Save( roleId, this.PageContext.PageBoardID, roleName, this.IsAdminX.Checked, this.IsGuestX.Checked, this.IsStartX.Checked, this.IsModeratorX.Checked, this.AccessMaskID.SelectedValue, this.PMLimit.Text.Trim(), this.StyleTextBox.Text.Trim(), this.Priority.Text.Trim(), this.Description.Text, this.UsrSigChars.Text, this.UsrSigBBCodes.Text, this.UsrSigHTMLTags.Text, this.UsrAlbums.Text.Trim(), this.UsrAlbumImages.Text.Trim()); // empty out access table(s) this.GetRepository <Active>().DeleteAll(); this.GetRepository <ActiveAccess>().DeleteAll(); // see if need to rename an existing role... if (oldRoleName.IsSet() && roleName != oldRoleName && AspNetRolesHelper.RoleExists(oldRoleName) && !AspNetRolesHelper.RoleExists(roleName) && !this.IsGuestX.Checked) { // transfer users in addition to changing the name of the role... var users = AspNetRolesHelper.GetUsersInRole(oldRoleName); // delete the old role... AspNetRolesHelper.DeleteRole(oldRoleName); // create new role... AspNetRolesHelper.CreateRole(roleName); if (users.Any()) { // put users into new role... users.ForEach(user => AspNetRolesHelper.AddUserToRole(user, roleName)); } } else if (!AspNetRolesHelper.RoleExists(roleName) && !this.IsGuestX.Checked) { // if role doesn't exist in provider's data source, create it // simply create it AspNetRolesHelper.CreateRole(roleName); } // Access masks for a newly created or an existing role if (this.Get <HttpRequestBase>().QueryString.Exists("i")) { // go through all forums for (var i = 0; i < this.AccessList.Items.Count; i++) { // get current repeater item var item = this.AccessList.Items[i]; // get forum ID var forumId = int.Parse(item.FindControlAs <HiddenField>("ForumID").Value); // save forum access masks for this role this.GetRepository <ForumAccess>().Save( forumId, roleId.ToType <int>(), item.FindControlAs <DropDownList>("AccessmaskID").SelectedValue.ToType <int>()); } BuildLink.Redirect(ForumPages.Admin_Groups); } // remove caching in case something got updated... this.Get <IDataCache>().Remove(Constants.Cache.ForumModerators); // Clearing cache with old permissions data... this.Get <IDataCache>().Remove( k => k.StartsWith(string.Format(Constants.Cache.ActiveUserLazyData, string.Empty))); // Clear Styling Caching this.Get <IDataCache>().Remove(Constants.Cache.GroupRankStyles); // Done, redirect to role editing page BuildLink.Redirect(ForumPages.Admin_EditGroup, "i={0}", roleId); }