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