0
votes

I'm sorry for being such a dumb ass. I'm following a tutorial to add admin routing to my cakephp application that I'm trying to create as a learning exercise.

The tutorial isn't hugely well explained (I think I'm just too much of a beginner in reality) and I don't understand the following, could anyone please tell me in english what is happening here.

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);
    }
2
where is this tutorial you are using ? is it a public tutorial ? it'd be easier to follow (and possibly answer !)marc-andre benoit
I'm afraid it's in a book. There's only a little more to it anyway. Just changing the routing.prefix in the core.php, adding the auth component, create three blank functions in users_controller (dashboard, manager_dashboard, admin_dashboard) + their blank views. After that it's just adding the following in the default.ctp:BPD
$dashboardUrl = array('controller'=>'users', 'action'=>'dashboard'); if (!empty($user['role'])) { $dashboardUrl[$user['role']] = true; } echo $this->Html->link('My Dashboard', $dashboardUrl);BPD
i'm not an expert far from it! i have my own issues with cake ,which might end up as a question (or three) soon enough :)marc-andre benoit

2 Answers

1
votes

where u have probelm???

u can debug one by one

// This method provides information of role about the currently authenticated user.
  $role = $this->Auth->user('role'); 

// you first check with var_dump($this->params['prefix']) and see the result

/*
 * this line use ternary operator, its say $this->params['prefix'] is not empty 
 * then set $prefix = $this->params['prefix'] otherwise set $prefix=null
*/
  $prefix = !empty($this->params['prefix']) ? $this->params['prefix'] : null;

/*
 *Now check the array
 *echo "<pre>";
 * print_r(Configure::read('Routing.prefixes'));
 * echo "</pre>";   
 * now below line said if `$prefix` is not empty then search that `$prefix` 
 * value in this array `Configure::read('Routing.prefixes')` and if it 
 * exist in the array then set  `$neededRole = $prefix;
 */ 

if (!empty($prefix) && in_array($prefix, Configure::read('Routing.prefixes'))) {
            $neededRole = $prefix;  
        }
/* below line say say that if $role == admin then return $role or return $neededRole */
return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole)

Reference

Happy To Help :)

0
votes
public function isAuthorized() {

//fetches the current user's role (admin,user,editor,visitor,etc..)
$role = $this->Auth->user('role');
//assigns a null value
$neededRole = null;
//fetches the param for prefix and assigns to $prefix,if not found,assigns null
$prefix = !empty($this->params['prefix']) ? $this->params['prefix'] : null;
//if $prefix not null & if $prefix has route configured assign $prefix to $neededRole
if (!empty($prefix) && in_array($prefix, Configure::read('Routing.prefixes'))) { $neededRole = $prefix;
} return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole) == 0); }
//the rest i'm not too sure..