2
使用SPSecurityTrimmedControl隱藏/顯示內容很簡單。有沒有辦法顯示其他內容,當SPSecurityTrimmedControl的條件是而不是遇到?我的品牌網站應根據用戶角色交換內容。 重寫行爲並沒有這樣做,因爲ShouldRender方法是內部的並且控制着Render方法的行爲。如何在SPSecurityTrimmedControl修剪內容時顯示其他內容
使用SPSecurityTrimmedControl隱藏/顯示內容很簡單。有沒有辦法顯示其他內容,當SPSecurityTrimmedControl的條件是而不是遇到?我的品牌網站應根據用戶角色交換內容。 重寫行爲並沒有這樣做,因爲ShouldRender方法是內部的並且控制着Render方法的行爲。如何在SPSecurityTrimmedControl修剪內容時顯示其他內容
我想出了一些我自己。
示例用法:
<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
}
它添加到您safecontrols,你準備好去。