无法弄清楚Laravel ACL(Gate)的工作原理

时间:2015-10-30 11:13:56

标签: php laravel acl

根据tutorial,我在<?php namespace App\Providers; use Illuminate\Contracts\Auth\Access\Gate as GateContract; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array */ protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any application authentication / authorization services. * * @param \Illuminate\Contracts\Auth\Access\Gate $gate * @return void */ public function boot(GateContract $gate) { parent::registerPolicies($gate); dd("duh"); $gate->define('update-contact', function ($user) { return true; }); } } 中添加了外观,并定义了如下所示的能力,但我的应用程序没有读取身份验证提供程序。以下是我的Auth服务提供商类。

no

在控制器中我做了以下但总是转到if (Gate::allows('update-contact')) { dd("allowed"); } else { dd("no"); }

make:Service

更新:我有点困惑。医生说:

  

Laravel附带的AuthServiceProvider用作   方便的位置,为您定义所有的能力   应用

我是否必须通过<?php $queries = array( 'DROP VIEW IF EXISTS Bedrijven_speci', 'CREATE VIEW Bedrijven_speci as SELECT bedrijfgegevens.id,bedrijfgegevens.subbranche_id, (6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance FROM bedrijfgegevens INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id INNER JOIN branches on branches.id = subbranches.branche_id INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen" GROUP BY bedrijfgegevens.id HAVING distance < 10', 'SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam FROM Bedrijven_speci INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id INNER JOIN branches on branches.id = subbranches.branche_id INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id' ); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->beginTransaction(); try { foreach( $queries as $q ) { $pdo->exec($q); } } catch(Exception $ex) { $pdo->rollBack(); throw $ex; } $pdo->commit(); 命令手动创建服务提供商?

2 个答案:

答案 0 :(得分:1)

如果查看authorization文档,您会看到需要传递第二个参数:

$gate->define('update-post', function ($user, $post) {
    return $user->id === $post->user_id;
});

授权检查用户是否可以对特定对象执行某些操作。在上述情况下,检查用户是否可以更新指定的$post。因此,在检查时,您需要指定第二个参数:

if (Gate::allows('update-post', $post)) {
    //
}

在您的情况下,您需要修改支票以接受应用程序中“联系人”的实例。

答案 1 :(得分:0)

如果你使用过这个 使用Illuminate \ Contracts \ Auth \ Access \ Gate作为GateContract
然后你需要调用 GateContract

if (GateContract::allows('update-contact')) {
            dd("allowed");
        }
        else {
            dd("no");
        }