orchid/access

此包已被废弃且不再维护。未建议替代包。

Laravel 框架的角色基础访问控制包。

1.0.1 2015-12-14 20:04 UTC

This package is auto-updated.

Last update: 2022-02-21 22:18:43 UTC


README

Laravel 的角色基础访问控制包

Latest Stable Version Total Downloads License

安装

  1. 安装包

    composer require orchid/access
  2. 编辑 config/app.php

    服务提供者

     Orchid\Access\Providers\SettingsServiceProvider::class
  3. 创建 user\roles 表

    php artisan vendor:publish
    php artisan migrate

使用方法

/**
* User
*/


use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Database\Eloquent\Model;
use Orchid\Access\Traits\UserAccess;


class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{

    use Authenticatable, CanResetPassword, UserAccess;

    /**
     * @var
     */
    protected static $rolesModel = Roles::class;


    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * @var array
     */
    protected $fillable = [
        'email',
        'permissions',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];


}








// It checks the availability of a user in the roles and permissions
Auth:user()->hasAccess('param');

// Get access for the user
Auth:user()->getPermissionsAttribute($permissions);

// Set access for the user
Auth:user()->setPermissionsAttribute($permissions)

// Get roles for the user
Auth:user()->getRoles();

// Check user has role
Auth:user()->inRole($role)

// Add Role for user
Auth:user()->addRole($role)
/**
* Middleware
*/

// To check on each controller add middleware in /Http/Kernel.php
        'Access'     => \Orchid\Access\Middleware\AccessMiddleware::class,
/**
* Role
*/

// Model Role

use Illuminate\Database\Eloquent\Model;
use Orchid\Access\Traits\RoleAccess;

class Roles extends Model
{

    use RoleAccess;

    /**
     * @var
     */
    protected static $usersModel = User::class;

    /**
     * @var string
     */
    protected $table = 'roles';

    /**
     * @var array
     */
    protected $fillable = [
        'id',
        'name',
        'slug',
        'permissions',
    ];

}






$role = Role::getRoleSlug('string');

// Will return all users with that role
$role->getUsers();

// Get access for the role
$role->getPermissionsAttribute($permissions);

// Set access for the role
$role->setPermissionsAttribute($permissions);


许可证

MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。