|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV SYS-CON.TV WEBCASTS |
MXDJ TOP LINKS YOU MUST CLICK ON ! Community Corner
Show and Hide Content Based on User Access Levels
Dreamweaver can help
By: Danilo Celic
Dec. 22, 2006 02:45 PM
Digg This!
Dreamweaver's native Log In User server behavior combined with the Restrict Access to Page server behavior can help you protect your pages from prying eyes. However, when it comes for more fine-grained control of content on pages viewable by users from multiple access levels, Dreamweaver doesn't have anything built in to offer any assistance to you.
As part of the CMS that you're building, you might have a control panel page that contains the main navigation links for common tasks performed by all users, such as updating the user password and contact information. All of this is easily handled by the Restrict Access to Page server behavior so that all logged in users can see your common content. If you have links to pages that have special significance only, for example, the admin users such as approving articles, you'd probably rather not create a special log in just for admin users, and you'd rather not show links that user access levels shouldn't see, and probably can't even visit if you're properly restricting access to those pages using the Restrict Access To page server behavior. Note: To read up on using the server behaviors mentioned above, take a look at Using the Log in Server Behavior (www.communitymx.com/content/article.cfm?cid=A222302CBCA928EB) and Access Level and Login for PHP (www.communitymx.com/abstract.cfm?cid=78EEB) and ASP (www.communitymx.com/abstract.cfm?cid=DFB68). Or check out our Liverpool JumpStart (www.communitymx.com/abstract.cfm?cid=3777A), which contains a page set design that includes password-protected pages. So how do you balance the two competing needs: a main control panel page that displays common links and also displays links that only specified users can access? One way to do this would be to analyze the code Dreamweaver uses to determine if a user can log in, or check out the code that is used to determine if a logged-in user can access a restricted page. We've taken a close look at these server behaviors for you and determined that Dreamweaver's Log In User and Restrict Access To Page server behaviors write code to your page that use session variables to maintain information about a user if they are logged in, and what access level they have assigned to them when logged in. Note: CF users make sure you have sessionmanagement turned on in your Application.cfm to enable sessions. For an article on doing this, check out: "Enabling Session Variables in ColdFusion" (www.communitymx.com/content/article.cfm?cid=62595). ASP and ColdFusion use a session variable named MM_UserAuthorization and PHP uses a session variable named MM_UserGroup to identify the access level for a logged-in user. So checking that a user's MM_UserAuthorization, or MM_UserGroup, value is part of a list of valid access levels (or alternately checking that their access level is not part of the list) will help you determine if you need to show or hide a particular piece of content. In the sample code, we're using Access levels for our users where 1 = System Admin, 2= Admin, 3= Users. ASP VBScript does things a little differently than ColdFusion and PHP, so we'll tackle PHP and ColdFusion together and then get on to VBScript further down the article. The sample pages within the support files contain pages written in ColdFusion, PHP and ASP VBScript that show content based upon the access level examples. You can use the included Access MDB file to test with the .sql file to create your own MySQL table.
Showing Content for ColdFusion and PHP
ColdFusion: If you want to show content to System Admins (1) and to Admin (2) users, wrap your content with the following:
ColdFusion: Please note that in the code being used here Coldfusion uses a quotes comma separated string such as "1,2" and PHP uses comma-separated quotes strings as in "1","2".
Hiding Content for ColdFusion and PHP
ColdFusion: Please note: The hiding for PHP is handled a little differently than the showing is. The array_search function returns the key of the item if it exists in the array of values, however, it returns FALSE when it doesn't find the value. As such, when checking for a value to not be within the array, you have to use the Identical operator (===) to check that the value is FALSE. If you just used the equal operator as in if(array_search($validLevel, $accessLevels)==FALSE), that could equate to a true statement if the value *is* found within the array at the zeroth element in the array. This is because 0 is equivalent to FALSE, however, the Identical operator can handle the difference between 0 and FALSE To hide content when a user is System Admins (1) or a Admin (2) users, wrap your content with the following:
ColdFusion:
ASP VBScript and Searching Arrays
InArray Function
-1. Tip: Place the InArray function into an include that contains your commonly used functions.
Showing Content for ASP VBScript
<% If you want to show content to System Admins (1) and to Admin (2) users, wrap your content with the following:
<%
Hiding Content for ASP VBScript
<% To hide content when a user is System Admins (1) or a Admin (2) users, wrap your content with the following:
<%
Conclusion Tip: Add these short bits of code to your Snippets panel for easy access to your access level show hide code. For more info on using the Snippets panel, check out: Exploring the Snippets Panel in Dreamweaver (www.communitymx.com/abstract.cfm?cid=AB7D1). Happy Coding! LATEST FLEX STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||