Welcome to the Cookbook

loading...

5.2.5 AuthComponent Methods

5.2.5.1 action

action (string $action = ':controller/:action')

Se stai utlizzando gli ACO come parte della struttura delle ACL, si può ottenere il percorso verso il nodo ACO legata ad un particolare controller / azione :

    $acoNode = $this->Auth->action('users/delete');
  1. $acoNode = $this->Auth->action('users/delete');

Se non viene passato alcun valore, usa il controller/azione corrente

5.2.5.2 allow

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

More information about translations

Se nel tuo controller c'è qualche azione che non richiede l'autenticazione (per esempio l'azione per registrare un utente), puoi specificare dei metodi che AuthComponent non controllerà. Il prossimo esempio mostra come permettere di accedere all'aziona 'register' bypassando l'autorizzazione.

Nota: non consentire esplicitamente l'azione 'login', perché è già permessa automaticamente, e specificandola il componente Auth non funziona.

    $this->Auth->allow('register');
  1. $this->Auth->allow('register');

Se vuoi che più di una azione non richieda autorizzazione, le devi fornire come parametri al metodo allow():

    $this->Auth->allow('foo', 'bar', 'baz');
  1. $this->Auth->allow('foo', 'bar', 'baz');

Scorciatoia: puoi permettere l'accesso a tutte le azioni di un controller usando '*'.

    $this->Auth->allow('*');
  1. $this->Auth->allow('*');

ATTENZIONE: Se nel layout o in un elemento utilizzi requestAction, assicurati che le azioni richiamate siano correttamente gestite da Auth.

5.2.5.3 deny

A volte avrai bisogno di rimuovere delle azioni dalla lista delle azioni permesse (impostate con $this->Auth->allow()). Ecco un esempio:

    function beforeFilter() {
        $this->Auth->authorize = 'controller';
        $this->Auth->allow('delete');
    }

    function isAuthorized() {
        if ($this->Auth->user('role') != 'admin') {
            $this->Auth->deny('delete');
        }

        ...
    }
  1. function beforeFilter() {
  2. $this->Auth->authorize = 'controller';
  3. $this->Auth->allow('delete');
  4. }
  5. function isAuthorized() {
  6. if ($this->Auth->user('role') != 'admin') {
  7. $this->Auth->deny('delete');
  8. }
  9. ...
  10. }

5.2.5.4 hashPasswords

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

More information about translations

hashPasswords ($data)
  1. hashPasswords ($data)

Questo metodo è utile quando devi effettuare una ricerca in un set di dati e trovare quelli criptati con il sistema di cifratura impostato. E' molto utile quando crei account multipli da un'unica schermata.

    $data['User'][0]['password'] = 'foo';
    $data['User'][1]['password'] = 'bar';
    $data['User'][2]['password'] = 'baz';
    $hashedPasswords = $this->Auth->hashPasswords($data);
  1. $data['User'][0]['password'] = 'foo';
  2. $data['User'][1]['password'] = 'bar';
  3. $data['User'][2]['password'] = 'baz';
  4. $hashedPasswords = $this->Auth->hashPasswords($data);

$hashedPasswords ora contiene un array di tutte le password, criptate e pronte per essere inserite nel database.

5.2.5.5 mapActions

Se stai utilizzando le ACL in modalità CRUD, puoi assegnare certe azioni non standard ad ogni parte del CRUD.

$this->Auth->mapActions(
	array(
		'create' => array('someAction'),
		'read' => array('someAction', 'someAction2'),
		'update' => array('someAction'),
		'delete' => array('someAction')
	)
);
  1. $this->Auth->mapActions(
  2. array(
  3. 'create' => array('someAction'),
  4. 'read' => array('someAction', 'someAction2'),
  5. 'update' => array('someAction'),
  6. 'delete' => array('someAction')
  7. )
  8. );

5.2.5.6 login

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

More information about translations

login($data = null)
  1. login($data = null)

Quando usi un tipo di login asicnrono, ad esempio in Ajax, puoi usare questo metodo per autenticare manualmente un utente. Se non passi nessun valore in $data, utilizza automaticamente i dati POST passati al controller.

5.2.5.7 logout

Fornisce un modo rapido per rimuovere l'autenticazione a un utente e indirizzarlo dove si vuole. Questo metodo risulta molto utile quando si vuole creare un link 'Log out' all'interno di un area riservata della propria applicazione.

Esempio:

$this->redirect($this->Auth->logout());
  1. $this->redirect($this->Auth->logout());

5.2.5.8 password

There is no translation yet for this section. Please help out and translate this.. More information about translations

password (string $password)

Pass in a string, and you can get what the hashed password would look like. This is an essential functionality if you are creating a user registration screen where you have users enter their password a second time to confirm it.

if ($this->data['User']['password'] ==
    $this->Auth->password($this->data['User']['password2'])) {

    // Passwords match, continue processing
    ...
} else {
    $this->flash('Typed passwords did not match', 'users/register');
}
  1. if ($this->data['User']['password'] ==
  2. $this->Auth->password($this->data['User']['password2'])) {
  3. // Passwords match, continue processing
  4. ...
  5. } else {
  6. $this->flash('Typed passwords did not match', 'users/register');
  7. }

The auth component will automatically hash the password field if the username field is also present in the submitted data

Cake appends your password string to a salt value and then hashes it. The hashing function used depends on the one set by the core utility class Security (sha1 by default). You can use the Security::setHash function to change the hashing method. The salt value is used from your application's configuration defined in your core.php

5.2.5.9 user

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

More information about translations

user(string $key = null)
  1. user(string $key = null)

Fornisce le informazioni riguardanti l'utente corrente.

if ($this->Auth->user('role') == 'admin') {
    $this->flash('You have admin access');
}
  1. if ($this->Auth->user('role') == 'admin') {
  2. $this->flash('You have admin access');
  3. }