This is part of Code Advent 2016
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require jdmaymeow/cake-auth
Load plugin
bin/cake plugin load CakeAuth -r
To AppController Initialize add
$this->loadComponent('Auth', [
'authorize' => ['Controller'], // Added this line
'loginRedirect' => [
'controller' => 'Links',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login',
'home'
]
]);
Add after initialize function into main APpControler
public function beforeFilter(Event $event)
{
$this->Auth->allow(['index', 'view', 'display']);
}
public function isAuthorized($user)
{
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
Allowed actions
Role | Login | Logout | Index | Add | Edit | View | Delete | Display |
---|---|---|---|---|---|---|---|---|
Author | Yes | Yes | Yes | Yes | No | Yes | No | Yes |
Admin | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes |