Exemplo n.º 1
0
        /// <summary>
        /// Gets a C-Access user account.
        /// </summary>
        /// <param name="username">The login name for the account to retrieve.</param>
        /// <returns>The C-Access user account matching the specified username if found; otherwise, null.</returns>
        public CPUser GetUser(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                return(null);
            }
            username = username.Trim();
            MembershipUser muser = Membership.GetUser(username);

            if (muser == null)
            {
                return(null);
            }
            username = muser.UserName;
            using (CPSecurityEntities context = new CPSecurityEntities())
            {
                SecurityUserProfile profile = (from p in context.SecurityUserProfiles
                                               where p.UserName == username
                                               select p).FirstOrDefault();

                // automatic legacy .NET Membership/SharePoint conversion
                if (profile == null)
                {
                    profile = context.AddToSecurityUserProfiles(Membership.GetUser(username));
                }

                return(CPUserFactory.CreateUser(profile));
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Searches for C-Access user accounts matching specific criteria.
        /// </summary>
        /// <param name="name">All or part of a display name to search for.</param>
        /// <param name="email">An email address to search for.</param>
        /// <param name="candidateID">The ID of a candidate associated with the accounts being sought.</param>
        /// <param name="electionCycle">An election cycle that is accessible to the accounts being sought.</param>
        /// <param name="groupID">The ID of a group of which the accounts being sought are members.</param>
        /// <param name="disabled">Whether or not the accounts being sought are disabled.</param>
        /// <param name="lockedOut">Whether or not the accounts being sought are locked out.</param>
        /// <param name="used">Whether or not the accounts being sought have ever been used.</param>
        /// <param name="createdStartDate">A creation date filter start date.</param>
        /// <param name="createdEndDate">A creation date filter end date.</param>
        /// <returns>A collection of C-Access accounts matching the specified search criteria.</returns>
        public List <CPUser> FindUsers(string name = null, string email = null, string candidateID = null, string electionCycle = null, byte?groupID = null, bool?disabled = null, bool?lockedOut = null, bool?used = null, DateTime?createdStartDate = null, DateTime?createdEndDate = null)
        {
            IEnumerable <CPUser> results;

            // filter by user profile fields
            using (Data.CPSecurityEntities context = new Data.CPSecurityEntities())
            {
                var profiles = context.SecurityUserProfiles.AsQueryable();
                if (!string.IsNullOrWhiteSpace(name))
                {
                    foreach (var n in name.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        profiles = profiles.Where(u => u.DisplayName.Contains(n));
                    }
                }
                if (!string.IsNullOrWhiteSpace(candidateID))
                {
                    profiles = profiles.Where(u => u.CandidateId == candidateID);
                }
                if (groupID.HasValue)
                {
                    profiles = from p in profiles
                               join g in context.SecurityGroupMemberships on p.UserName equals g.UserName
                               where g.GroupId == groupID.Value
                               select p;
                }
                if (!string.IsNullOrWhiteSpace(email))
                {
                    var emailMatches = Membership.FindUsersByEmail(email);
                    var musers       = new MembershipUser[emailMatches.Count];
                    if (emailMatches.Count > 0)
                    {
                        emailMatches.CopyTo(musers, 0);
                    }
                    var usernames = musers.Select(mu => mu.UserName);
                    profiles = from p in profiles
                               where usernames.Contains(p.UserName)
                               select p;
                }
                if (!string.IsNullOrWhiteSpace(electionCycle))
                {
                    // include users with no election cycle filter
                    var unrestricted = from p in profiles
                                       where !context.SecurityUserElectionCycles.Any(uec => uec.UserName == p.UserName)
                                       select p;
                    profiles = from p in profiles
                               join e in context.SecurityUserElectionCycles on p.UserName equals e.UserName
                               where e.ElectionCycle == electionCycle
                               select p;
                    profiles = profiles.ToList().Concat(unrestricted.ToList().Where(c => Elections.GetActiveElectionCycles(c.CandidateId).Contains(electionCycle))).AsQueryable();
                }

                // convert results to CPUser objects
                results = profiles.ToList().Select(p => CPUserFactory.CreateUser(p)).Where(u => u != null);
            }

            // filter by membership fields
            if (disabled.HasValue)
            {
                results = results.Where(u => u.Enabled == !disabled.Value);
            }
            if (lockedOut.HasValue)
            {
                results = results.Where(u => u.LockedOut == lockedOut.Value);
            }
            if (used.HasValue)
            {
                results = results.Where(u => u.LastLoginDate.HasValue == used.Value);
            }
            if (createdStartDate.HasValue)
            {
                results = results.Where(u => u.CreationDate >= createdStartDate.Value);
            }
            if (createdEndDate.HasValue)
            {
                results = results.Where(u => u.CreationDate.Date <= createdEndDate.Value);
            }

            return(results.ToList());
        }