Laravel 5.2:集成委托包,创建角色和权限并访问它 [英] Laravel 5.2: Integrate entrust package, create role and permissions and access it

查看:95
本文介绍了Laravel 5.2:集成委托包,创建角色和权限并访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对laravel完全陌生. 我安装了laravel 5.2.我已经在Laravel中完成了CRUD.现在,我想集成laravel身份验证包.所以我选择zizaco \ entrust.

I am totally new in laravel. I install laravel 5.2 . I have done with CRUD in laravel. Now i want to integrate laravel authentication package. so i choose zizaco\entrust.

我遵循文档链接中的每个步骤.但我不明白这是怎么回事.在文档中没有提到我必须在以下文件中添加以下代码.

I follow each steps from doc link. but i don't understand what is wrong. In doc there is not mentioned that in which file i have to add following code.

$owner = new Role();
$owner->name         = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description  = 'User is the owner of a given project'; // optional
$owner->save();

$admin = new Role();
$admin->name         = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description  = 'User is allowed to manage and edit other users'; // optional
$admin->save();

以及文档中其他下面的代码.

and other below code in document.

甚至

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract

未提及工具类.

我愿意

use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

但是我遇到了错误

Trait 'App\Authenticatable' not found

新学习者无法找到放置代码的位置.我搜索了很多,但我无法获得给出正确方向的完美文档.

New learner can't get where to place code. i search alot but i can't get perfect document which give right direction.

权限在哪里创建角色?任何人都可以帮助我.

Where to create role,permissions? Anyone please help me.

推荐答案

在创建第一个给定进程后

After Creating first given process

创建角色中间件示例CheckRole

create roles middleware example CheckRole

<?php

namespace App\Http\Middleware;


use Closure;

class CheckRole
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {


        if (\Auth::user()->hasRole($role)) {

            return $next($request);
        } else {
            return response()->view('errors.401');
        }
    }
}

现在创建检查权限

<?php namespace App\Http\Middleware;

use Closure;

class CheckPermission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param $permission
     * @return mixed
     */
    public function handle($request, Closure $next, $permission)
    {
        if (\Auth::user()->can($permission)) {

            return $next($request);
        } else {
            return response()->view('errors.401');
        }
    }
}

在kernal.php中添加这些中间件

add these middlewares in kernal.php

'role' => CheckRole::class,
        'permission' => CheckPermission::class

这篇关于Laravel 5.2:集成委托包,创建角色和权限并访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆