farbesofts/checkrole

Laravel 的 API 库

1.0 2019-03-18 06:16 UTC

This package is auto-updated.

Last update: 2024-09-20 04:23:55 UTC


README

Laravel 5.5 Source License

checkRole 是一个简单轻量级的库,为开发者在创建用户对系统特定角色的个性化时间表登录限制时提供了一个有用的工具。

  • 每个用户只有一个定义好的时间表。

文档

(即将推出)

快速安装

首先通过 Composer 安装该包。最佳做法是通过终端直接使用 Composer 进行操作

composer require farbesofts/checkrole

配置

要发布配置文件和 NotAccess 视图,请运行以下命令

php artisan vendor:publish

选择库所在的选项,在我的例子中

[2] Provider: Farbesofts\Checkrole\CheckroleServiceProvider

服务提供者

  • 在 config.app (数组 Providers) 中复制以下内容
Farbesofts\Checkrole\CheckroleServiceProvider::class,

中间件 kernel.php

  • 在 App\Http\Kernel.php -> (数组 $routeMiddleware) 中复制
'CheckRole' => \Farbesofts\Checkrole\CheckroleServiceProvider::class,

迁移

  • 迁移模型
php artisan make:auth
php artisan migrate

如果您使用的是角色和权限库,在迁移之前,请先在 config/checkrole.php 中将 'run-migrations' => false 改为 false,并且只您迁移

2019_03_16_160254_create_timetables_table

用户模型上的头信息

  • 复制到 App\User.php 模型
use Farbesofts\Checkrole\Models\Role;
use Farbesofts\Checkrole\Models\Timetable;
use Illuminate\Support\Facades\Auth;

用户模型上的方法

    public function roles()
    {
        return $this->belongsToMany(Role::class)->withTimestamps();
    }

    public function hasRole($role)
    {
        if ($this->roles()->where('name', $role)->first()) {
            return  true;
        }
        return false;
    }

    public function Timetable(){
        return $this->hasOne(Timetable::class);
    }

    public function getTimetable(){
        return $this->Timetable()->where('user_id',Auth::user()->id)->first();
    }

使用 CheckRole 中间件的路由

  • 复制到 routes\web.php: 示例 (角色:admin)
Route::get('/notaccess', function () {
    return view('notaccess');
});
Route::group(['middleware' => 'CheckRole:admin'], function() {
    Route::get('/home', 'HomeController@index')->name('home');
});