Пример #1
0
        /// <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);
        }