Authentication: Either a Custom Resolver class, or using associations in TokenResolver?

I have a UserSurveys table, which belongs to a Users table. I want to use a token in the UserSurveys to return a row from Users as the identity.

Can I use the token to return an associated record? If I could set Users as the userModel and set the tokenField to ‘UserSurveys.id’ that would be perfect. That doesn’t seem to work.

Since that didn’t work I’m trying to create a custom Resolver class, but it’s not finding it. I’m not sure what I’m doing wrong. I’m sure it’s something silly I’m just overlooking.

I created the class src/Identifier/Resolver/UserSurveyResolver.php

The code looks like this:

namespace App\Identifier\Resolver;

use Cake\Core\InstanceConfigTrait;
use Cake\ORM\Locator\LocatorAwareTrait;
use Authentication\Identifier\Resolver\ResolverInterface;

class UserSurveyResolver implements ResolverInterface
{
    use InstanceConfigTrait;
    use LocatorAwareTrait;

    /**
     * @inheritDoc
     */
    public function find(array $conditions, $type = self::TYPE_AND)
    {
        $table = $this->getTableLocator()->get('UserSurveys');

        $query = $table->query();

        $where = [];
        foreach ($conditions as $field => $value) {
            $field = $table->aliasField($field);
            if (is_array($value)) {
                $field = $field . ' IN';
            }
            $where[$field] = $value;
        }

        $query->contain('Users');

        $result = $query->where([$type => $where])->first();

        return $result->user;
    }
}

But I’m getting the error: Resolver class Authentication.UserSurvey does not exist.

Any suggestions?

1 Like

Oooh, I found it. I was setting the className in application.php to Authentication.UserSurveys. Changed it to just UserSurveys and it works now.

I knew it was something simple.