/// <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)); } }
/// <summary> /// Gets the candidate ID associated with a C-Access user account. /// </summary> /// <param name="username">The name of the user to query.</param> /// <returns>The candidate ID associated with the specified user.</returns> public string GetCid(string username) { using (CPSecurityEntities context = new CPSecurityEntities()) { var match = from p in context.SecurityUserProfiles where p.UserName == username select p.CandidateId; if (match.Count() > 0) { return(match.First()); } // attempt to convert legacy data var profile = context.AddToSecurityUserProfiles(Membership.GetUser(username)); return(profile == null ? null : profile.CandidateId); } }
/// <summary> /// Creates a new security user profile entity instance from a membership user and adds it to the datastore. /// </summary> /// <param name="context">The datastore entity context to use.</param> /// <param name="user">The membership user to convert and add.</param> /// <returns>A <see cref="SecurityUserProfile"/> instance representing the newly created profile that has been added to the datastore.</returns> public static SecurityUserProfile AddToSecurityUserProfiles(this CPSecurityEntities context, MembershipUser user) { if (user == null) { return(null); } string username = user.UserName; string caid = UserManagement.GetCaid(username); SecurityUserProfile profile = context.AddToSecurityUserProfiles(username, UserManagement.GetCfisId(username), UserManagement.GetFullName(user), AccountAnalysis.ParseEntityType(caid), AccountAnalysis.ParseCommitteeID(caid), AccountAnalysis.ParseElectionCycle(caid), AccountAnalysis.ParseLiaisonID(caid), UserManagement.IsPasswordExpired(user)); return(profile); }
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); }