<?php
    namespace App\Controller;

    use App\Controller\AppController;
    use Cake\Http\ServerRequest;
    use Cake\Validation\Validator;
    use Cake\Event\Event;
    use Cake\Auth\BasicAuthenticate;

    class UsersController extends AppController
    {
      public function beforeFilter(\Cake\Event\EventInterface $event)
      {
          parent::beforeFilter($event);
          // Configure the login action to not require authentication, preventing
          // the infinite redirect loop issue
          $this->Authentication->addUnauthenticatedActions(['login']);
      }
      public function login()
      {
        $this->request->allowMethod(['get', 'post']);
        $result = $this->Authentication->getResult();
        // regardless of POST or GET, redirect if user is logged in
        if ($result->isValid()) {
            return $this->redirect(['controller' => 'Users', 'action' => 'index']);
        }
        // display error if user submitted and authentication failed
        if ($this->request->is('post') && !$result->isValid()) {
            $this->Flash->error(__('Invalid username or password'));
        }
      }
      public function index()
      {
          $this->paginate = ['contain' => ['Matriculas']];

          $users = $this->paginate($this->Users);

          $this->set(compact('users'));
      }
    }
