コード例 #1
0
 /// <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();
     }
 }
コード例 #2
0
        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;
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        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();
        }
コード例 #6
0
 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;
 }
コード例 #7
0
ファイル: PushContext.cs プロジェクト: boro2g/sinj
        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));
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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>());
            }
        }
コード例 #10
0
        /// <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]);
            }
        }
コード例 #11
0
ファイル: TestBasicOperations.cs プロジェクト: zigor/phantom-
        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();
            }
              }
        }
コード例 #12
0
        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();
                    }
                }
            }
        }
コード例 #13
0
 public void SwitchToUser(string username, bool isAuthenticated)
 {
     var userSwitcher = new UserSwitcher(User.FromName(username, isAuthenticated));
 }