Welcome to the Cookbook

loading...

5.2.1 Setting Auth Component Variables

ada perubahan yang masih ditangguhkan untuk bagian ini. More information about translations

Whenever you want to alter a default option for AuthComponent, you do that by creating a beforeFilter() method for your controller, and then calling various built-in methods or setting component variables.

For example, to change the field name used for passwords from 'password' to 'secretword', you would do the following:

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 this particular situation, you would also need to remember to change the field name in the view template!

Another common use of Auth component variables is to allow access to certain methods without the user being logged in (by default Auth restricts access to every action except the login and logout methods).

For example if we want to allow all users access to the index and view methods ( but not any other), we would do the following:

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