authentication - CakePHP 身份验证插件身份关联

标签 authentication cakephp associations cakephp-3.x

我正在使用 CakePHP 3.8 并迁移到身份验证插件 ( https://book.cakephp.org/authentication/1.1/en/index.html )。

在 Controller 中调用 $this->Authentication->getIdentity()->getOriginalData() 时,我想访问我的 User 的几个关联> 实体。

目前,我通过在我的 User 实体中实现以下 IdentityInterface 方法来实现此目的:

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

但我觉得插件配置中的某处应该有一个 contain 参数(就像 AuthComponent 一样)。

任何人都可以指导我如何在调用 getIdentity() 时在 User 实体上包含关联吗?

最佳答案

旧 Auth 组件的身份验证对象的 contain 选项早已被弃用,推荐的方法是使用自定义查找器,新版本中也是这样做的身份验证插件。

ORM 解析器采用 finder 选项,并且必须通过使用的标识符进行配置,在您的情况下,该标识符可能是密码标识符,即类似:

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

在表类(可能是 UsersTable)的 finder 方法中,您可以包含您需要的任何内容:

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

另请参阅

关于authentication - CakePHP 身份验证插件身份关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58640337/

相关文章:

ruby-on-rails - rails : form to build deep associations

ruby-on-rails - Rails协会has_one最新记录

c# - 无法设置 AuthenticationHeaderValue

asp.net - 创建持久身份验证 cookie : ASP. NET MVC 时出现问题

php - 比较两个非常相似的表中的所有个人记录,其中包含大量数据

cakephp - 请帮我获取 Cakephp 2.3.9 记录列表中记录的动态 id

python - 使用 ldap 配置文件在 django admin 中进行身份验证

javascript - 登录成功后返回 Angular 页面吗?

MySQL 查询 - 如何检查第二个表记录是否存在?

ruby-on-rails - 在 Rails 中创建关联模型?