/// <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>().List(boardId: this.PageContext.PageBoardID, groupId: roleID)
                               .FirstOrDefault().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();
        }
Example #2
0
        /// <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 (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 = this.Get <IDbFunction>().GetAsDataTable(cdb => cdb.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
                this.Get <IDbFunction>().Query.usergroup_save(this.CurrentUserID, roleID, isChecked);

                // empty out access table
                this.Get <IDbFunction>().Query.activeaccess_reset();

                // update roles if this user isn't the guest
                if (UserMembershipHelper.IsGuestUser(this.CurrentUserID))
                {
                    continue;
                }

                // 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.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();
        }