Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associated roles table not returned #74

Closed
monsurhoq opened this issue Oct 3, 2016 · 11 comments
Closed

Associated roles table not returned #74

monsurhoq opened this issue Oct 3, 2016 · 11 comments
Labels

Comments

@monsurhoq
Copy link

I am using ADmad/CakePHP-HybridAuth with dereuromark/cakephp-tinyauth with multi role facility. I wrote following code in my AppController.php:

    $this->loadComponent('TinyAuth.Auth', [
            'autoClearCache' => true,
            // followings are common setting for auth component
            'authenticate' => [
                 'Form' => [
                    'finder' => 'auth'
                 ],
                 'ADmad/HybridAuth.HybridAuth' => [
                    'fields' => [
                        'provider' => 'provider',
                        'openid_identifier' => 'openid_identifier',
                        'email' => 'email'
                    ],
                    'profileModel' => 'ADmad/HybridAuth.SocialProfiles',
                    'profileModelFkField' => 'user_id',
                    'hauth_return_to' => ['controller' => 'Users', 'action' => 'index', 'prefix' => false, '_ext' => null],
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ],
            'loginRedirect' => [
                'controller' => 'Users',
                'action' => 'index',
                'prefix' => false
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ],
            'flash' => [
                'element' => 'error',
                'key' => 'auth'
            ],
            // using bootstrap ui for flash element
            'flash' => [
                'element' => 'error',
                'key' => 'auth'
            ],
            'authorize' => [
                // check authorization using TinyAuth based on config/acl.ini settings
                'TinyAuth.Tiny' => [
                    'multiRole' => true,
                    'autoClearCache' => true,
                    'superAdminRole' => '01',
                    'rolesTable' => 'roles',
                    'pivotTable' => 'roles_users'
                ]
            ]
         ]);

When I try to login using form it returns associated roles, but when try to login using HybridAuth not returned the roles. FYI, authentication and authorization works for both case.

I am using CakePHP 3.3.5. I also try using contain, which have been deprecated as of 3.1, but no roles return for HybridAuth. I also try my custom finder findAuth for HybridAuth, no change.

FYI, when I save new user using \Cake\Event\EventManager::instance()->on('HybridAuth.newUser', [$this, 'createUser']);, my associated roles saved successfully.

How can I retrieve roles using HybridAuth?

@monsurhoq monsurhoq changed the title Association roles table not returned Associated roles table not returned Oct 3, 2016
@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

I see you have added 'finder' => 'auth' for your Form authenticator, why haven't you done the same for ADmad/HybridAuth.HybridAuth's config?

@ADmad ADmad added the on-hold label Oct 3, 2016
@monsurhoq
Copy link
Author

monsurhoq commented Oct 3, 2016

I also try that, I told on my message.

@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

I told on my message.

Not clear enough :) All you said was HybridAuth does not return roles.

@monsurhoq
Copy link
Author

I am using CakePHP 3.3.5. I also try using contain, which have been deprecated as of 3.1, but no roles return for HybridAuth. I also try my custom finder findAuth for HybridAuth, no change.

Yes, HybridAuth does not return roles using 'finder' => 'auth' in ADmad/HybridAuth.HybridAuth's config

@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

HybridAuth does take the finder into account.

@monsurhoq
Copy link
Author

monsurhoq commented Oct 3, 2016

This my new code:

        $this->loadComponent('TinyAuth.Auth', [
            'autoClearCache' => Configure::read('debug'),
            // followings are common setting for auth component
            'authenticate' => [
                 'Form' => [
                    'finder' => 'auth'
                 ],
                 'ADmad/HybridAuth.HybridAuth' => [
                    'fields' => [
                        'provider' => 'provider',
                        'openid_identifier' => 'openid_identifier',
                        'email' => 'email'
                    ],
                    'profileModel' => 'ADmad/HybridAuth.SocialProfiles',
                    'profileModelFkField' => 'user_id',
                    'hauth_return_to' => ['controller' => 'Users', 'action' => 'index', 'prefix' => false, '_ext' => null],
                    'finder' => 'auth'
                ]
            ],
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ],
            'loginRedirect' => [
                'controller' => 'Users',
                'action' => 'index',
                'prefix' => false
            ],
            'logoutRedirect' => [
                'controller' => 'Users',
                'action' => 'login',
                'prefix' => false
            ],
            'flash' => [
                'element' => 'error',
                'key' => 'auth'
            ],
            // using bootstrap ui for flash element
            'flash' => [
                'element' => 'error',
                'key' => 'auth'
            ],
            'authorize' => [
                // check authorization using TinyAuth based on config/acl.ini settings
                'TinyAuth.Tiny' => [
                    'multiRole' => true,
                    'autoClearCache' => true,
                    'superAdminRole' => '01',
                    'rolesTable' => 'roles',
                    'pivotTable' => 'roles_users'
                ]
            ]
         ]);

And this my finder in UsersTable.php

    public function findAuth(\Cake\ORM\Query $query, array $options)
    {
        $query
            ->contain(['Roles'])
            ->where(['Users.isactive' => 1,'Users.activationkey IS' => null]);

        return $query;
    }

Roles not return.

@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

You can step through the code and check whether the finder you set is being used at the line i linked.

@monsurhoq
Copy link
Author

monsurhoq commented Oct 3, 2016

That block executed, when a new user try to login with hybridauth first time. But when user information save into database, after then when same user try to login using hybridauth, that block not executed.

        if ($profile && !empty($profile->user)) { // this is for old user
            $user = $profile->user;
            $profile->unsetProperty('user');
        } elseif ($providerProfile->email) { // this is for new user
            pr($config['finder']);
            $user = $this->_userModel->find($config['finder'])
                ->where([
                    $this->_userModel->aliasField($config['fields']['email']) => $providerProfile->email
                ])
                ->first();
            pr($user);
            die();
        }

@ADmad ADmad added bug and removed on-hold labels Oct 3, 2016
@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

Okay, I see the problem. Will make a fix.

@ADmad ADmad closed this as completed in 088a33d Oct 3, 2016
@ADmad
Copy link
Owner

ADmad commented Oct 3, 2016

I have made a new release with the fix.

@monsurhoq
Copy link
Author

monsurhoq commented Oct 4, 2016

Thanks. It is working now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants