CakePHP Authentication Plugin Identity Associations

感情迁移 提交于 2021-02-10 14:20:32

问题


I'm using CakePHP 3.8 and migrating to the Authentication Plugin (https://book.cakephp.org/authentication/1.1/en/index.html).

When calling $this->Authentication->getIdentity()->getOriginalData() in a controller, I'd like to access a couple of assocations of my User entity.

At the moment, I'm doing this by implementing the following IdentityInterface method in my User entity:

public function getOriginalData() {
  $table = TableRegistry::getTableLocator()->get($this->getSource());
  $table->loadInto($this, ['Activities', 'Clients']);
  return $this;
}

But I feel there should be a contain parameter somewhere within the Plugin configuration (as there was with the AuthComponent).

Can anyone guide me on how to include assocations on the User entity when calling getIdentity()?


回答1:


The contain option of the authentication objects for the old Auth component has been deprecated quite some time ago, and the recommended method is to use a custom finder, and that's also how it's done in the new authentication plugin.

The ORM resolver takes a finder option, and it has to be configured via the used identifier, which in your case is probably the password identifier, ie something like:

$service->loadIdentifier('Authentication.Password', [
    // ...
    'resolver' => [
        'className' => 'Authentication.Orm',
        'finder' => 'authenticatedUser' // <<< there it goes
    ],
]);

In the finder method in your table class (probably UsersTable) you can then contain whatever you need:

public function findAuthenticatedUser(\Cake\ORM\Query $query, array $options)
{
    return $query->contain(['Activities', 'Clients']);
}

See also

  • Cookbook > Controllers > Components > AuthComponent > Customizing The Find Query
  • Cookbook > Database Access & ORM > Retrieving Data & Results Sets > Custom Finder Methods
  • Authentication Cookbook > Identifiers
  • Authentication Cookbook > Identifiers > ORM Resolver


来源:https://stackoverflow.com/questions/58640337/cakephp-authentication-plugin-identity-associations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!