Esempio n. 1
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());
        }