1. Go to this page and download the library: Download mistery23/laravel-roles 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/ */
mistery23 / laravel-roles example snippets
'providers' => [
...
/**
* Third Party Service Providers...
*/
Mistery23\LaravelRoles\RolesServiceProvider::class,
],
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Mistery23\LaravelRoles\Traits\HasRoleAndPermission;
class User extends Authenticatable
{
use Notifiable;
use HasRoleAndPermission;
// rest of your model ...
}
if ($user->hasRole('admin')) { // you can pass an id or slug
//
}
if ($user->isAdmin()) {
//
}
if ($user->hasRole(['admin', 'moderator'])) {
/*
| Or alternatively:
| $user->hasRole('admin, moderator'), $user->hasRole('admin|moderator'),
| $user->hasOneRole('admin, moderator'), $user->hasOneRole(['admin', 'moderator']), $user->hasOneRole('admin|moderator')
*/
// The user has at least one of the roles
}
if ($user->hasRole(['admin', 'moderator'], true)) {
/*
| Or alternatively:
| $user->hasRole('admin, moderator', true), $user->hasRole('admin|moderator', true),
| $user->hasAllRoles('admin, moderator'), $user->hasAllRoles(['admin', 'moderator']), $user->hasAllRoles('admin|moderator')
*/
// The user has all roles
}
if ($user->level() > 4) {
//
}
if ($user->hasPermission('create.users')) { // you can pass an id or slug
//
}
if ($user->canDeleteUsers()) {
//
}
use App\Article;
$article = Article::find(1);
if ($user->allowed('edit.articles', $article)) { // $user->allowedEditArticles($article)
//
}
if ($user->allowed('edit.articles', $article, false)) { // now owner check is disabled
//
}
@role('admin') // @if(Auth::check() && Auth::user()->hasRole('admin'))
// user has admin role
@endrole
@permission('edit.articles') // @if(Auth::check() && Auth::user()->hasPermission('edit.articles'))
// user has edit articles permissison
@endpermission
@level(2) // @if(Auth::check() && Auth::user()->level() >= 2)
// user has level 2 or higher
@endlevel
@allowed('edit', $article) // @if(Auth::check() && Auth::user()->allowed('edit', $article))
// show edit button
@endallowed
@role('admin|moderator', true) // @if(Auth::check() && Auth::user()->hasRole('admin|moderator', true))
// user has admin and moderator role
@else
// something else
@endrole
Route::get('/', function () {
//
})->middleware('role:admin');
Route::get('/', function () {
//
})->middleware('permission:edit.articles');
Route::get('/', function () {
//
})->middleware('level:2'); // level >= 2
Route::get('/', function () {
//
})->middleware('role:admin', 'level:2'); // level >= 2 and Admin
Route::group(['middleware' => ['role:admin']], function () {
//
});
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
$userLevelCheck = $exception instanceof \Mistery23\LaravelRoles\App\Exceptions\RoleDeniedException ||
$exception instanceof \Mistery23\LaravelRoles\App\Exceptions\RoleDeniedException ||
$exception instanceof \Mistery23\LaravelRoles\App\Exceptions\PermissionDeniedException ||
$exception instanceof \Mistery23\LaravelRoles\App\Exceptions\LevelDeniedException;
if ($userLevelCheck) {
if ($request->expectsJson()) {
return Response::json(array(
'error' => 403,
'message' => 'Unauthorized.'
), 403);
}
abort(403);
}
return parent::render($request, $exception);
}
return [
/*
|--------------------------------------------------------------------------
| Package Connection
|--------------------------------------------------------------------------
|
| You can set a different database connection for this package. It will set
| new connection for models Role and Permission. When this option is null,
| it will connect to the main database, which is set up in database.php
|
*/
'connection' => env('ROLES_DATABASE_CONNECTION', null),
'usersTable' => env('ROLES_USERS_DATABASE_TABLE', 'user_users'),
'rolesTable' => env('ROLES_ROLES_DATABASE_TABLE', 'user_roles'),
'roleUserTable' => env('ROLES_ROLE_USER_DATABASE_TABLE', 'user_roles_users'),
'permissionsTable' => env('ROLES_PERMISSIONS_DATABASE_TABLE', 'user_permissions'),
'permissionsRoleTable' => env('ROLES_PERMISSION_ROLE_DATABASE_TABLE', 'user_permissions_roles'),
'permissionsUserTable' => env('ROLES_PERMISSION_USER_DATABASE_TABLE', 'user_permissions_users'),
/*
|--------------------------------------------------------------------------
| Models
|--------------------------------------------------------------------------
|
| If you want, you can replace default models from this package by models
| you created. Have a look at `Mistery23\LaravelRoles\Model\Entity\Role\Role` model and
| `Mistery23\LaravelRoles\Model\Entity\Permission\Permission` model.
|
*/
'models' => [
'role' => env('ROLES_DEFAULT_ROLE_MODEL', Mistery23\LaravelRoles\Model\Entity\Role\Role::class),
'permission' => env('ROLES_DEFAULT_PERMISSION_MODEL', Mistery23\LaravelRoles\Model\Entity\Permission\Permission::class),
'permissionRole' => env('ROLES_DEFAULT_ROLE_PERMISSION_MODEL', Mistery23\LaravelRoles\Model\Entity\RolePermission::class),
'userRole' => env('ROLES_DEFAULT_ROLE_USER_MODEL', Mistery23\LaravelRoles\Model\Entity\RoleUser::class),
'userPermission' => env('ROLES_DEFAULT_PERMISSION_MODEL', Mistery23\LaravelRoles\Model\Entity\PermissionUser::class),
'defaultUser' => env('ROLES_DEFAULT_USER_MODEL', config('auth.providers.users.model')),
],
'dependencies' => [
'userRepository' => env('USER_REPOSITORY', \App\Model\User\Entity\User\Repository\UserRepository::class),
'userQueries' => env('USER_QUERIES', \App\Model\User\Entity\User\Repository\UserQueries::class),
],
'defaultSeparator' => '.',
/*
|--------------------------------------------------------------------------
| Laravel Roles API Settings
|--------------------------------------------------------------------------
|
| This is the API for Laravel Roles to be able to CRUD them
| easily and fast via an API. This is optional and is
| not needed for your application.
|
*/
'rolesApiEnabled' => env('ROLES_API_ENABLED', true),
// Enable `auth` middleware
'rolesAPIAuthEnabled' => env('ROLES_API_AUTH_ENABLED', true),
// Enable Roles API middleware
'rolesAPIMiddlewareEnabled' => env('ROLES_API_MIDDLEWARE_ENABLED', true),
// Optional Roles API Middleware
'rolesAPIMiddleware' => env('ROLES_API_MIDDLEWARE', 'role:admin'),
];