locked
RoleView - role based page content RRS feed

  • Question

  • User-1741990555 posted
    /* * RoleView control for use with Role feature * Don't forget to register tag prefix on the page for the code to work <%@ Register TagPrefix="variatron" Namespace="Variatron" %> Example 1 <variatron:RoleView ID="RoleView_Registered" runat="server" RoleFilter="*"> If you registered, you should see this </variatron:RoleView> <variatron:RoleView ID="RoleView_Managers" runat="server" RoleFilter="manager"> This is for managers
    </variatron:RoleView> <variatron:RoleView ID="RoleView_Staff" runat="server" RoleFilter="manager,editor"> This is for employees
    </variatron:RoleView> <variatron:RoleView ID="RoleView_Advertising" runat="server" RoleFilter="?,tester"> Something all users can see
    </variatron:RoleView> <variatron:RoleView ID="RoleView_OnlyAnonimous" runat="server" RoleFilter="?"> By the way, you can register... </variatron:RoleView> Example 2 <fieldset> <legend>User</legend><variatron:RoleView ID="RoleView_registered" runat="server" RoleFilter="*"> <asp:LoginName ID="LoginName1" runat="server"></asp:LoginName>
    <%=(string)Profile["name"] %>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/profile/">my personal data</asp:HyperLink></variatron:RoleView> <asp:LoginStatus ID="LoginStatus2" runat="server" meta:resourcekey="LoginStatus2Resource1" /> </fieldset> <variatron:RoleView ID="RoleView_unknown" runat="server" RoleFilter="?"> <small>new users:</small> register </variatron:RoleView> */ using System.Web.UI.WebControls; using System; using System.Collections; using System.Web.Security; namespace Variatron { public class RoleView : Panel { private string m_roles = ""; public virtual string RoleFilter { get { return m_roles; } set { m_roles = value; } } public override bool Visible { get { if (DesignMode) { return base.Visible; } else { return base.Visible && this.IsInAnyRole; } } set { base.Visible = value; } } public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer) { } public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer) { } protected override void OnInit(EventArgs e) { ViewState.Clear(); } private bool IsInAnyRole { get { string[] separators ={ "," }; ArrayList ar = new ArrayList( RoleFilter.Split( separators, StringSplitOptions.RemoveEmptyEntries) ); ArrayList user_roles = new ArrayList(Roles.GetRolesForUser()); bool is_in_a_role = false; if (ar.Contains("?") && user_roles.Count == 0) { is_in_a_role = true; } else { if (ar.Contains("*") && user_roles.Count != 0) { is_in_a_role = true; } else { foreach (string role in user_roles) { if (ar.Contains(role)) { is_in_a_role = true; break; } } } } return is_in_a_role; } } } }
    Saturday, February 11, 2006 12:13 PM

All replies

  • User-1741990555 posted
    Example usage

    Don't forget to register tag prefix on the page for the code to work
    <%@ Register TagPrefix="variatron" Namespace="Variatron" %>

    Example 1

    <variatron:RoleView ID="RoleView_Registered" runat="server" RoleFilter="*">
    If you registered, you should see this
    </variatron:RoleView>
    <variatron:RoleView ID="RoleView_Managers" runat="server" RoleFilter="manager">
    This is for managers<br />
    </variatron:RoleView>
    <variatron:RoleView ID="RoleView_Staff" runat="server" RoleFilter="manager,editor">
    This is for employees<br />
    </variatron:RoleView>
    <variatron:RoleView ID="RoleView_Advertising" runat="server" RoleFilter="?,tester">
    Something all users can see<br />
    </variatron:RoleView>
    <variatron:RoleView ID="RoleView_OnlyAnonimous" runat="server" RoleFilter="?">
    By the way, you can register...
    </variatron:RoleView>

    Example 2

    <fieldset>
    <legend>User</legend><variatron:RoleView ID="RoleView_registered" runat="server" RoleFilter="*">
    <asp:LoginName ID="LoginName1" runat="server"></asp:LoginName><br /><%=(string)Profile["name"] %><br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/profile/">my personal data</asp:HyperLink></variatron:RoleView>
    <asp:LoginStatus ID="LoginStatus2" runat="server" meta:resourcekey="LoginStatus2Resource1" />
    </fieldset>
    <variatron:RoleView ID="RoleView_unknown" runat="server" RoleFilter="?">
    <small>new users:</small>&nbsp;<a id="a1" href="~/register.aspx" runat="server">register</a>
    </variatron:RoleView>


    Sunday, February 12, 2006 7:24 PM