authority-php/authority-laravel

适用于PHP的简单且灵活的授权系统

2.4.5 2017-01-28 14:30 UTC

This package is auto-updated.

Last update: 2024-09-20 01:11:38 UTC


README

适用于Laravel 5的简单且灵活的授权系统

通过Composer安装

将Authority添加到你的composer.json文件中,以引入Authority

require : {
  "laravel/framework": "~5.0.16",
  "authority-php/authority-laravel": "dev-master"
}

现在更新Composer

composer update

最后一步是向config/app.php添加服务提供者

    'Authority\AuthorityLaravel\AuthorityLaravelServiceProvider',

恭喜你,你已经成功安装了Authority。然而,我们还为你提供了一些其他方便的配置选项。

有关对Laravel 4的支持,请参阅Authority-Laravel 2.3分支

附加(可选)配置选项

将别名(外观)添加到你的Laravel应用配置文件中。
    'Authority' => 'Authority\AuthorityLaravel\Facades\Authority',

这将允许你通过你习惯的Laravel组件的静态接口访问Authority类。

  Authority::can('update', 'SomeModel');
发布Authority默认配置文件
  php artisan vendor:publish

这将把配置文件复制到config/authority.php。该配置文件包括一个'initialize'函数,这是设置规则和别名的绝佳位置。

  // config/authority.php
    <?php

  return array(

    'initialize' => function($authority) {
      $user = $authority->getCurrentUser();

      // action aliases
      $authority->addAlias('manage', array('create', 'read', 'update', 'delete'));
          $authority->addAlias('moderate', array('read', 'update', 'delete'));

          // an example using the `hasRole` function, see below examples for more details
          if ($user->hasRole('admin')){
            $authority->allow('manage', 'all');
      }
    }

  );
创建角色和权限表

我们提供了一种基本的表结构,帮助你开始创建角色和权限。

将它们发布到你的迁移目录或直接复制。

  php artisan vendor:publish

运行迁移

  php artisan migrate

这将创建以下表

  • roles
  • role_user
  • permissions

为了利用这些表,你可以在你的User模型中添加以下方法。你还需要创建角色和权限模型模板。

  // app/User.php

  public function roles() {
        return $this->belongsToMany('App\Authority\Role');
    }

    public function permissions() {
        return $this->hasMany('App\Authority\Permission');
    }

  public function hasRole($key) {
    foreach ($this->roles as $role) {
      if ($role->name === $key) {
        return true;
      }
    }

    return false;
  }

  // app/Authority/Role.php
    <?php

    use Illuminate\Database\Eloquent\Model;

  class Role extends Model {}

  // app/Authority/Permission.php
    <?php

    use Illuminate\Database\Eloquent\Model;

  class Permission extends Model {}

最后,在你的Authority配置文件中,你可以在之前的配置步骤中复制的文件中添加一些规则

  // config/authority.php
    <?php

  return array(

    'initialize' => function($authority) {

      $user = $authority->getCurrentUser();

      // action aliases
      $authority->addAlias('manage', array('create', 'read', 'update', 'delete'));
          $authority->addAlias('moderate', array('read', 'update', 'delete'));

          // an example using the `hasRole` function, see below examples for more details
          if ($user->hasRole('admin')) {
            $authority->allow('manage', 'all');
      }

      // loop through each of the users permissions, and create rules
      foreach ($user->permissions as $perm) {
        if ($perm->type == 'allow') {
          $authority->allow($perm->action, $perm->resource);
        } else {
          $authority->deny($perm->action, $perm->resource);
        }
      }
    }

  );

通用用法

  // If you added the alias to `config/app.php` then you can access Authority, from any Controller, View, or anywhere else in your Laravel app like so:
  if (Authority::can('create', 'User')) {
    User::create(array(
      'username' => 'someuser@test.com'
    ));
  }

  // If you just chose to use the service provider, you can use the IoC container to resolve your instance
  $authority = App::make('authority');

接口

为了利用Authority,你需要了解以下5个基本功能。

  • allow: 创建一条规则以允许访问资源

    示例1

        Authority::allow('read', 'User');

    示例2,使用额外的条件

        Authority::allow('manage', 'User', function($self, $user){
          return $self->getCurrentUser()->id === $user->id;
        });
  • deny: 创建一条规则以拒绝访问资源

    示例1

        Authority::deny('create', 'User');

    示例2,使用额外的条件

        Authority::deny('delete', 'User', function ($self, $user) {
              return $self->getCurrentUser()->id === $user->id;
          });
  • can: 检查用户是否可以访问资源

    示例

        Authority::can('read', 'User', $user);
  • cannot: 检查用户是否不能访问资源

    示例

        Authority::cannot('create', 'User');
  • addAlias: 将一组操作别名化

    此示例将名为manage的CRUD方法别名化

        Authority::alias('manage', array('create', 'read', 'update', 'delete'));

将此库转换为,你之前曾使用IoC容器解析实例。

服务提供者将仅创建Authority的新实例,并将当前登录用户传递给构造函数。这应该基本上与你在IoC注册表中执行的过程相同。这意味着你过去使用的任何代码都应该仍然正常工作!然而,建议你将规则定义移动到提供的配置文件中。