PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 17, 2022

[FIXED] CakePHP Authentication Plugin Identity Associations

 March 17, 2022     associations, authentication, cakephp, cakephp-3.x     No comments   

Issue

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()?


Solution

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


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing