2013-10-18 37 views
1

目前我想安全修剪全球导航,使用xmlsitemapprovider跨越所有网站集的自定义全局导航。一切工作正常,但是因为我使用web.allusers它不显示节点,除非明确授予用户访问权限。由于我的大部分权限都基于Active Directory组,因此用户在访问该站点之后才能看到这些节点。如何让这些用户无需首先访问该网站即可显示这些节点?SharePoint 2010中的安全调整与web.allusers

public class CustomNavSecurityTrim : XmlSiteMapProvider 
{ 

    public override bool IsAccessibleToUser(HttpContext context, SiteMapNode node) 
    { 
     try 
     { 
      if (node == null) 
      { 
       throw new ArgumentNullException("node"); 
      } 
      if (context == null) 
      { 
       throw new ArgumentNullException("context"); 
      } 
      if (!base.SecurityTrimmingEnabled) 
      { 
       return true; 
      } 
      if (string.IsNullOrEmpty(node.Url)) 
      { 
       return this.IsGranted(context, node.ChildNodes); 
      } 
      return this.IsGranted(context, node); 
     } 
     catch 
     { 
      return false; 
     } 
    } 


    private bool IsGranted(HttpContext context, SiteMapNode node) 
    { 
     bool isGranted = false; 
     SPSecurity.RunWithElevatedPrivileges(delegate 
     { 
      using (SPWeb web = new SPSite(SPContext.Current.Site.MakeFullUrl(node.Url)).OpenWeb()) 
      { 
       SPUser user = web.AllUsers[context.User.Identity.Name]; 
       if (user != null) 
       { 
        try 
        { 
         if ((node.Roles != null) && (node.Roles.Count > 0)) 
         { 
          foreach (string str in node.Roles) 
          { 
           isGranted = (str == "*") || (user.Groups[str] != null); 
           if (isGranted) 
           { 
            break; 
           } 
          } 
         } 
         isGranted = web.DoesUserHavePermissions(user.LoginName, SPBasePermissions.EmptyMask | SPBasePermissions.ViewPages); 
        } 
        catch 
        { 
        } 
       } 
      } 
     }); 
     return isGranted; 
    } 

    private bool IsGranted(HttpContext context, SiteMapNodeCollection childNodes) 
    { 
     bool flag = false; 
     foreach (SiteMapNode node in childNodes) 
     { 
      flag = this.IsGranted(context, node); 
      if (flag) 
      { 
       return flag; 
      } 
      this.IsGranted(context, node.ChildNodes); 
     } 
     return false; 
    } 
} 

}

回答

0

我怎样才能得到的节点,以显示这些用户没有他们首先必须访问的网站?

您必须为每个新用户拨打SPWeb.EnsureUser()。此方法会使SharePoint'检索'给定用户的帐户并使其对SharePoint“预先知晓”。


但是,更重要的是,您应该对您的解决方案进行性能测试。您提升权限,并在每次调用IsGranted(),这又多次调用打开一个新的SPWeb实例。这可能会导致严重的性能瓶颈。

我会建议重新考虑你正在努力实现的目标(这我就不详细从你的问题越来越)是更加符合SharePoint的架构。

+0

我有超过45网站集,我试图实现全球导航利用xmlsitemapprovider,我需要是安全通过SharePoint权限修剪。你能提供一个如何在这个解决方案中使用'EnsureUser'的例子吗? – tunacode

+1

的Ondrej讨论和关注的问题是,如果你实现你的导航像这样和命令执行**为每个用户** **每次**,你将有性能瓶颈。您需要实施某种缓存,因此访问权并不总是被重新计算。 –

+0

有没有一种方法可以在使用PowerShell时创建用户'EnsureUser'?我将如何缓存用户的访问权限?我并不是要求你写代码,但如果你能提供一个例子或可能的解决方案,我会非常感激。谢谢! – tunacode