Package Data | |
---|---|
Maintainer Username: | seancheung |
Maintainer Contact: | theoxuanx@gmail.com (Sean) |
Package Create Date: | 2016-11-21 |
Package Last Update: | 2017-02-14 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:20:29 |
Package Statistics | |
---|---|
Total Downloads: | 17 |
Monthly Downloads: | 4 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Role and Permission control for Laravel
composer require panoscape/access
config/app.php
'providers' => [
...
Panoscape\Access\AccessServiceProvider::class,
];
config/app.php
'aliases' => [
...
'Access' => Panoscape\Access\Facades\Access::class,
];
config/app.php
'aliases' => [
...
'App\Permission' => Panoscape\Access\Permission::class,
'App\Role' => Panoscape\Access\Role::class,
];
app/Http/Kernel.php
protected $routeMiddleware = [
...
'access' => \Panoscape\Access\Middleware\VerifyAccess::class,
];
php artisan vendor:publish --provider="Panoscape\Access\AccessServiceProvider" --tag=migrations
Before migrating, you'll need to modify the users
table in the published migration file to the correct user table used in your application
//TODO: user table
$table->foreign('user_id')->references('id')->on('users')
Add HasRoles
trait to user model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Panoscape\Access\HasRoles;
class User extends Authenticatable
{
use Notifiable, SoftDeletes, HasRoles;
}
$user->roles();
$user->permissions();
$role->permissions();
$role->users();
$permission->roles();
You may also use dynamic properties
$user->roles;
$user->permissions;
$role->permissions;
$role->users;
$permission->roles;
//return true if the user has this role
$user->hasRoles('admin');
//return true if the user has all three roles
$user->hasRoles(['admin', 'editor', 'author']);
//equivalent to array
$user->hasRoles('admin|editor|author');
//return true if the user has any of the three roles
$user->hasRoles(['admin', 'editor', 'author'], false);
//by default it checks 'name' column; you may specify which column to check
$user->hasRoles([1, 3, 12], true, 'id');
//or check permissions
$user->hasPermissions('edit_users');
//also available on role
$role->hasPermissions('edit_users');
//by name
$user->attachRoles('admin');
//by id
$user->attachRoles(1);
//by model instance
$user->attachRoles($role);
//with array
$user->attachRoles(['admin', 'editor']);
$user->attachRoles([1, 2]);
//specify column
$user->attachRoles(['1', '2'], 'id');
//detach
$user->detachRoles('admin');
//detach all
$user->detach([]);
//sync
$user->syncRoles('admin');
//detach all
$user->syncRoles([]);
//sync without detaching
$user->syncRoles(['admin', 'editor'], false);
//same with role and permissions
$role->attachPermissions('editor_users');
$role->detachPermissions('editor_users');
$role->syncPermissions('editor_users');
//check the current authenticated user's roles and permissions
Access::hasRoles('admin');
Access::hasPermissions('edit_users');
//check the given user's roles and permissions
Access::hasRoles('admin', true, 'name', $user);
Access::hasPermissions('edit_users', true, 'name', $user);
//attach roles to current authenticated user
Access::attachRoles('admin');
Access::attachRoles(['admin', 'editor']);
//attach roles to the given user
Access::attachRoles('admin', 'name', $user);
Access::attachRoles(['admin', 'editor'], 'name', $user);
//role
Route::get('/dashboard', 'DashboardController@index')->middleware('access:roles,admin');
//permission
Route::get('/dashboard', 'DashboardController@index')->middleware('access:permissions,edit_users');
//multiple
Route::get('/dashboard', 'DashboardController@index')->middleware('access:permissions,edit_users|manage_sites');
//requirement and column
Route::get('/dashboard', 'DashboardController@index')->middleware('access:permissions,1|3,false,id');
@roles('admin')
<div>
...
<div>
@endroles
@permissions('edit_users|Manage_sites')
<div>
...
<div>
@endpermissions
@roles('admin|editor|root', false)
<div>
...
<div>
@endroles
@permissions('1|2|3', false, 'id')
<div>
...
<div>
@endpermissions
composer test
or
vendor/bin/phpunit
auth()->user()
access
middleware's first argument to plural format: role
to roles
, permission
to permissions
roles
and permissions
blade directives failure issue