Finally i found the solution!
I share it for someone who is with the same doubt to start working with the Auth plungin.
Model: UsersTable.php
Controller: AccountsController.php
This is a controller without an entity or table in the database, but it uses the user model.
The AccountsController.php
<?PHP
declare(strict_types = 1);
namespace App\Controller;
use Cake\Routing\Router;
class AccountsController extends AppController
{
public function initialize(): void
{
parent::initialize();
$this->loadModel('Users');
}
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
$this->Authentication->addUnauthenticatedActions(['login']);
// Skip all other methods
$this->Authorization->skipAuthorization();
}
public function editar()
{
$user_id = $this->request->getSession()->read('Auth.id');
// Forcing the method to Auth
// 1st call the user model
$usuario = $this->Users->get($user_id);
// Then, call the auth in the Users Policy
$this->Authorization->authorize($usuario, 'editar');
}
And in the Users Policy:
public function canEditar(IdentityInterface $user, User $resource)
{
// logged in users can delete their own articulos.
return $this->isAuthor($user, $resource);
}
protected function isAuthor(IdentityInterface $user, User $resource)
{
return $user->id === $user->getIdentifier();
// This is a simple logic. But others can be created.
}
And that's all. You can find more about this plugin in the official Cake Book (https://book.cakephp.org/authorization/2/en/index.html) o in MarkStory Blog (http://mark-story.com/posts/view/introducing-the-cakephp-authorization-plugin)