henriale/stinter

Laravel 的计划/订阅审计器。

v1.0.2 2016-04-12 02:39 UTC

This package is not auto-updated.

Last update: 2024-09-25 23:09:01 UTC


README

Laravel 的计划/资源审计器。

安装

1 - 首先,使用 composer 将包放入您的项目中

$ composer require henriale/stinter
$ composer composer update

2 - 然后,创建一个 /config/stinters.php 文件或者使用以下命令发布它

$ php artisan vendor:publish --provider="Henriale\Stinter\StintServiceProvider"

入门

1 - 创建一个限制/限制类来控制您的资源

$ php artisan make:stint CreateProduct

2 - 使用它进行一些验证

class CreateProduct extends Stinter
{
  protected $basicPlanLimitation = 10;
  
  public function check(Authenticatable $user)
  {
      if ($user->products()->count() >= $this->basicPlanLimitation) {
        // user has too many products
        return false;
      }
      
      //user still can have more products
      return true;
  }
}

3 - 现在,在 /config/stinters.php 中注册它,以便审计器在触发时进行检查

return [
    \App\Stinters\CreateProduct::class
];

4 - 最后,使用 \Gate 类并通过传递限制 FQN 来处理其权限

<!-- using User model -->
<?php
if ($user->can(\App\Stinters\CreateProduct::class)) {
  echo 'yes, he is able';
}
?>

<!-- using Gate -->
<?php
$userCan = \Gate::allows(\App\Stinters\CreateProduct::class);
if ($userCan) {
  echo "user can create more stuffs";
} else {
  echo "user cannot create more stuffs. he better upgrade his plan/subscription!";
} 
?>

<!-- using blade -->
@can(\App\Stinters\CreateProduct::class)
  // can
@else
  // cannot
@endcan