Page 1 of 1

only show link if you are in a certain group

Posted: Wed Oct 14, 2009 1:48 pm
by joey
I would like to know how to show a link on the "My Acount" page to only show if you are in a certain group.

I was doing this with the htm pages...

Code: Select all

#if($customer.IsInGroup(18))
       <li><a class="rounded" href="#">Windows Server 2008</a></li>
#end
but obviously it will not work in my MyAccount.ascx page.

How can i have that link only show if they are in group 18 on the account page?

Re: only show link if you are in a certain group

Posted: Thu Oct 15, 2009 3:56 am
by mazhar
In ascx code place ASP.NET hyperlink on desired location like

Code: Select all

<asp:Hyperlink ID="MyLink" runat="server" NavigateUrl="#" Text="Windows Server 2008" />
and then in its code file in load or init method

Code: Select all

MyLink.Visible = Token.Instance.User.IsInGroup(18);

Re: only show link if you are in a certain group

Posted: Thu Oct 15, 2009 1:03 pm
by joey
thanks for that, how can i show a div if the user is not in any group?

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 11:04 am
by joey
i have already found an issue with the way i am doing this. I had a list who's list items contained asp link, if the user was not in the group the link would show but an empy <li></li> still appears on the page. I know this is because I'm putting the links inside a <li></li>. I guess I should have (and I'm going to) ask how to show a list item is the user in a certain group?

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 11:07 am
by joey
here is my code

Code: Select all

<ul class="left-nav">
	<li><asp:Hyperlink ID="IntroWinServer2008" class="rounded" runat="server" NavigateUrl="#" Text="Intro to Windows Server 2008" /></li>
	<li><asp:Hyperlink ID="WinServer2008" class="rounded" runat="server" NavigateUrl="#" Text="Windows Server 2008" /></li>
	<li><asp:Hyperlink ID="WinServer2008r2" class="rounded" runat="server" NavigateUrl="#" Text="Windows Server 2008 R2" /></li>
	<li><asp:Hyperlink ID="WinServer2008ActDir" class="rounded" runat="server" NavigateUrl="#" Text="Windows Server 2008 Active Directory" /></li>
	<li><asp:Hyperlink ID="SQLServer2008DatabaseAdminstrator" class="rounded" runat="server" NavigateUrl="#" Text="SQL Server 2008 Database Adminstrator" /></li>
	<li><asp:Hyperlink ID="SQLServer2008DatabaseDevelopment" class="rounded" runat="server" NavigateUrl="#" Text="SQL Server 2008 Database Development" /></li>
	<li><asp:Hyperlink ID="VMwarevSphere" class="rounded" runat="server" NavigateUrl="#" Text="VMware vSphere" /></li>
	<li><asp:Hyperlink ID="VMwareOnline" class="rounded" runat="server" NavigateUrl="#" Text="VMware Online Training" /></li>
	<li><asp:Hyperlink ID="HyperV" class="rounded" runat="server" NavigateUrl="#" Text="Hyper V" /></li>
</ul>

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 11:40 am
by joey
i figured it out... here is what it should look like (i think).

here is the ascx.cs code

Code: Select all

  liIntroWinServer2008.Visible = Token.Instance.User.IsInGroup(9);
			liWinServer2008.Visible = Token.Instance.User.IsInGroup(18);
			liWinServer2008r2.Visible = Token.Instance.User.IsInGroup(30);
			liWinServer2008ActDir.Visible = Token.Instance.User.IsInGroup(10);
			liSQLServer2008DatabaseAdminstrator.Visible = Token.Instance.User.IsInGroup(12);
			liSQLServer2008DatabaseDevelopment.Visible = Token.Instance.User.IsInGroup(60);
			liVMwarevSphere.Visible = Token.Instance.User.IsInGroup(50);
			liVMwareOnline.Visible = Token.Instance.User.IsInGroup(59);
			liHyperV.Visible = Token.Instance.User.IsInGroup(11);
here is the ascx code

Code: Select all

<ul class="left-nav">
			<li ID="liIntroWinServer2008" runat="server">IntroWinServer2008</li>
			<li ID="liWinServer2008" runat="server">2</li>
			<li ID="liWinServer2008r2" runat="server">3</li>
			<li ID="liWinServer2008ActDir" runat="server">4</li>
			<li ID="liSQLServer2008DatabaseAdminstrator" runat="server">5</li>
			<li ID="liSQLServer2008DatabaseDevelopment" runat="server">6</li>
			<li ID="liVMwarevSphere" runat="server">7</li>
			<li ID="liVMwareOnline" runat="server">8</li>
			<li ID="liHyperV" runat="server">9</li>
		</ul>
i had to give the id to the <li> and also add the runat="server"

so far i think it works

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 11:44 am
by William_firefold
I was about to suggest exactly that.

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 11:57 am
by joey
well i feel better knowing that i did it correctly. sometimes things work even though they aren't done right.

I really need to figure out how to display a message (in a div) if the user is not part of any groups.

if there are no subscriptions than they will see a message "You do not have subscriptions"

Re: only show link if you are in a certain group

Posted: Fri Oct 16, 2009 12:43 pm
by joey
i should rephrase this...

if the user is not in any group they will see a message "You do not have subscriptions"

Re: only show link if you are in a certain group

Posted: Mon Oct 19, 2009 8:26 am
by William_firefold
something like this, though i havent tested syntax.

Code: Select all

        CommerceBuilder.Users.Group groups = GroupDataSource.LoadForStore();
		MyLink.Visible = (!Token.Instance.User.IsInGroup(groups));

Re: only show link if you are in a certain group

Posted: Mon Oct 19, 2009 9:42 am
by joey
thanks for the help, it didn't work, it gives me an error

Code: Select all

error CS0029: Cannot implicitly convert type 'CommerceBuilder.Users.GroupCollection' to 'CommerceBuilder.Users.Group'
i'm trying to show a div that contains a message if the user is not in any groups.

if the users are assigned to a group they have access to private pages. I have customized the my account page to show a link list of the private pages they have access to. The only problem is, if the user doesn't have access to any pages (the user is not in any groups) they need to see a message saying "you do not have access to private pages", this way, there isn't a header saying "private pages" without and links below it. It will show the message instead.

Re: only show link if you are in a certain group

Posted: Tue Oct 20, 2009 8:17 am
by William_firefold
Able documentation says it should work for the collections, but i guess its not right somehow.
IsInGroup(Int32) Determines whether a user is in the given group.
IsInGroup(ICollection<(Of <(Int32>)>)) Determines whether a user is in any of the given groups.

You could always cycle through them:

Code: Select all

boolean checkGroup=false;
  CommerceBuilder.Users.Group groups = GroupDataSource.LoadForStore();
foreach(CommerceBuilder.Users.Group group in groups){
if(Token.Instance.User.IsInGroup(group)){
checkGroup=true;
}
}
but you may not want to do that every pageload. Maybe just make a special group for everyone, and you can assign the group when they register. Then you can just check to see if they are in that group, instead of checking to see if they arent in all the others.

Re: only show link if you are in a certain group

Posted: Mon Oct 26, 2009 2:02 pm
by joey
so there's no way to say "IsNotInGroup" or something? Of course it wouldn't be that easy. Thanks for your help William, I'm afraid I just can't get it to work any way I try.