SecurityUser CreateWebUser() { if (Repository == null) { CreateRepository(); } var user = new SecurityUser(Repository.Security); //Load profile if (System.IO.File.Exists(user.ProfilePath)) { user.Profile = SecurityUserProfile.LoadFromFile(user.ProfilePath); } user.Profile.Path = user.ProfilePath; setSessionValue(SessionUser, user); //Clear previous Session variables setSessionValue(SessionNavigationContext, null); setSessionValue(SessionDashboardExecutions, null); #if NETCOREAPP user.SessionID = SessionKey; #else user.SessionID = Session.SessionID; #endif return(user); }
/// <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> /// Creates a new <see cref="CPUser"/> instance using C-Access Security profile data. /// </summary> /// <param name="profile">The profile to use.</param> /// <returns>A new C-Access user account instance using the specified C-Access Security profile data if valid; otherwise, null.</returns> public static CPUser CreateUser(SecurityUserProfile profile) { CPUser user = null; if (profile != null) { user = CreateUser(Membership.GetUser(profile.UserName)); if (user != null) { user.DisplayName = profile.DisplayName; user.Cid = profile.CandidateId; user.PasswordExpired = profile.PasswordExpired; // load additional user data user.ElectionCycles.AddRange(CPSecurity.Provider.GetAuthorizedElectionCycles(user.UserName)); user.ImplicitElectionCycles = !SecurityService.GetExplicitElectionCycles(user.UserName).Any(); user.UserRights = CPSecurity.Provider.GetUserRights(user.UserName); using (CPSecurityEntities context = new CPSecurityEntities()) { foreach (var app in context.SecuritySsoApplications) { if (user.UserRights.HasFlag((CPUserRights)app.UserRights)) { user.Applications.Add(ApplicationFactory.CreateApplication(app)); } } } // get CFIS source info user.SourceType = Enum.IsDefined(typeof(EntityType), profile.CfisType) ? (EntityType)profile.CfisType : EntityType.Generic; if (!string.IsNullOrWhiteSpace(profile.CfisCommitteeID)) { user.SourceCommitteeID = profile.CfisCommitteeID.ToCharArray()[0]; } byte liaisonID; if (byte.TryParse(profile.CfisCommitteeContactID, out liaisonID)) { user.SourceLiaisonID = liaisonID; // attempt to determine election cycle of associated committee if (user.SourceCommitteeID.HasValue) { char commID = user.SourceCommitteeID.Value; var elections = from ec in CPProviders.DataProvider.GetActiveElectionCycles(profile.CandidateId, CPProviders.SettingsProvider.MinimumElectionCycle) let comm = CPProviders.DataProvider.GetAuthorizedCommittees(profile.CandidateId, ec) where comm.Committees.ContainsKey(commID) select ec; if (elections.Any()) { user.SourceElectionCycle = elections.First(); } } } else { user.SourceElectionCycle = profile.CfisCommitteeContactID; } } } return(user); }
SecurityUser CreateWebUser() { if (Repository == null) { CreateRepository(); } var user = new SecurityUser(Repository.Security); //Load profile if (System.IO.File.Exists(user.ProfilePath)) { user.Profile = SecurityUserProfile.LoadFromFile(user.ProfilePath); } user.Profile.Path = user.ProfilePath; Session[SessionUser] = user; return(user); }
/// <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); }
/// <summary> /// Adds a new security user profile entity instance to the datastore. /// </summary> /// <param name="context">The datastore entity context to use.</param> /// <param name="username">The username for the new profile.</param> /// <param name="candidateID">The candidate ID for the new profile.</param> /// <param name="displayName">The display name for the profile.</param> /// <param name="type">The entity type of the CFB-registered contact used to create the profile.</param> /// <param name="committeeID">If not representing a candidate, the ID of the profile's committee.</param> /// <param name="electionCycle">If representing a treasurer, the election cycle for which the profile's committee is authorized.</param> /// <param name="liaisonID">If representing a liaison, the committee-relative liaison ID of the profile.</param> /// <param name="passwordExpired">Indicates whether or not the password on the newly created profile is to be expired.</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, string username, string candidateID, string displayName = null, EntityType type = EntityType.Generic, char?committeeID = null, string electionCycle = null, byte?liaisonID = null, bool passwordExpired = true) { if (context == null || string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(candidateID)) { return(null); } SecurityUserProfile profile = SecurityUserProfile.CreateSecurityUserProfile(username, candidateID, passwordExpired, (byte)type); profile.DisplayName = displayName; if (committeeID.HasValue) { profile.CfisCommitteeID = committeeID.Value.ToString(); } profile.CfisCommitteeContactID = liaisonID.HasValue ? liaisonID.Value.ToString() : electionCycle; context.SecurityUserProfiles.AddObject(profile); context.SaveChanges(); return(profile); }
SecurityUser CreateWebUser() { if (Repository == null) { CreateRepository(); } var user = new SecurityUser(Repository.Security); //Load profile if (System.IO.File.Exists(user.ProfilePath)) { user.Profile = SecurityUserProfile.LoadFromFile(user.ProfilePath); } user.Profile.Path = user.ProfilePath; Session[SessionUser] = user; //Clear previous Session variables Session[SessionNavigationContext] = null; Session[SessionDashboardExecutions] = null; return(user); }
/// <summary> /// Start a session with the Web Report Server using the user name, password, token (may be optional according to the authentication configured on the server) and returns information of the logged user (SWIUserProfile). /// </summary> public ActionResult SWILogin(string user, string password, string token) { writeDebug("SWILogin"); try { bool newAuthentication = false; if (WebUser == null || !WebUser.IsAuthenticated || (!string.IsNullOrEmpty(user) && WebUser.WebUserName != user) || (!string.IsNullOrEmpty(token) && WebUser.Token != token)) { CreateRepository(); CreateWebUser(); WebUser.WebPrincipal = User; WebUser.WebUserName = user; WebUser.WebPassword = password; WebUser.Token = token; WebUser.Request = Request; Authenticate(); if (!WebUser.IsAuthenticated) { throw new LoginException(string.IsNullOrEmpty(WebUser.Error) ? Translate("Invalid user name or password") : WebUser.Error); } //Load profile if (System.IO.File.Exists(WebUser.ProfilePath)) { WebUser.Profile = SecurityUserProfile.LoadFromFile(WebUser.ProfilePath); } checkRecentFiles(); WebUser.Profile.Path = WebUser.ProfilePath; newAuthentication = true; } //Audit Audit.LogAudit(AuditType.Login, WebUser); Audit.LogEventAudit(AuditType.EventLoggedUsers, SealSecurity.LoggedUsers.Count(i => i.IsAuthenticated).ToString()); //Set repository defaults var defaultGroup = WebUser.DefaultGroup; if (!string.IsNullOrEmpty(defaultGroup.LogoName)) { Repository.Configuration.LogoName = defaultGroup.LogoName; } string culture = WebUser.Profile.Culture; if (!string.IsNullOrEmpty(culture)) { Repository.SetCultureInfo(culture); } else if (!string.IsNullOrEmpty(defaultGroup.Culture)) { Repository.SetCultureInfo(defaultGroup.Culture); } else if (Repository.CultureInfo.EnglishName != Repository.Instance.CultureInfo.EnglishName) { Repository.SetCultureInfo(Repository.Instance.CultureInfo.EnglishName); } string reportToExecute = "", reportToExecuteName = ""; bool executeLast = false; if (defaultGroup.EditProfile && WebUser.Profile.OnStartup != StartupOptions.Default) { if (WebUser.Profile.OnStartup == StartupOptions.ExecuteLast && WebUser.Profile.RecentReports.Count > 0) { executeLast = true; } else if (WebUser.Profile.OnStartup == StartupOptions.ExecuteReport) { reportToExecute = WebUser.Profile.StartUpReport; reportToExecuteName = WebUser.Profile.StartupReportName; } } else { if (defaultGroup.OnStartup == StartupOptions.ExecuteLast) { executeLast = true; } else if (defaultGroup.OnStartup == StartupOptions.ExecuteReport) { reportToExecute = defaultGroup.StartupReport; reportToExecuteName = Repository.TranslateFileName(defaultGroup.StartupReport); } } if (executeLast && WebUser.Profile.RecentReports.Count > 0) { reportToExecute = WebUser.Profile.RecentReports[0].Path; reportToExecuteName = WebUser.Profile.RecentReports[0].Name; } //Refresh menu reports if (newAuthentication) { MenuReportViewsPool.ForceReload(); } var profile = new SWIUserProfile() { name = WebUser.Name, group = WebUser.SecurityGroupsDisplay, culture = culture, folder = WebUser.Profile.LastFolder, showfolders = WebUser.ShowFoldersView, editprofile = defaultGroup.EditProfile, usertag = WebUser.Tag, onstartup = WebUser.Profile.OnStartup, startupreport = WebUser.Profile.StartUpReport, startupreportname = WebUser.Profile.StartupReportName, report = reportToExecute, reportname = reportToExecuteName, }; if (!string.IsNullOrEmpty(profile.startupreport)) { try { getFileDetail(profile.startupreport); } catch { profile.startupreport = ""; profile.startupreportname = ""; } } if (!string.IsNullOrEmpty(profile.report)) { try { getFileDetail(profile.report); } catch { profile.report = ""; profile.reportname = ""; } } return(Json(profile, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { return(HandleSWIException(ex)); } }
/// <summary> /// Start a session with the Web Report Server using the user name, password, token (may be optional according to the authentication configured on the server) and returns information of the logged user (SWIUserProfile). /// </summary> public ActionResult SWILogin(string user, string password, string token) { writeDebug("SWILogin"); try { bool newAuthentication = false; if (WebUser == null || !WebUser.IsAuthenticated || (!string.IsNullOrEmpty(user) && WebUser.WebUserName != user) || (!string.IsNullOrEmpty(token) && WebUser.Token != token)) { CreateRepository(); CreateWebUser(); WebUser.WebPrincipal = User; WebUser.WebUserName = user; WebUser.WebPassword = password; WebUser.Token = token; WebUser.Request = Request; Authenticate(); if (!WebUser.IsAuthenticated) { throw new LoginException(string.IsNullOrEmpty(WebUser.Error) ? Translate("Invalid user name or password") : WebUser.Error); } //Load profile if (System.IO.File.Exists(WebUser.ProfilePath)) { WebUser.Profile = SecurityUserProfile.LoadFromFile(WebUser.ProfilePath); } checkRecentFiles(); WebUser.Profile.Path = WebUser.ProfilePath; newAuthentication = true; } //Audit Audit.LogAudit(AuditType.Login, WebUser); Audit.LogEventAudit(AuditType.EventLoggedUsers, SealSecurity.LoggedUsers.Count(i => i.IsAuthenticated).ToString()); //Set culture string culture = WebUser.Profile.Culture; if (string.IsNullOrEmpty(culture) || string.IsNullOrEmpty(WebUser.WebUserName)) { culture = getCookie(SealCultureCookieName); } if (!string.IsNullOrEmpty(culture)) { Repository.SetCultureInfo(culture); } //Refresh menu reports if (newAuthentication) { MenuReportViewsPool.ForceReload(); } return(Json(new SWIUserProfile() { name = WebUser.Name, group = WebUser.SecurityGroupsDisplay, culture = Repository.CultureInfo.EnglishName, folder = getCookie(SealLastFolderCookieName), showfolders = WebUser.ShowFoldersView, usertag = WebUser.Tag }));; } catch (Exception ex) { return(HandleSWIException(ex)); } }