Beispiel #1
0
 /// <summary>
 /// Saves changes to the user.
 /// </summary>
 private void SaveUser()
 {
     if (!_user.Save())
     {
         MessageBox.Show(this, "An error occurred while saving your changes. Please try again.", "Save Unsuccessful", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
     }
     _user.Refresh();
     SetUser(_user);
 }
Beispiel #2
0
        private void _okButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (this.IsCreateMode)
            {
                // validate
                if (this.ValidateForm())
                {
                    // create new C-Access user account
                    char?  middleInitial = string.IsNullOrWhiteSpace(_miTextBox.Text) ? null : (char?)_miTextBox.Text.Trim().ToCharArray()[0];
                    string first         = _firstNameTextBox.Text.Trim();
                    string last          = _lastNameTextBox.Text.Trim();
                    CPUser user          = CPSecurity.Provider.CreateUser(first, middleInitial, last, _candidateID, _passwordTextBox.Text.Trim(), _cfisEmailTextBox.Text.Trim(), SettingsManager.CurrentUser.Username, _entityType, _committeeID, _electionCycle, _liaisonID, _newUsernameTextBox.Visible ? _newUsernameTextBox.Text : null);
                    if (user == null)
                    {
                        if (MessageBox.Show(this, string.Format("Unable to create a C-Access account for {0}. Retry?", Entity.ToFullName(first, last, middleInitial, false)), "Account Creation Failure", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == System.Windows.Forms.DialogResult.Retry)
                        {
                            _okButton_Click(sender, e);
                        }
                    }
                    else
                    {
                        // auto-set security group
                        var groups = from g in CPSecurity.Provider.GetGroups()
                                     where g.Name == user.SourceType.ToString() + "s"
                                     select g.ID;
                        if (groups.Any())
                        {
                            CPSecurity.Provider.AddUserToGroups(user.UserName, groups.ToList());
                        }

                        // auto-set election cycle access
                        if (!user.IsCandidate && !string.IsNullOrWhiteSpace(_electionCycle))
                        {
                            user.ElectionCycles.Clear();
                            user.ImplicitElectionCycles = false;
                            user.ElectionCycles.Add(_electionCycle);
                            user.Save();
                        }
                        else
                        {
                            user.Refresh();
                        }

                        // refresh form
                        this.SetUser(user);
                        _mainTabControl.SelectedTab = _generalTabPage;
                        _cancelButton.Location      = _cancelButtonLocation;
                        RefreshAccountExplorers();
                    }
                }
            }
            else if (_applyButton.Enabled)
            {
                _applyButton.PerformClick();
                if (!_applyButton.Enabled)
                {
                    this.Close();
                }
            }
            else
            {
                this.Close();
            }
            this.Cursor = Cursors.Default;
        }
Beispiel #3
0
        public CPUser CreateUser(string firstName, char?middleInitial, string lastName, string candidateID, string password, string email, string creator, EntityType type = EntityType.Generic, char?committeeID = null, string electionCycle = null, byte?liaisonID = null, string username = null)
        {
            if (username == null)
            {
                username = GenerateUserName(firstName, middleInitial, lastName, candidateID);
            }
            if (!string.IsNullOrWhiteSpace(username))
            {
                if (password == null)
                {
                    password = Membership.GeneratePassword(10, 0);
                }
                if (UserManagement.CreateUser(username, password, email, creator) != null)
                {
                    using (CPSecurityEntities context = new CPSecurityEntities())
                    {
                        switch (type) // sanitize CFIS source entity properties first
                        {
                        case EntityType.Liaison:
                            electionCycle = null;
                            break;

                        case EntityType.Treasurer:
                            liaisonID = null;
                            break;

                        default:
                            committeeID   = null;
                            electionCycle = null;
                            liaisonID     = null;
                            break;
                        }
                        context.DeleteSecurityUserProfile(username);
                        CPUser user = CPUserFactory.CreateUser(context.AddToSecurityUserProfiles(username, candidateID, Entity.ToFullName(firstName, lastName, middleInitial, false), type, committeeID, electionCycle, liaisonID));
                        if (user != null)
                        {
                            CPGroup group = GetGroup(type.ToString() + "s");
                            if (group != null)
                            {
                                AddUserToGroups(user.UserName, new List <byte>(new[] { group.ID }));
                            }
                            if (electionCycle != null)
                            {
                                user.ElectionCycles.Add(electionCycle);
                            }
                            if (!user.Save())
                            {
                                this.DeleteUser(user.UserName);
                                user = null;
                            }
                        }
                        else
                        {
                            UserManagement.DeleteUser(Membership.GetUser(username));
                        }
                        return(user);
                    }
                }
            }
            return(null);
        }