/// <summary> /// Restores the original context user /// </summary> /// <param name="args">The arguments.</param> public static void RestoreContextUser(JobArgs args) { if (args.Job.Options.ContextUser != null) { UserSwitcher.Exit(); } }
public void Dispose() { if (_securityDisabler != null) { _securityDisabler.Dispose(); _securityDisabler = null; } if (_siteContextSwitcher != null) { _siteContextSwitcher.Dispose(); _siteContextSwitcher = null; } if (_languageSwitcher != null) { _languageSwitcher.Dispose(); _languageSwitcher = null; } if (_databaseSwitcher != null) { _databaseSwitcher.Dispose(); _databaseSwitcher = null; } if (_userSwitcher != null) { _userSwitcher.Dispose(); _userSwitcher = null; } }
public bool LoginUser(string userName, string password) { if (!ServiceAuthorizationManager.IsUserAuthorized(WebServiceSettings.ServiceClient, userName)) { return(false); } if (!userName.Contains("\\")) { userName = "******" + userName; } if (Sitecore.Context.IsLoggedIn) { if (Sitecore.Context.User.Name.Equals(userName, StringComparison.OrdinalIgnoreCase)) { return(true); } Sitecore.Context.Logout(); } if (!LicenseManager.HasContentManager && !LicenseManager.HasExpress) { throw new AccessDeniedException("A required license is missing"); } Assert.IsTrue(Membership.ValidateUser(userName, password), "Unknown username or password."); var user = Sitecore.Security.Accounts.User.FromName(userName, true); UserSwitcher.Enter(user); return(true); }
/// <summary> /// Checks, if the user has read access to the item path. /// </summary> /// <param name="database">The database.</param> /// <param name="userName">The user name.</param> /// <param name="itemPath">The item path.</param> /// <returns> /// <c>true</c> if the user can acces the item; otherwise, <c>false</c>. /// </returns> private bool CanAccessItem(Database database, string userName, string itemPath) { bool flag; using (UserSwitcher userSwitcher = new UserSwitcher(User.FromName(userName, false))) { Item item = database.GetItem(itemPath); flag = (item == null ? false : item.Access.CanRead()); } return(flag); }
public ReportContext(string userName = "******", string siteName = "shell", string databaseName = "master", string language = null) { var user = User.FromName(userName, false); _userSwitcher = new UserSwitcher(user); _databaseSwitcher = new DatabaseSwitcher(Sitecore.Configuration.Factory.GetDatabase(databaseName)); if (!string.IsNullOrEmpty(language)) { _languageSwitcher = new LanguageSwitcher(language); } _siteContextSwitcher = new SiteContextSwitcher(SiteContextFactory.GetSiteContext(siteName)); _securityDisabler = new SecurityDisabler(); }
private void Login(Credentials credentials) { Error.AssertObject(credentials, "credentials"); if (Sitecore.Context.IsLoggedIn) { if (Sitecore.Context.User.Name.Equals(credentials.UserName)) { return; } // Sitecore.Context.Logout(); Sitecore.Security.Authentication.AuthenticationManager.Logout(); } Assert.IsTrue(Membership.ValidateUser(credentials.UserName, credentials.Password), "Unknown username or password."); UserSwitcher.Enter(Sitecore.Security.Accounts.User.FromName(credentials.UserName, true)); _Credentials = credentials; }
public void RunAsUser(string username) { if (UserSwitcher.CurrentValue != null) { Log(string.Format("Finished running as '{0}'", UserSwitcher.CurrentValue.Name)); UserSwitcher.Exit(); } if (string.IsNullOrWhiteSpace(username)) { return; } Log(string.Format("Running as '{0}'", username)); UserSwitcher.Enter(User.FromName(username, true)); }
protected override void ProcessRecord() { RecoverHttpContext(); var username = Identity.Name; if (!username.Contains(@"\") && !String.IsNullOrEmpty(username)) { username = @"sitecore\" + username; } if (!User.Exists(username)) { WriteError(new ErrorRecord( new ObjectNotFoundException("User '" + username + "' could not be found"), "user not found", ErrorCategory.ObjectNotFound, null)); } if (ShouldProcess(username, "Login as user")) { if (Context.IsLoggedIn) { if (Context.User.Name.Equals(username, StringComparison.OrdinalIgnoreCase)) { return; } var authenticationManager = TypeResolver.ResolveFromCache <IAuthenticationManager>(); authenticationManager.Logout(); } if (!LicenseManager.HasContentManager && !LicenseManager.HasExpress) { WriteError(new ErrorRecord(new LicenseException("A required license is missing"), "sitecore_license_missing", ErrorCategory.ResourceUnavailable, null)); } if (!Membership.ValidateUser(username, Password)) { WriteError(new ErrorRecord(new LicenseException("Unknown username or password."), "sitecore_invalid_login_info", ErrorCategory.PermissionDenied, null)); } var user = User.FromName(username, true); UserSwitcher.Enter(user); SessionState.PSVariable.Set("me", HttpContext.Current.User.Identity.Name); } }
private List <Item> GetSitemapItems(string rootPath, Language language) { Item[] descendants; string enabledTemplates = SitemapManagerConfiguration.EnabledTemplates; string excludeItems = SitemapManagerConfiguration.ExcludeItems; Database database = Factory.GetDatabase(SitemapManagerConfiguration.WorkingDatabase); Item item; if (language == null) { item = database.Items.GetItem(rootPath); } else { item = database.Items.GetItem(rootPath, language); } if (item != null) { using (UserSwitcher userSwitcher = new UserSwitcher(User.FromName("extranet\\Anonymous", true))) { descendants = item.Axes.GetDescendants(); } List <Item> list = descendants.ToList <Item>(); list.Insert(0, item); List <string> strs = this.BuildListFromString(enabledTemplates, '|'); List <string> strs1 = this.BuildListFromString(excludeItems, '|'); IEnumerable <Item> items = list.Where <Item>((Item itm) => { if (itm.Template == null || !strs.Contains(itm.TemplateID.ToString())) { return(false); } return(!strs1.Contains(itm.ID.ToString())); }); return(items.Where(x => x != null && x.Versions.Count > 0 && x.Versions.IsLatestVersion() && (CheckboxField)x.Fields[CommonConstants.IncludeinSitemap] != null && ((CheckboxField)x.Fields[CommonConstants.IncludeinSitemap]).Checked).ToList <Item>()); } else { return(new List <Item>()); } }
/// <summary> /// </summary> protected virtual void LoggedIn() { User user = Sitecore.Security.Accounts.User.FromName(this.fullUserName, false); State.Client.UsesBrowserWindows = true; LoggedInArgs loggedInArg = new LoggedInArgs() { Username = this.fullUserName, StartUrl = this.startUrl }; LoggedInArgs loggedInArg1 = loggedInArg; Pipeline.Start("loggedin", loggedInArg1); this.startUrl = loggedInArg1.StartUrl + "&code=" + tokenService.TokenEncoder(user.Name, new TimeSpan(0, 0, 5)); using (UserSwitcher userSwitcher = new UserSwitcher(user)) { Log.Audit(this, "Bot-auth", new string[0]); } }
public void ShouldApplySecurityCheck() { // Arrange using (var tree = new TTree()) { User user = AuthenticationManager.BuildVirtualUser("User", true); user.RuntimeSettings.IsAdministrator = false; var rules = new AccessRuleCollection { AccessRule.Create(user, AccessRight.ItemRead, PropagationType.Any, AccessPermission.Deny) }; const string Path = "/sitecore/content/home"; var item = tree.Database.GetItem(Path); item.Security.SetAccessRules(rules); // Act & Assert using (var switcher = new UserSwitcher(user)) { tree.Database.GetItem(Path).Should().BeNull(); } } }
public new void Process(LoggingInArgs args) { Assert.ArgumentNotNull(args, "args"); bool flag = false; NoAccessTo noAccessTo = NoAccessTo.Empty; using (UserSwitcher userSwitcher = new UserSwitcher(args.Username, true)) { switch (args.StartUrl) { case "/sitecore/shell/default.aspx": { Item item = Client.CoreDatabase.Items["/sitecore/content/Applications/Desktop"]; if (!Context.IsAdministrator && (item == null || !item.Access.CanRead())) { noAccessTo = NoAccessTo.Desktop; flag = true; } break; } case "/sitecore/shell/applications/clientusesoswindows.aspx": { Item item1 = Client.CoreDatabase.Items["/sitecore/content/Applications/Content Editor"]; if (item1 == null || !item1.Access.CanRead()) { noAccessTo = NoAccessTo.ContentEditor; flag = true; } break; } case "/sitecore/shell/applications/webedit.aspx": { Item item2 = Client.CoreDatabase.Items["/sitecore/content/Applications/WebEdit"]; if (item2 == null || !item2.Access.CanRead()) { noAccessTo = NoAccessTo.PageEditor; flag = true; } break; } } var membershipUser = Membership.GetUser(Sitecore.Context.User.Name, false); if (IsPasswordExpiredEnabled() && membershipUser != null && HasPasswordExpired(membershipUser)) { noAccessTo = NoAccessTo.PasswordExpired; UserName = args.Username; flag = true; } if (flag) { AuthenticationHelper authenticationHelper = new AuthenticationHelper(AuthenticationManager.Provider); if (!string.IsNullOrEmpty(args.Username) && args.Password != null && authenticationHelper.ValidateUser(args.Username, args.Password)) { args.Success = false; args.AddMessage(GetErrorMessage(noAccessTo)); args.AbortPipeline(); } } } }
public void SwitchToUser(string username, bool isAuthenticated) { var userSwitcher = new UserSwitcher(User.FromName(username, isAuthenticated)); }