views:

532

answers:

1

I'm building a site with CakePHP which I would like to have 3 sections:

  • public area
  • user area
  • admin area

I've setup prefix routing in routes.php which looks like

Router::connect('/user/:controller/:action/*', array('prefix' => 'user', 'user' => true));
Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin', 'admin' => true));

I want it so any actions with the user_ prefix will redirect to a login screen if not already logged in and user type is 'normal' (side question: can a user be normal :P) and any actions with admin_ prefix also redirect but require user type of admin.

I started trying to use the Auth component but it seems quite inflexible whereas ACL seems over the top. Could anyone offer some advice on the best way to achieve what I want?

+5  A: 

The Auth Component should be plenty flexible for this.

You could do a beforeFilter() like this:

//  I think it's params['prefix'], might be different
//               vvvvvvvvvvvvvvvv
if (isset($this->params['prefix'])) {
    $this->Auth->userScope = array('User.type' => $this->params['prefix']);
}

You can also add isAuthorized() functions to either your model or controller on an as-needed basis to do even more advanced authentication. See http://book.cakephp.org/view/396/authorize.

deceze
Thanks a lot deceze - just the help I needed.
gacrux
BTW $this->params['prefix'] was right
gacrux