can someone please put this in english for me PHP Cakephp
Sorry for being a bit of a dumb-ass but I'm trying to learn and have been looking at this for a couple of days but I'm still not completely sure how it works. I'm following an offline tutorial about using prefixes.
the tutorial says do this: 1 - uncomment the admin routing prefix in core.php
2 - add the following to users_controller.php:
public function dashboard() {
$role = $this->Auth->user('role');
if (!empty($role)) {
$this->redirect(array($role=>true, 'action'=>'dashboard'));
}
}
public function admin_dashboard() {
}
public function manager_dashboard() {
}
3 - create three views for the above
4 - in app_controller add the authorize and session components with appropriate settings, then add:
public function isAuthorized() {
$role = $this->Auth->user('role');
$neededRole = null;
$prefix = !empty($this->params['prefix']) ? $this-
>params['prefix'] : null;
if (!empty($prefix) && in_array($prefix,
Configure::read('Routing.prefixes'))) {
$neededRole = $prefix;
}
return (empty($neededRole) || strcasecmp($role, 'admin') == 0 ||
strcasecmp($role, $neededRole) == 0);
}
5 - in the default layout:
<?php
$dashboardUrl = array('controller'=>'users', 'action'=>'dashboard');
if (!empty($user['role'])) {
$dashboardUrl[$user['role']] = true;
}
echo $this->Html->link('My Dashboard', $dashboardUrl);
?>
Now the whole thing works and I can follow most of whats going on, but I'm confused with the last line in the isAuthorized method. What exactly does:
return (empty($neededRole) ||
strcasecmp($role, 'admin')== 0 ||
strcasecmp($role, $neededRole) == 0);
actually return?
Also, the above code works fine and when I log in as someone who is an admin they can view the admin_dashboard ok, but there after it seems any other link has an admin prefix. How do I stop that happening? I don't want to have to create an admin_ method for everything including the homepage!.
empty($neededRole) ||
strcasecmp($role, 'admin') == 0 ||
strcasecmp($role, $neededRole) == 0
The needed role is blank/empty: empty($neededRole) OR
the role is 'admin' when compared in a case-insensitive manner: strcasecmp($role, 'admin') == 0 OR
the role is the same as the needed role when compared in a case-insensitive manner.
So it returns true if it doesn't require a role, or the role is admin, or the role is the same as the required role, otherwise false.
To address the first part of your question...
return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole) == 0);
This returns a boolean. Re-arrange it like this to make a little more sense:
return (
empty($neededRole)
|| strcasecmp($role, 'admin') == 0
|| strcasecmp($role, $neededRole) == 0
);
Thus, it will return true if any of the three conditions are met:
empty($neededRole)
is true
strcasecmp($role, 'admin')
equals 0 strcasecmp($role, $neededRole)
equals 0 According to the documentation, those last two will equal zero if the two arguments are the same. So, the function will return true
if:
$neededRole
value or; $role
is the same as "admin" or; $role
if the same as $neededRole
If none of those conditions are met, it will return false
.
From the documentation http://php.net/manual/en/function.strcasecmp.php strcasecmp
compares to strings ignoring case, returning 0 if they are the same, so the final line says:
If the needed role is empty (you don't need a role)
You are authorized
OR IF your role is 'admin'
You are authorized
OR IF your role is the needed role
You are authorized
ELSE
You are not authorized
As for your other question, i'm not sure, but i'm pretty sure you do need an admin_*
method, but these methods could delegate to the normal method where no special logic is needed.
上一篇: CakePHP了解本地化..?