0
0

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.

Gate::allows()とGate::check()は同じもの?

Last updated at Posted at 2021-07-08

Laravel6.xの内容です。
内容間違っていたので修正しました。

結論

実質同じ処理でした。(phpdocが違うのみ)
しかし今後Laravelがバージョンアップの変更を考慮してphpdoc通りに使うのが良さそうです。

Gate::allows()

1つの権限を持っている
第1引数はString

if (Gate::allows('edit-settings')) {
    // 現在のユーザーは設定を変更できる
}

Gate::check()

全ての権限を持っている
第1引数はiterable(array等)

if (Gate::check(['update-post', 'delete-post'], $post)) {
    // ユーザーはポストの更新「と」削除ができる
}

Gate::any()

いずれかの権限を持っている
第1引数はiterable(array等)

if (Gate::any(['update-post', 'delete-post'], $post)) {
    // ユーザーはポストの更新「か」削除ができる
}

翻訳ドキュメントでは、Gate::any()が「ユーザーはポストの更新「と」削除ができる」になっていたため、勘違いしました。
https://readouble.com/laravel/6.x/ja/authorization.html#authorizing-actions-via-gates

詳細

Gateクラスの実クラス。
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Auth/Access/Gate.php#L243-L293

src/Illuminate/Auth/Access/Gate.php
    /**
     * Determine if the given ability should be granted for the current user.
     * (機械翻訳)指定された能力を現在のユーザーに付与する必要があるかどうかを決定します。 
     *
     * @param  string  $ability
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function allows($ability, $arguments = [])
    {
        return $this->check($ability, $arguments);
    }

    /**
     * Determine if the given ability should be denied for the current user.
     * (機械翻訳)特定の能力を現在のユーザーに対して拒否する必要があるかどうかを判断します。 
     *
     * @param  string  $ability
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function denies($ability, $arguments = [])
    {
        return ! $this->allows($ability, $arguments);
    }

    /**
     * Determine if all of the given abilities should be granted for the current user.
     * (機械翻訳)指定されたすべての機能を現在のユーザーに付与する必要があるかどうかを決定します。 
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function check($abilities, $arguments = [])
    {
        return collect($abilities)->every(function ($ability) use ($arguments) {
            return $this->inspect($ability, $arguments)->allowed();
        });
    }

    /**
     * Determine if any one of the given abilities should be granted for the current user.
     * (機械翻訳)指定された能力のいずれかを現在のユーザーに付与する必要があるかどうかを判断します。 
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function any($abilities, $arguments = [])
    {
        return collect($abilities)->contains(function ($ability) use ($arguments) {
            return $this->check($ability, $arguments);
        });
    }

Bladeの @can

Bladeの @canGate::check()でした。
https://github.com/laravel/framework/blob/6.x/src/Illuminate/View/Compilers/Concerns/CompilesAuthorizations.php#L15

src\Illuminate\View\Compilers\Concerns\CompilesAuthorizations.php
    protected function compileCan($expression)
    {
        return "<?php if (app(\Illuminate\\Contracts\\Auth\\Access\\Gate::class)->check{$expression}): ?>";
    }

参考URL

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?