I think if I had to do it, I'd do it in controller. In cakephp we can easily choose authenticate options including basic auth.
class AppController extends Controller
{
public function initialize() {
parent::initialize();
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => [
// username and password from database
'username' => 'email',
'password' => 'password'
],
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
/**
* Allowed actions
* ['Controller' => ['method' => 1, 'method' => 1]]
*/
$excluded_actions = [
'Users' => [
'index' => 1,
'edit' => 1
]
];
$controller = $this->request->getParam('controller');
$method = $this->request->getParam('action');
if(isset($excluded_actions[$controller]) && isset($excluded_actions[$controller][$method])) {
$this->Auth->allow();
}
}
}