开发者

CakePhp Auth : Questions

I've to make a cakePhp authentification, and I wish to use the "Auth" component. I'm trying to see if it fill my requirement:

  • I need to authenticate users with their email OR their customerId, (with an addition password of course). I can't find if it is possible to hav开发者_开发百科e two fields(or more) on which the authentication can be done

  • I've several parts on which I need to be authenticated. But I need differents granularity:

    • For some things, it's the whole controller which should not be accessible(if possible with exception(e.g. all the "User" controller, except the login/register action) for other I really need that it's the whole controller(e.g. the cart controller)
    • Sometimes I need that only some actions are unavailable without being logged
    • Sometimes I need that only a part of the view isn't displayed(e.g. login element not displayed)
  • Does the component manage action like password change? Because if the user change its password I need that he did not get disconnected.

Thank you very much for your help


The short answer is that yes, you can do these things, but it seems to me that the ACL might be overkill for your needs (but I also tend to avoid the ACL if there's any opening at all to do so). To your points:

  • As Ivo suggests, you'll need a custom UsersController::login() method to authenticate by multiple fields (If your auth model isn't User, then use the appropriate controller). If the Auth component's login method fails, it passes control to your custom login() method. Here's a snippet from a project I've been working on:

    function login() {
      # Allow login by either username (legacy) or email.
      # If no authenticated user exists at this point then the Auth
      # component's login() method has failed and control has been passed
      # here for any further handling. Since the expected credentials
      # (email and password) have failed we're going to check for
      # username and password.
      $user = $this->Auth->user();
      if( empty( $user ) && !empty( $this->Auth->data['User']['email'] ) && !empty( $this->Auth->data['User']['password'] ) ) {
        $user = $this->User->find(
          'first',
          array(
            'recursive'  => -1,
            'conditions' => array(
              'User.username' => $this->Auth->data['User']['email'],
              'User.password' => $this->Auth->data['User']['password'],
            )
          )
        );
    
        if( empty( $user ) || !$this->Auth->login( $user ) ) {
          # Set the configured flash message b/c login failed after both checks.
          $this->Session->setFlash( $this->Auth->loginError, null, array(), 'auth' );
        }
      }
    
      $this->redirect( $this->Auth->redirect(), null, true );
    }
    
  • For action access, just use the $this->Auth->allow() and $this->Auth->deny() methods in each relevant controller's beforeFilter() callback. For example, in the UsersController, you may want to do something like this:

    public function beforeFilter() {
      parent::beforeFilter();
    
      $this->Auth->deny('*');
      $this->Auth->allow( 'login', 'logout' );
    }
    
  • In views, just determine whether the user is authenticated by testing the Auth.User value to determine what to display/hide from anonymous/authenticated:

    if( $this->Session->check( 'Auth.User' ) ) { ... }
    
  • If the password changes, you can re-authenticate the user transparently by calling $this->Auth->login( $user_data ). I do this, for example, when a user registers. I don't want him/her to have to then go login, so I just login automatically.


Auth doesn't do exactly what you want out of the box. It can only handle whether a user authentification is required to access an action or not. Once the user is logged in, Auth does not care about the user's level access anymore.

If you wish to use more than two fields, I suggest that you extend the AuthComponent and rewrite the login method to fit your needs. I never did it but I imagine that it is reasonably easy.

Regarding your access levels, I would use ACL that can manage access to actions of all the controllers. Once set up, you will have to manually set the permissions for each actions, either using one of the plugins written by the community or manually.

If you wish to disable part of your views you will need to read the permissions to test the user's access level from there. A good thing would be to save the permissions in a cache file or in the session as the user logs in to make it avaiable in the view. Then write your tests and echo what's needed.

(I am using CakePHP 2.0, I don't know how easily you can extend AuthComponent in 1.3 if you're using it)


With Auth, you need to have exactly 2 fields (that you can specify) to authenticate on. One field (password) will be hashed. Yes, all levels of access you want can be specified in Auth: http://book.cakephp.org/view/1251/Setting-Auth-Component-Variables

You'll have to manage the password change, but the users won't get logged out if they change password.

Sometimes I need that only a part of the view isn't displayed(e.g. login element not displayed)

wut?


Create a custom login() which tries to authenticate through either method. You can also setup the Authenticate variable to do a custom login.

You can specify in different controllers what parts Auth should be allowing to authenticated users. See Auth methods.

You can also use ACL (see the full Cake ACL tutorial, etc) to control granular permissions.

Sometimes I need that only a part of the view isn't displayed

Create an element which checks for Auth->user() to select what content to display.


For the Allow and Deny part can be easily done with the Auth Component.

use something like

$this->allow('*'); // to allow every thing
$this->deny('*'); // to deny every thing
$this->allow('login', 'signup'); // allows login and sign up without being logged in
$this->deny('profile', 'password'); // need to be logged into to access profile and password and rest of the actions are allowed.

For the change of password you can save the changed password to the database and force logout the user and redirect him to login again

$this->Auth->logout(); this forces the user to logout of cakephp Auth

For the first question - loggin in using email or customer id is not directly possible using cakephp Auth component as you will have to define in particular which of them acts as a username.

Alternative solution is you can just copy the Auth Component into your app/controller/component and hack the code. When you receive the user name you can test it against email and customer id and maintain the flow.

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜