/// <summary>
    /// Removes membership from user. Called when the "Remove membership from user" button is pressed.
    /// </summary>
    private bool RemoveMembershipFromUser()
    {
        // Get role and user objects
        UserInfo       user       = UserInfoProvider.GetUserInfo("MyNewUser");
        MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("MyNewMembership", CMSContext.CurrentSiteName);

        if ((user != null) && (membership != null))
        {
            MembershipUserInfoProvider.RemoveMembershipFromUser(membership.MembershipID, user.UserID);

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Store selected (unselected) roles.
    /// </summary>
    private void SaveData()
    {
        bool updateUser = false;

        // Check permission for manage membership for users
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.Membership", "ManageUserMembership"))
        {
            RedirectToAccessDenied("CMS.Membership", "Manage user membership");
        }

        string result = ValidateGlobalAndDeskAdmin(ui);

        if (result != String.Empty)
        {
            ShowError(result);
            return;
        }

        // Remove old items
        string newValues = ValidationHelper.GetString(usMemberships.Value, null);
        string items     = DataHelper.GetNewItemsInList(newValues, currentValues);

        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                foreach (string item in newItems)
                {
                    int membershipID = ValidationHelper.GetInteger(item, 0);
                    MembershipUserInfoProvider.RemoveMembershipFromUser(membershipID, UserID);
                    updateUser = true;
                }
            }
        }


        // Add new items
        items = DataHelper.GetNewItemsInList(currentValues, newValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                DateTime dt = ValidationHelper.GetDateTime(hdnDate.Value, DateTimeHelper.ZERO_TIME);

                // Add all new items to membership
                foreach (string item in newItems)
                {
                    int membershipID = ValidationHelper.GetInteger(item, 0);
                    MembershipUserInfoProvider.AddMembershipToUser(membershipID, UserID, dt);
                    updateUser = true;
                }
            }
        }

        if (updateUser)
        {
            usMemberships.Reload(true);

            // Invalidate user object
            if (ui != null)
            {
                ui.Generalized.Invalidate(false);
            }

            ShowChangesSaved();
        }
    }
示例#3
0
    private void SaveUsers()
    {
        // Check "manage user membership" permission
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.Membership", "ManageUserMembership"))
        {
            RedirectToAccessDenied("CMS.Membership", "Manage user membership");
        }

        // Remove old items
        string        newValues = ValidationHelper.GetString(usUsers.Value, null);
        string        items     = DataHelper.GetNewItemsInList(newValues, currentValues);
        bool          reload    = false;
        bool          saved     = false;
        StringBuilder errors    = new StringBuilder();

        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                // Add all new items to user
                foreach (string item in newItems)
                {
                    int    userId = ValidationHelper.GetInteger(item, 0);
                    string result = ValidateGlobalAndDeskAdmin(userId);

                    if (result != String.Empty)
                    {
                        errors.AppendLine(result);
                        reload = true;
                        continue;
                    }
                    else
                    {
                        MembershipUserInfoProvider.RemoveMembershipFromUser(membershipID, userId);

                        // Invalidate UserInfo
                        UserInfoProvider.InvalidateUser(userId);

                        saved = true;
                    }
                }
            }
        }

        // Add new items
        items = DataHelper.GetNewItemsInList(currentValues, newValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems         = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            DateTime dt               = ValidationHelper.GetDateTime(hdnDate.Value, DateTimeHelper.ZERO_TIME);
            bool     sendNotification = ValidationHelper.GetBoolean(hdnSendNotification.Value, false);

            if (newItems != null)
            {
                // Add all new items to user
                foreach (string item in newItems)
                {
                    int    userId = ValidationHelper.GetInteger(item, 0);
                    string result = ValidateGlobalAndDeskAdmin(userId);

                    if (result != String.Empty)
                    {
                        errors.AppendLine(result);
                        reload = true;
                        continue;
                    }
                    else
                    {
                        MembershipUserInfoProvider.AddMembershipToUser(membershipID, userId, dt, sendNotification);

                        // Invalidate UserInfo
                        UserInfoProvider.InvalidateUser(userId);

                        saved = true;
                    }
                }
            }
        }

        if (errors.Length > 0)
        {
            ShowError(GetString("general.saveerror"), errors.ToString(), null);
        }

        if (reload)
        {
            currentValues = GetMembershipUsers();
            usUsers.Value = currentValues;
        }

        if (saved)
        {
            ShowChangesSaved();
        }

        usUsers.Reload(true);
    }
示例#4
0
    private void SaveUsers()
    {
        // Check "manage user membership" permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.Membership", "ManageUserMembership"))
        {
            RedirectToAccessDenied("CMS.Membership", "Manage user membership");
        }

        // Remove old items
        string newValues = ValidationHelper.GetString(usUsers.Value, null);
        string items     = DataHelper.GetNewItemsInList(newValues, currentValues);
        bool   reload    = false;
        bool   saved     = false;

        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (newItems != null)
            {
                // Add all new items to user
                foreach (string item in newItems)
                {
                    int    userId = ValidationHelper.GetInteger(item, 0);
                    string result = ValidateGlobalAndDeskAdmin(userId);

                    if (result != String.Empty)
                    {
                        lblError.Visible = true;
                        lblError.Text   += result;
                        reload           = true;
                        continue;
                    }
                    else
                    {
                        MembershipUserInfoProvider.RemoveMembershipFromUser(membershipID, userId);
                        // Invalidate UserInfo
                        UserInfo ui = UserInfoProvider.GetUserInfo(userId);
                        if (ui != null)
                        {
                            ui.Invalidate();
                        }

                        saved = true;
                    }
                }
            }
        }

        // Add new items
        items = DataHelper.GetNewItemsInList(currentValues, newValues);
        if (!String.IsNullOrEmpty(items))
        {
            string[] newItems         = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            DateTime dt               = ValidationHelper.GetDateTime(hdnDate.Value, DateTimeHelper.ZERO_TIME);
            bool     sendNotification = ValidationHelper.GetBoolean(this.hdnSendNotification.Value, false);

            if (newItems != null)
            {
                // Add all new items to user
                foreach (string item in newItems)
                {
                    int    userId = ValidationHelper.GetInteger(item, 0);
                    string result = ValidateGlobalAndDeskAdmin(userId);

                    if (result != String.Empty)
                    {
                        lblError.Visible = true;
                        lblError.Text   += result;
                        reload           = true;
                        continue;
                    }
                    else
                    {
                        MembershipUserInfoProvider.AddMembershipToUser(membershipID, userId, dt, sendNotification);

                        // Invalidate UserInfo
                        UserInfo ui = UserInfoProvider.GetUserInfo(userId);
                        if (ui != null)
                        {
                            ui.Invalidate();
                        }

                        saved = true;
                    }
                }
            }
        }

        if (reload)
        {
            currentValues = GetMembershipUsers();
            usUsers.Value = currentValues;
        }

        if (saved)
        {
            lblInfo.Visible = true;
            lblInfo.Text    = GetString("General.ChangesSaved");
        }

        usUsers.Reload(true);
    }