CakePHP Authentication with Prefix Routing
I'm building a site with Cake开发者_JS百科PHP 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?
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/1.3/en/The-Manual/Core-Components/Authentication.html#authcomponent-variables.
精彩评论