2

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

4

1 回答 1

2

我自己想出了一些办法。

示例用法:

<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 
}

将它添加到您的安全控件中,您就可以开始使用了。

于 2013-01-23T08:53:00.610 回答