将额外数据传递给 finder auth

2024-03-11

我的发现者来自Auth有我需要访问的条件$this->request但我无权访问UsersTable.

应用控制器::初始化

$this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'finder' => 'auth',
            ]
        ]
    ]);

用户表

public function findAuth(Query $query, array $options)
{
    $query
        ->select([
            'Users.id',
            'Users.name',
            'Users.username',
            'Users.password',
        ])
        ->where(['Users.is_active' => true]); // If I had access to extra data passed I would use here.

    return $query;
}

我需要传递额外的数据AppController to finder auth因为我无权访问$this->request->data on UsersTable.

Update

人们在评论中说这是一个糟糕的设计,所以我将准确解释我需要什么。

我有一张桌子users但每个用户都属于一个gym. The username(email)仅对于特定的gym所以我可以有一个[email protected] /cdn-cgi/l/email-protectionfrom gym_id 1和另一个[email protected] /cdn-cgi/l/email-protection from gym_id 2。 在登录页面上我有gym_slug告诉auth finder which gym用户username我提供的属于。


据我所知,在 3.1 中没有办法通过将其传递到配置中来做到这一点。这可能是一个好主意,将其作为功能请求提交到 cakephp git hub 上。

有多种方法可以通过创建一个扩展基本身份验证的新身份验证对象,然后覆盖 _findUser 和 _query 来实现此目的。像这样的东西:

class GymFormAuthenticate extends BaseAuthenticate
{

 /**
  * Checks the fields to ensure they are supplied.
  *
  * @param \Cake\Network\Request $request The request that contains login information.
  * @param array $fields The fields to be checked.
  * @return bool False if the fields have not been supplied. True if they exist.
  */
 protected function _checkFields(Request $request, array $fields)
 {
     foreach ([$fields['username'], $fields['password'], $fields['gym']] as $field) {
         $value = $request->data($field);
         if (empty($value) || !is_string($value)) {
             return false;
         }
     }
     return true;
 }

 /**
  * Authenticates the identity contained in a request. Will use the `config.userModel`, and `config.fields`
  * to find POST data that is used to find a matching record in the `config.userModel`. Will return false if
  * there is no post data, either username or password is missing, or if the scope conditions have not been met.
  *
  * @param \Cake\Network\Request $request The request that contains login information.
  * @param \Cake\Network\Response $response Unused response object.
  * @return mixed False on login failure.  An array of User data on success.
  */
 public function authenticate(Request $request, Response $response)
 {
     $fields = $this->_config['fields'];
     if (!$this->_checkFields($request, $fields)) {
         return false;
     }
     return $this->_findUser(
         $request->data[$fields['username']],
         $request->data[$fields['password']],
         $request->data[$fields['gym']],
     );
 }

/**
  * Find a user record using the username,password,gym provided.
  *
  * Input passwords will be hashed even when a user doesn't exist. This
  * helps mitigate timing attacks that are attempting to find valid usernames.
  *
  * @param string $username The username/identifier.
  * @param string|null $password The password, if not provided password checking is skipped
  *   and result of find is returned.
  * @return bool|array Either false on failure, or an array of user data.
  */
 protected function _findUser($username, $password = null, $gym = null)
 {
     $result = $this->_query($username, $gym)->first();

     if (empty($result)) {
         return false;
     }

     if ($password !== null) {
         $hasher = $this->passwordHasher();
         $hashedPassword = $result->get($this->_config['fields']['password']);
         if (!$hasher->check($password, $hashedPassword)) {
             return false;
         }

         $this->_needsPasswordRehash = $hasher->needsRehash($hashedPassword);
         $result->unsetProperty($this->_config['fields']['password']);
     }

     return $result->toArray();
 }

/**
  * Get query object for fetching user from database.
  *
  * @param string $username The username/identifier.
  * @return \Cake\ORM\Query
  */
 protected function _query($username, $gym)
 {
     $config = $this->_config;
     $table = TableRegistryget($config['userModel']);

     $options = [
         'conditions' => [$table->aliasField($config['fields']['username']) => $username, 'gym' => $gym]
     ];

     if (!empty($config['scope'])) {
         $options['conditions'] = array_merge($options['conditions'], $config['scope']);
     }
     if (!empty($config['contain'])) {
         $options['contain'] = $config['contain'];
     }

     $query = $table->find($config['finder'], $options);

     return $query;
 }
 }

欲了解更多信息,请参阅此:创建自定义身份验证对象 http://book.cakephp.org/3.0/en/controllers/components/authentication.html#creating-custom-authentication-objects

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将额外数据传递给 finder auth 的相关文章

随机推荐

  • viewpager和fragment中布局的幽灵

    I use the viewpager and fragment the adapter is FragmentStatePagerAdapter fragment s layout i use is SwipeRefreshLayout
  • 分配器类型参数的 C++ 设计模式

    C 03 标准库在将类型传递给旨在作为分配器的类时使用简单的模板类型参数 这是可能的 因为模板在 C 中的工作方式 但是 它并不是很简单 您可能不知道类型定义到底应该是什么样子 特别是在非标准类型的情况下 我认为使用适配器类可能是个好主意
  • vue动态mapGetters

    我有一个 props 我想用它来制作动态 mapGetters 但是 mapGetters 将 props 视为未定义 可能是因为计算值是在 props 之前加载的 有人知道我怎样才能让它变得动态吗 我的代码如下 export defaul
  • 从 VSCode 中删除 git 集成

    我下载了 Visual Studio Code 来在工作中试用 我几乎爱上了它的一切 但我不喜欢的一部分是 Git 集成 我导入了整个工作文件夹 约 14000 个源文件 git 项目 Visual Studio Code 中的布局和所有内
  • 切换大小写奇怪的范围

    在查看一些第 3 方 C 代码时 我遇到了类似的情况 switch state case 0 if c A open brace code break brace not closed case 1 code break close bra
  • python 中的套接字问题

    我有一个用 C 编写的服务器 我想用 python 编写一个客户端 当 python 客户端想要发送文件时 它会发送一个字符串 send some file 后跟文件的内容和字符串 end some file 这是我的客户端代码 file
  • 使用 igraph 在 R 中进行 Louvain 社区检测 - 分配交替的组成员资格分配

    我一直在使用 igraph 在 R 中运行 Louvain 社区检测 感谢我之前查询的这个答案 https stackoverflow com questions 49834827 louvain community detection i
  • jQuery 选择器性能

    我知道我只是对几毫秒的表演时间有强迫症 但我只是想知道为什么以下内容对我来说是正确的 这似乎违背了我的逻辑 我目前有一个 div 它在悬停时淡出内部图像 div someclass hover function this children
  • 如何使用一个文件输入元素上传多个文件

    我正在尝试使用一个文件输入元素使用 html 表单将多个文件上传到云端硬盘 尽管文件选择器允许选择多个文件 但这似乎仅适用于一个文件 回到脚本日志查看器 我只看到我上传的两个文件中捕获的一个文件 这是不受支持的 还是我走错了路 Code g
  • 标记位置更改事件

    我正在使用新的 Android 版 Google 地图 v2 有没有办法为标记位置变化设置监听器 例如 当用户拖动标记时 Quoting 文档 https developers google com maps documentation a
  • R 中 SumIf 函数的等效项是什么

    我是 R 和这个网站的新手 但我搜索后没有找到我正在寻找的答案 如果我有以下数据集 总计 names lt c a b c d a b c d x lt cbind x1 3 x2 c 3 10 total lt data frame na
  • 如何将 JSONP 数据类型与 Ember 数据结合使用?

    如何设置 Ember Data 在进行 ajax 调用时使用 JSONP 数据类型 我将使用 Ember 和 Phonegap 并需要发出跨域请求 覆盖私有的要容易得多ajaxOptions函数而不是使用jQuery 无论如何 Ember
  • 如何在facet_wrap中将label_wrap_gen与as_labeller一起使用

    我有一个方面图 并且想将方面条标题包装在多行上 如果超过一定数量的字符 所以我知道我使用labeller label wrap gen 10 比如包裹超过 10 个字符 当传递给facet wrap不过 我也想传递新的标签 我知道我可以使用
  • 如何仅将图片框显示的内容捕获为位图,而不使用“从屏幕复制”?

    具体来说 我需要将图片框实际显示的特定区域捕获为位图 该区域的坐标由我覆盖在图片框顶部的控件的边界指定 但该控件属于图片框 当我制作该区域的 快照 时 该控件被隐藏 我尝试使用普通的屏幕捕获方法 CopyFromScreen 但您无法真正控
  • Greasemonkey调试,获取真实行号

    我正在尝试让 Greasemonkey 用户脚本正常工作 但它一直抛出异常 缺少 声明之前 在 Javascript 错误控制台中 Greasemonkey 文档说应该忽略行号 但由于脚本相当长 因此了解错误发生的位置将非常有帮助 我怎样才
  • 使用 Javascript 旋转文本

    我想循环浏览一系列单词来创建文本旋转效果 我的大部分工作都按预期进行 有什么方法可以在 p 元素的长度上使用 css 过渡吗 当从 char length gt 10 的对象遍历到 char length HTML p span span
  • 通过经过身份验证的 Web 表单保护 ASP.net 中的 Ajax 请求

    我已经读过通过 GUID 保护 AJAX 请求 https stackoverflow com questions 652851 securing ajax requests via guid and 保护 ajax 请求 https st
  • 安装 XAMPP 时如何使用 UAC

    我正在将 Xampp 安装到我的计算机上 但是当我尝试安装它时 会出现一个对话框 我该如何解决这个问题 非常感谢您的帮助 对话框 重要的 因为您的系统上已激活用户帐户控制 UAC XAMPP 的功能可能受到限制 使用UAC请避免安装XAMP
  • Vertx 线程阻塞警告

    我正在 ubuntu 服务器上运行 vert x 应用程序 它有一个在端口 3000 上运行的 HTTPServer 应用程序工作正常 但有时我会看到以下警告 Dec 08 2017 1 23 43 AM io vertx core imp
  • 将额外数据传递给 finder auth

    我的发现者来自Auth有我需要访问的条件 this gt request但我无权访问UsersTable 应用控制器 初始化 this gt loadComponent Auth authenticate gt Form gt finder