How To Hide Part Of Html Form Depending On User Role
I am developing a website using Kohana 3.3 and i want to selectively display HTML UI elements depending on the role of the user. e:- If user is an admin then show the 'edit' hyperl
Solution 1:
Ok, so you want to distinguish three different cases
- visitor
- admin
- user
The place to handle this, is your controller. In this you have access to Auth::instance()->get_user()
.
$user = Auth::instance()->get_user();
if ($user === null) {
//visitor
} else {
if ($user->has('roles', ORM::factory('Role', array('name' => 'admin')))) {
//admin
} else {
//user
}
}
Now that you know how to handle the cases, you somehow need to tell your view. To do that, you can create a new variable in which you load either one of three views - one for each case.
$specificViewName = "";
$user = Auth::instance()->get_user();
if ($user === null) {
$specificViewName = "visitor";
} else {
if ($user->has('roles', ORM::factory('Role', array('name' => 'admin')))) {
$specificViewName = "admin";
} else {
$specificViewName = "user";
}
}
$specificView = View::factory("index/".$specificViewName);
If you are in a Controller_Template
, you can now use $this->template->set("specificView", $specificView);
.
In this case you'd have a index template like this
<html><!--etc.--><h1>Welcome to my website</h1><!--stuff all sites share like navigation--><?phpprint$specificView; ?><!--more--></html>
And index/visitor
<spanclass="sadtext">Nothing special for you here</span>
index/user
<form>
<button>ask a question!
</form>
index/admin
<ahref="edit">hyperlink</a>
Post a Comment for "How To Hide Part Of Html Form Depending On User Role"