Welcome to the Cookbook

loading...

5.2.1 Configurare le Variabili di Auth Component

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

Quando vuoi modificare un'opzione default di AuthComponent, puoi farlo creando un metodo beforeFilter() del tuo controller, e poi chiamando vari metodi built-in o variabili del setting component.

Ad esempio, per modificare il nome del campo utilizzato per le password da 'password' a 'secretword' potresti agire come segue:

class UsersController extends AppController {
    var $components = array('Auth');

    function beforeFilter() {
        $this->Auth->fields = array(
            'username' => 'username', 
            'password' => 'secretword'
            );
    }
}
  1. class UsersController extends AppController {
  2. var $components = array('Auth');
  3. function beforeFilter() {
  4. $this->Auth->fields = array(
  5. 'username' => 'username',
  6. 'password' => 'secretword'
  7. );
  8. }
  9. }

In questo specifico caso, dovresti ricordarti di modificare anche il nome del campo nel template view!

Un altro uso frequente delle variabili del componente Auth è quello di consentire l'accesso ad alcuni metodi senza che l'utente sia loggato.

Ad esempio, se vuoi consentire a tutti gli utenti di accedere ai metodi index e view (ma nessun altro), potresti agire come segue:

function beforeFilter() {
        $this->Auth->allow('index','view');
}
  1. function beforeFilter() {
  2. $this->Auth->allow('index','view');
  3. }