Authentication – podstawowe komendy

W szablonie:

<?php
  $user_id = $this->getRequest()->getAttribute('identity')->getIdentifier();
?>

W kontrolerze:

$user_id = $this->Authentication->getIdentity()->getIdentifier();

$role = $this->Authentication->getIdentity()->get('role');

$role == 'admin' || 'user'

Wyłączenie z dostępu po zalogowaniu

use Cake\Event\EventInterface;


public function beforeFilter(EventInterface $event): void
{
        parent::beforeFilter($event);

        if(!$this->is_admin) {
            $this->Flash->error(__('Nie ma takiej strony'));
            $event->setResult($this->redirect('/'));
            return;
        }

        $this->Authentication->addUnauthenticatedActions(['edit', 'changePassword' ]);
    }