PHP code example of cloty / cloty-entrust
1. Go to this page and download the library: Download cloty/cloty-entrust library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cloty / cloty-entrust example snippets
Cloty\Entrust\Providers\EntrustServiceProvider::class,
'Entrust' => Cloty\Entrust\Facades\EntrustFacade::class,
'entrust.role' => \Cloty\Entrust\Middleware\EntrustRole::class,
'entrust.permission' => \Cloty\Entrust\Middleware\EntrustPermission::class,
'entrust.ability' => \Cloty\Entrust\Middleware\EntrustAbility::class,
'cache_driver' => 'redis', // 建议配置为`redis`,由于使用Tag缓存角色关系,`file`是不支持的
'cfc' => 1 // 默认值1,中间件验证失败时返回abort(403);
use Cloty\Entrust\EntrustRole;
use Cloty\Entrust\EntrustPermission;
use Cloty\Entrust\EntrustPermissionRole;
use Cloty\Entrust\Traits\EntrustUserTrait;
class User extends Eloquent
{
use EntrustUserTrait;
...
}
...
use Entrust;
class TestController extends Controller
{
...
}
$admins = [
'name' => 'admin',
'display_name' => 'Administrator', //选填
'description' => 'given project', //选填
];
$admin = Entrust::createRole($admins);
$user = User::where('username', 'cloty')->first();
// 方法
$user->attachRole($admin); // 参数可以是EntrustRole对象、数组或id
// 或者laravel框架原生的关系绑定
$user->entrustRoles()->attach($admin->id); // 只能是角色ID
//or
Entrust::attachUserRole($user->id, $admin->id); //uids参数 int|array
$permissions = [
'name' => 'post',
'display_name' => 'Posts',//选填
'description' => 'posts',//选填
'p_id' => 0, //选填,默认为0
];
$permission = Entrust::createPermission($permissions);
$admin->attachPermission($permission); // 参数可以是EntrustPermission对象、数组或id
// 相当于 $admin->perms()->sync(array($permission->id));
$user->hasRole('owner'); // false
$user->hasRole('admin'); // true
$user->canDo('post_add'); // false
$user->canDo('post'); // true
$user->hasRole(['owner', 'admin']); // true
$user->canDo(['post_add', 'post']); // true
$user->hasRole(['owner', 'admin']); // true
$user->hasRole(['owner', 'admin'], true); // false
$user->canDo(['post_add', 'post']); // true
$user->canDo(['post_add', 'post'], true); // false
Entrust::hasRole('role-name');
Entrust::canDo('permission-name');
// 等同于
Auth::user()->hasRole('role-name');
Auth::user()->canDo('permission-name');
// match any admin permission
$user->canDo("admin.*"); // true
// match any permission about users
$user->canDo("*_users"); // true
$user->ability(['admin', 'cloty'], ['post', 'post_create', 'post_create_detail']);
// 或
$user->ability('admin,owner', 'post,post_create,post_create_detail');
$options =[
'validate_all' => true | false (Default: false),
'return_type' => boolean | array | both (Default: boolean)
];
$options = [
'validate_all' => true,
'return_type' => 'both'
];
list($validate, $allValidations) = $user->ability(
['admin', 'owner'],
['post', 'post_create'],
$options
);
var_dump($validate);
// bool(false)
var_dump($allValidations);
// array(4) {
// ['admin'] => bool(true)
// ['owner'] => bool(false)
// ['post'] => bool(true)
// ['post_create'] => bool(false)
// }
Entrust::ability('admin,owner', 'post,post_create');
// 相当于
Auth::user()->ability('admin,owner', 'post,post_create');
@role('admin')
<p>具有`admin` 角色的用户可以看到这一点.渲染时被替换成
\Entrust::hasRole('admin')</p>
@endrole
@permission('post')
<p>具有给定权限的用户可以看到这一点.渲染时被替换成
\Entrust::canDo('post').</p>
@endpermission
@ability('admin,owner', 'post,post_create')
<p>具有给定abilities的用户可以看到. 渲染时被替换成
\Entrust::ability('admin,owner', 'post,post_create')</p>
@endability
Route::group(['prefix' => 'admin', 'middleware' => ['entrust.role:admin']], function() {
Route::get('/', 'AdminController@welcome');
Route::get('/manage', ['middleware' => ['entrust.permission:manage_admins'], 'uses' => 'AdminController@manageAdmins']);
});
'middleware' => ['entrust.role:admin|root']
'middleware' => ['entrust.role:owner', 'entrust.role:writer']
'middleware' => ['entrust.ability:admin|owner,post|post_create,true']
shell
php artisan vendor:publish
bash
php artisan migrate