sharepoint - SPSecurityTrimmedControl 修剪内容时如何显示其他内容

标签 sharepoint sharepoint-designer

使用 SPSecurityTrimmedControl 隐藏/显示内容很简单。当 SPSecurityTrimmedControl 的条件满足时,是否有办法显示其他内容?我的品牌网站应该根据用户角色交换内容。 重写行为并不能解决问题,因为 ShouldRender 方法是内部方法并控制 Render 方法的行为。

最佳答案

我自己想出了一些办法。

示例用法:

<uc:TemplatedSecurityTrimmedControl runat="server" Permissions="ManageLists">
   <Authorised>
     authorised content goes here
   </Authorised>
   <Unauthorised>
      unauthorised content goes here
   </Unauthorised>
</uc:TemplatedSecurityTrimmedControl>

代码:

/// <summary>
/// Templated version of the SPSecurityTrimmedControl
/// </summary>
[Bindable(false), ParseChildren(true), PersistChildren(false),
SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), 
AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), 
SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true), 
AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class TemplatedSecurityTrimmedControl : SPSecurityTrimmedControl, INamingContainer
{
    #region Fields (1) 

    private bool _visible = true;

    #endregion Fields 

    #region Properties (3) 

    /// <summary>
    /// Content container for authorised users
    /// </summary>
    [Browsable(false),
    DefaultValue(null),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplatedSecurityTrimmedControl))]
    public ITemplate Authorised { get; set; }

    /// <summary>
    /// Content container when user is unauthorised
    /// </summary>
    [Browsable(false),
    DefaultValue(null),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplatedSecurityTrimmedControl))]
    public ITemplate Unauthorised { get; set; }

    /// <summary>
    /// Visible state
    /// </summary>
    /// <remarks>
    /// base.Visible uses the ShouldRender() state. 
    /// If ShouldRender() returns false then Visible returns false.
    /// Though I do want the unauthorised template to be visible.
    /// </remarks>
    public override bool Visible
    {
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        get
        {
            return _visible;
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        set
        {
            _visible = value;
        }
    }

    #endregion Properties 

    #region Methods (5) 

    // Public Methods (2) 

    /// <summary>
    /// Control doesn't have a begin tag
    /// </summary>
    /// <param name="writer"></param>
    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        return;
    }

    /// <summary>
    /// Control doesn't have an end tag
    /// </summary>
    /// <param name="writer"></param>
    public override void RenderEndTag(HtmlTextWriter writer)
    {
        return;
    }
    // Protected Methods (3) 

    /// <summary>
    /// Create child controls
    /// </summary>
    protected override void CreateChildControls()
    {
        Controls.Clear();

        // Decide which template to render
        ITemplate template = ShouldRender() ? Authorised : Unauthorised;

        // Add template to child controls
        if (template != null)
        {
            Control container = new Control();
            template.InstantiateIn(container);
            Controls.Add(container);
        }

        base.CreateChildControls();
    }

    /// <summary>
    /// Render control
    /// </summary>
    /// <param name="writer"></param>
    [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
    protected override void Render(HtmlTextWriter writer)
    {
        EnsureChildControls();

        // Suppress SPSecurityTrimmedControl.Render behavior by calling inner render operations directly
        this.RenderBeginTag(writer);
        this.RenderContents(writer);
        this.RenderEndTag(writer);
    }

    /// <summary>
    /// Decides if authorised content should be rendered
    /// </summary>
    /// <remarks>
    /// base.ShouldRender is internal which makes it inaccessible. ty ILSpy.
    /// </remarks>
    protected virtual bool ShouldRender()
    {

        return 
            RightsSensitiveVisibilityHelper.UserHasRights(this.PermissionContext, this.Permissions, this.PermissionMode, this.RenderContext, null, null) 
            && (this.PageModes == PageModes.All || (PageModes.Normal & this.PageModes) != (PageModes)0) 
            && (this.AuthenticationRestrictions == AuthenticationRestrictions.AllUsers 
                || ((HttpContext.Current.Request.IsAuthenticated ? AuthenticationRestrictions.AuthenticatedUsersOnly : AuthenticationRestrictions.AnonymousUsersOnly) & this.AuthenticationRestrictions) != (AuthenticationRestrictions)0);
    }

    #endregion Methods 
}

将其添加到您的安全控件中,然后您就可以开始了。

关于sharepoint - SPSecurityTrimmedControl 修剪内容时如何显示其他内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14442526/

相关文章:

c# - Sharepoint 客户端对象模型语言环境

sharepoint - SharePoint 工作流中未触发延迟事件

c# - SharePoint 2013 测试登录用户

mysql - Sharepoint Designer 2007 ODBC 数据库无法连接到 MySQL?

list - 如何使用rest API获取基于不同ID的记录?

c# - 如何显示标准 SharePoint "Access Denied"消息

rest - 尝试使用用户配置文件 REST API 访问用户属性

sharepoint - 如何清除 SharePoint Designer 缓存?

CSS 背景图像在 SP 2013 中不工作,但在本地工作

asp.net - SharePoint 2013 中的设计页面和母版页的工具