loadComponent('FormProtection');` * * @return void */ public function initialize(): void { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('RestResponse'); $this->loadComponent('Security'); $this->loadComponent('ParamHandler', [ 'request' => $this->request ]); $this->loadModel('MetaFields'); $this->loadModel('MetaTemplates'); $table = $this->getTableLocator()->get($this->modelClass); $this->loadComponent('CRUD', [ 'request' => $this->request, 'table' => $table, 'MetaFields' => $this->MetaFields, 'MetaTemplates' => $this->MetaTemplates ]); $this->loadComponent('Authentication.Authentication'); $this->loadComponent('ACL', [ 'request' => $this->request, 'Authentication' => $this->Authentication ]); $this->loadComponent('Navigation', [ 'request' => $this->request, ]); $this->loadComponent('Notification', [ 'request' => $this->request, ]); if (Configure::read('debug')) { Configure::write('DebugKit.panels', ['DebugKit.Packages' => true]); Configure::write('DebugKit.forceEnable', true); } $this->loadComponent('CustomPagination'); $this->loadComponent('FloodProtection'); /* * Enable the following component for recommended CakePHP form protection settings. * see https://book.cakephp.org/4/en/controllers/components/form-protection.html */ //$this->loadComponent('FormProtection'); } public function beforeFilter(EventInterface $event) { $this->loadModel('Users'); $this->Users->checkForNewInstance(); if ($this->ParamHandler->isRest()) { $this->authApiUser(); $this->Security->setConfig('unlockedActions', [$this->request->getParam('action')]); } $this->ACL->setPublicInterfaces(); if (!empty($this->request->getAttribute('identity'))) { $user = $this->Users->get($this->request->getAttribute('identity')->getIdentifier(), [ 'contain' => ['Roles', 'Individuals' => 'Organisations', 'UserSettings', 'Organisations'] ]); if (!empty($user['disabled'])) { $this->Authentication->logout(); $this->Flash->error(__('The user account is disabled.')); return $this->redirect(\Cake\Routing\Router::url('/users/login')); } unset($user['password']); $this->ACL->setUser($user); $this->request->getSession()->write('authUser', $user); $this->isAdmin = $user['role']['perm_admin']; if (!$this->ParamHandler->isRest()) { $this->set('menu', $this->ACL->getMenu()); $this->set('loggedUser', $this->ACL->getUser()); $this->set('roleAccess', $this->ACL->getRoleAccess(false, false)); } Configure::write('loggedUser', $user); } else if ($this->ParamHandler->isRest()) { throw new MethodNotAllowedException(__('Invalid user credentials.')); } if ($this->request->getParam('action') === 'index') { $this->Security->setConfig('validatePost', false); } $this->Security->setConfig('unlockedActions', ['index']); if ($this->ParamHandler->isRest()) { $this->Security->setConfig('unlockedActions', [$this->request->getParam('action')]); $this->Security->setConfig('validatePost', false); } $this->ACL->checkAccess(); if (!$this->ParamHandler->isRest()) { $this->set('ajax', $this->request->is('ajax')); $this->request->getParam('prefix'); $this->set('baseurl', Configure::read('App.fullBaseUrl')); if (!empty($user) && !empty($user->user_settings_by_name['ui.bsTheme']['value'])) { $this->set('bsTheme', $user->user_settings_by_name['ui.bsTheme']['value']); } else { $this->set('bsTheme', Configure::check('ui.bsTheme') ? Configure::read('ui.bsTheme') : 'default'); } if ($this->modelClass == 'Tags.Tags') { $this->set('metaGroup', !empty($this->isAdmin) ? 'Administration' : 'Cerebrate'); } $this->response = $this->response->withHeader('X-Frame-Options', 'DENY'); } if (mt_rand(1, 50) === 1) { $this->FloodProtection->cleanup(); } } public function beforeRender(EventInterface $event) { if (!empty($this->request->getAttribute('identity'))) { if (!$this->ParamHandler->isRest()) { $this->set('breadcrumb', $this->Navigation->getBreadcrumb()); $this->set('notifications', $this->Notification->getNotifications()); } } } private function authApiUser(): void { if (!empty($_SERVER['HTTP_AUTHORIZATION']) && strlen($_SERVER['HTTP_AUTHORIZATION'])) { $this->loadModel('AuthKeys'); $logModel = $this->Users->auditLogs(); $authKey = $this->AuthKeys->checkKey($_SERVER['HTTP_AUTHORIZATION']); if (!empty($authKey)) { $this->loadModel('Users'); $user = $this->Users->get($authKey['user_id']); $logModel->insert([ 'request_action' => 'login', 'model' => 'Users', 'model_id' => $user['id'], 'model_title' => $user['username'], 'changed' => [] ]); if (!empty($user)) { $this->Authentication->setIdentity($user); } } else { $user = $logModel->userInfo(); $logModel->insert([ 'request_action' => 'login', 'model' => 'Users', 'model_id' => $user['id'], 'model_title' => $user['name'], 'changed' => [] ]); } } } public function generateUUID() { $uuid = Text::uuid(); return $this->RestResponse->viewData(['uuid' => $uuid], 'json'); } public function queryACL() { return $this->RestResponse->viewData($this->ACL->findMissingFunctionNames()); } public function getRoleAccess() { return $this->RestResponse->viewData($this->ACL->getRoleAccess(false, false)); } }