简体   繁体   中英

CakePHP 3.x using redirectUrl() on diffrent User Roles

I am using the Auth Component to set the redirects on Login and Logout:

$this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Adresses',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display',
                'home'
            ],
                'authenticate' => [
                    'Form' => [
                        'fields' => ['username' => 'email']
                    ]
                ],
            'authorize' => 'Controller',

        ]);

Now I want the User to get redirect on different pages accordance with their role,which works well so far:

if ($this->Auth->user('role') == 'admin') {                           
                            return $this->redirect([
                                'controller' => 'adresses',
                                'action' => 'adminindex'
                            ]);
                        }
                    } elseif ($this->Auth->user('role') == 'user') {
                        return $this->redirect([
                            'controller' => 'adresses',
                            'action' => 'index'
                        ]);
                    }

Now I want to redirect the different roles to the requested links.And for that I am using:

return $this->redirect($this->Auth->redirectUrl());

Which works only for request like view and edit.Otherwise It is falling back to the Auth Component redirect , which I do not want.

How can I accomplish that I have my regular Fallback in the Auth Component (App Controller) my Role switcher and also the redirect, when views are requested and the user/admin is not logged in already.

For that i tried:

 if(!empty($this->Auth->redirectUrl())){

 }

Which doesn't working.Any idea would be appreciated.

$this->Auth->redirectUrl()

is just for Redirecting Users After Login

see http://book.cakephp.org/3.0/en/controllers/components/authentication.html#redirecting-users-after-login

if you need redirect to referer pages , you can use Controller::referer()

see http://api.cakephp.org/3.3/class-Cake.Controller.Controller.html#_referer

you can check logged in users with $this->Auth->user()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM