Skip to content

Instantly share code, notes, and snippets.

@anandnalya
Created November 16, 2011 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandnalya/1369451 to your computer and use it in GitHub Desktop.
Save anandnalya/1369451 to your computer and use it in GitHub Desktop.
Role based views using CSS
.role_admin, .role_user, .role_guest{
display:none;
}
$(document).ready(function() {
// We are assuming that jQuery and jQuery Cookie plugin are included in the doc
var role = $.cookie('user_role'); // Should return one of 'ADMIN', 'USER' OR 'GUEST'
// set property for relevant css class
var cssClass = '.role_'+ role.toLowerCase();
$("<style type='text/css'> "+ cssClass + "{ display:block } </style>").appendTo("head");
});
$(document).ready(function() {
// We are assuming that jQuery and jQuery Cookie plugin are included in the doc
var role = $.cookie('user_role'); // Should return one of 'ADMIN', 'USER' OR 'GUEST'
// set property for relevant css class
if(role == "ADMIN") {
// make everything visible
$("<style type='text/css'>.role_admin, .role_user, .role_guest {display:block} </style>").appendTo("head");
} else if(app.role == "USER") {
// make user and guest part visible, admin part will remain invisible
$("<style type='text/css'>.role_user, .role_guest {display:block} </style>").appendTo("head");
} else if(app.role == "GUEST") {
// only show the guest part
$("<style type='text/css'>.role_guest {display:block} </style>").appendTo("head");
}
});
<html>
<head>
<link href="./role.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 class="role_admin">This should be visible to admin only.<h1>
<h2 class="role_user">This is for the normal logged in user.</h1>
<h3 class="role_guest">And this is for the guest.</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment