1

We are working on a project where are 4 roles. But in cakephp 3.x Auth component holds authenticate user data in session with Auth.User indexing using

$this->Auth->setUser($user);

Due to this we are not able to access front-end user account from admin panel for some purpose, because of when we login to front-end user from admin panel, front-end login action performs and over write of session value.

So if there is any process to handle this please suggest us.

Thank you in advance.

3 Answers3

2

As well I have understood that you are not using prefix to manage back-end and front-end user then may be you worked with separate folder structure for back-end, May I right?

You are right that $this->Auth->setUser($user); always holds session with Auth.User indexing. So you need to write different session indexing for back-end, and you can do it as follow :

For back-end user authentication : **

$this->loadComponent('Auth', [
                'authorize' => ['Controller'], // Added this line
                'loginRedirect' => [
                    'controller' => 'Users',
                    'action' => 'dashboard',
                    'prefix' => 'admin_panel'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login',
                    'prefix' => 'admin_panel'
                ],
                'storage' => [
                    'className' => 'Session',
                    'key' => 'Auth.Admin',              
                ]
            ]);

**

Here you can pass your desired index in 'storage' array key value. I think it'll works for you.

Krishna Gupta
  • 695
  • 4
  • 15
  • Thank you @Krishna Gupta, it works for me. and your answer full-filled my requirement. well done, thank you very much once again. – user7738665 May 06 '17 at 11:27
0

Check out the section Authentication and Authorization in this curated list of CakePHP Plugins.

You could, for example, use dereuromarks TinyAuth Plugin to authorize your users and configure what they are able to see.

This way you can use the same authentication (be aware of the differences between Authentication and Authorization) and the same users table, which will prevent the Session conflict you mentioned.

Marijan
  • 1,825
  • 1
  • 13
  • 18
0

The Auth component overwrite the previous session because it store the session in Auth.users all the time so we have to change the session key for different role.

If you are using URL prefix for the different roles to access then you can do like this.

AppController.php

    public function beforeFilter(Event $event)
    {
        if($this->request->params['prefix']){
            $this->Auth->config('storage',  [
                'key'=>'Auth.'.$this->request->params['prefix'],
                'className'=>'Session'
            ]);
        }

        return parent::beforeFilter($event); // TODO: Change the autogenerated stub
    }

This will create different roles in Auth as you required. The session will be like this

[
    'Auth'=>[
        'User'=>['id'=>''],
        'Admin'=>['id'=>''],
    ]
]

Tested it, working great for me.

Aman Rawat
  • 2,625
  • 1
  • 25
  • 40