8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

cakePHP4 ログイン時の認証条件追加

Last updated at Posted at 2021-01-28

はじめに

すぐ解決できなかった、そしてピンポイント記事が無いように感じたのでここに記録しておきます!
以下認証の実装はすでに行っていることとします。
きっとここから悩んでいる方が、
記事にきてくださっている気がするので、、、、
https://book.cakephp.org/4/ja/tutorials-and-examples/cms/authentication.html

参考
https://book.cakephp.org/authentication/2/en/identifiers.html

コード

Application.php
        // identifiers を読み込み、email と password のフィールドを確認します
        $authenticationService->loadIdentifier('Authentication.Password', [
            'fields' => [
                'username' => 'email',
                'password' => 'password',
            ],
            'resolver' => [
              'className' => 'Authentication.Orm', //ここ追加
              'finder' => 'auth' //ここ追加
            ],
        ]);
Model/Table/UsersTable.php

    //チェックする取得レコード制限
    public function findAuth(Query $query, array $options)
    {
        $query
            ->where(['Users.is_temporary' => 0]); //本登録のみ ここの条件は適宜変更してください!

        return $query;
    }

おまけ

:shamrock:コメントがあって調べたので追記します!

『コメント内容』
以下の『$options』の箇所に値を入れるにはどうするべきか?

.php
public function findAuth(Query $query, array $options)

『回答』

.PHP
'finder' => 'auth',

の箇所を以下に変更すると

.PHP
'finder' => array('auth' => array('sample' => 13)),

以下のような感じで取得出来ます!

.php
    public function findAuth(Query $query, array $options)
    {
      
      var_dump($options['sample']); //13

        $query
            ->where(['Users.is_temporary' => 0]); //本登録のみ

        return $query;
    }

ちなみに以下箇所が主な処理なのですが、これみると『auth』一つでなくても『auth』以外も入れて複数も指定出来そうです!ヽ(´▽`)/便利ですね!

.php
    public function find(array $conditions, $type = self::TYPE_AND)
    {
        $table = $this->getTableLocator()->get($this->_config['userModel']);

        $query = $table->query();
        $finders = (array)$this->_config['finder'];

        //ここでfinderに入れたものチェックしているようなので配列で複数入れて色々なチェックもためせそうです!
        foreach ($finders as $finder => $options) {
            if (is_string($options)) {
                $query->find($options);
            } else {
                $query->find($finder, $options);
            }
        }

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

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

最後に

何かあれば追加します(。・ω・。)
テンション高くなくて面白みにかけると思った方、
私のテンションが高くなるようにほめてください。
心の中でよしよししてください、、
いつかテンションが上がるかもしれません、、
きっとここに来てくれた方も元気が無いので、
丶(・ω・`) ヨシヨシ丶(・ω・`) ヨシヨシ丶(・ω・`) ヨシヨシ
もし少しでもこの記事で時間が空いたら身体をやすめましょう。
寝るのや、食べるの大事ですよ、、、、
丶(・ω・`) ヨシヨシ丶(・ω・`) ヨシヨシ丶(・ω・`) ヨシヨシ

8
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?