CakePHP Authentication Using Prefix Routing

I am creating a website with CakePHP that would like to have 3 sections:

  • public area
  • user area
  • administration area

I have a prefix routing setting in routes.php that looks like

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

I want any actions with the user_ prefix to be redirected to the login screen if it has not been registered yet, and the user type is “normal” (side question: can the user be normal: P) and any actions with the admin_ prefix should also be redirected but request an admin user type.

I started trying to use the Auth component, but it seems rather inflexible, while the ACL seems to be on top. Can anyone offer some tips on how to best achieve what I want?

+3
source share
1 answer

The Auth component must be flexible enough for this.

You can do beforeFilter()as follows:

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

You can also add features isAuthorized()to your model or controller as needed to perform even more advanced authentication. See http://book.cakephp.org/1.3/en/The-Manual/Core-Components/Authentication.html#authcomponent-variables .

+7
source

Source: https://habr.com/ru/post/1725347/


All Articles