Checking User Logged in Role

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
8/8/2009 11:26:55 AM
Gravatar
Total Posts 74

Checking User Logged in Role

How can I check to see what role a user is logged in with? I have an asp:panel that I only want to show if the user is an admin. I thought it would be something like this...

<script>

if(User.IsInRole("Administrator"))
{
adminpanel.visible="true"
}

</script>

I put that at the top of my layout.master, but it throws off the style sheets for some reason. Everything goes out of alignment.

Is that the best way to check the users role? Why would that code change my layout?

 

Thanks,
Joe

8/8/2009 12:55:34 PM
Gravatar
Total Posts 18439

Re: Checking User Logged in Role

Hi,

You should not put any code directly in the layout.master file. Instead create a UserControl with your logic and then just declare the UserControl in the layout.master. 

You can check the if the current user is in a role using WebUser.IsInRole(...) or WebUser.IsInRoles(takes a semi colon separated list of roles)

Note that roles have a RoleName and a DisplayName in the db and you have to check agaisnt the RoleName which is not always the same as the display name. In this case the role name is Admins while the disoplay name is Administrators.

Hope it helps,

Joe

8/10/2009 12:10:09 PM
Gravatar
Total Posts 218

Re: Checking User Logged in Role

Joe helped me build something similar and I'll share how that works here. I wanted a custom admin panel that only appeared at the top of the page when an admin was logged in. Here's how to do this:

In layout.master put something like this at the top (choose your own path to the file)
<%@ Register TagPrefix="pad" TagName="AdminNav" Src="~/_Custom/AdminBar.ascx" %>

In layout.master add this in place of the admin/editor links:
<div id="AdminPanel" runat="server" class="topnav" style="background-color: #FFFFCC; border: 1px dashed #C0C0C0; width: 99%; padding: 7px; margin: 0px">
<pad:AdminNav id="an1" runat="server"></pad:AdminNav>
<asp:ContentPlaceHolder id="pageEditContent" runat="server"></asp:ContentPlaceHolder>&nbsp;&nbsp;&nbsp; </div>

Here's the code in that AdminBar.ascx file:

<%@ Control Language="C#" AutoEventWireup="true" ClassName="AdminBar.ascx" Inherits="System.Web.UI.UserControl" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="mojoPortal.Business" %>
<%@ Import Namespace="mojoPortal.Business.WebHelpers" %>
<%@ Import Namespace="mojoPortal.Web.Framework" %>
<%@ Import Namespace="mojoPortal.Web.Controls" %>
<%@ Import Namespace="mojoPortal.Web.Editor" %>
<%@ Import Namespace="mojoPortal.Net" %>

<script runat="server">
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}

protected void Page_Load(object sender, EventArgs e)
{
if(!WebUser.IsAdmin){ this.Visible = false;}
}
</script>

<ul>
<portal:WelcomeMessage id="WelcomeMessage" runat="server" ListItemCSS="firstnav" RenderAsListItem="true" />
<portal:HomeLink id="HomeLink" runat="server" RenderAsListItem="true" />
<portal:SiteMapLink id="SiteMapLink2" runat="server" CssClass="sitelink" RenderAsListItem="true" />
<portal:MyPageLink id="MyPageLink1" runat="server" RenderAsListItem="true" />
<portal:UserProfileLink id="UserProfileLink" runat="server" RenderAsListItem="true" />
<portal:MailboxLink id="MailboxLink1" runat="server" RenderAsListItem="true" />
<portal:RegisterLink id="RegisterLink" runat="server" RenderAsListItem="true" />
<portal:MemberListLink id="MemberListLink" runat="server" RenderAsListItem="true" />
<portal:SearchInput id="SearchInput1" LinkOnly="True" RenderAsListItem="true" runat="server" />
<portal:LoginLink id="LoginLink" runat="server" RenderAsListItem="true" />
<portal:LogoutLink id="LogoutLink" runat="server" RenderAsListItem="true" />
</ul>
<style type="text/css">div.topnav {display:block;}</style>

 

This apparently is how you display/show content in the layout.master file based on role. It's also a slick little admin bar for the top of your site so you don't need that "login" and other links to always display. My clients woulnd't like that being right on their public web sites for example.

How this helps someone.

8/11/2009 3:34:32 PM
Gravatar
Total Posts 74

Re: Checking User Logged in Role

Thanks guys! Those were both very helpful!

You must sign in to post in the forums. This thread is closed to new posts.