1. Go to this page and download the library: Download trunow/rpac 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/ */
trunow / rpac example snippets
'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
Illuminate\Auth\AuthServiceProvider::class,
...
/**
* Third Party Service Providers...
*/
Trunow\Rpac\RpacServiceProvider::class,
],
use Trunow\Rpac\Traits\Rpacable;
class User extends Model implements AuthenticatableContract
{
use Authenticatable, Rpacable;
namespace App\Policies;
use Trunow\Rpac\Policies\RpPolicy;
class PostPolicy extends RpPolicy
{
//...
}
use App\User;
$user = User::find($id);
$user->roles()->attach($adminRole); // you can pass whole object, or just an id
$user->roles()->detach($adminRole); // in case you want to detach role
$user->roles()->detach(); // in case you want to detach all roles
if ($user->is('admin')) { // pass role slug here
// ...
}
if ($user->isAdmin()) {
//
}
if ($user->is(['admin', 'moderator'])) {
/*
| It is same as:
| $user->isOr(['admin', 'moderator'])
*/
// if user has at least one role
}
if ($user->is(['admin', 'moderator'], true)) {
/*
| Or alternatively:
| $user->isAnd(['admin', 'moderator'])
*/
// if user has all roles
}
use Trunow\Rpac\Role;
$role = Role::find($roleId);
$role->permissions()->attach($createPostPermission); // permission attached to a role
$role->permissions()->detach($createPostPermission); // in case you want to detach permission
$role->permissions()->detach(); // in case you want to detach all permissions
use App\Article;
use Trunow\Rpac\Permission;
$editArticlesPermission = Permission::create([
'name' => 'Edit articles',
'entity' => 'App\Article',
'action' => 'edit',
]);
$user->roles()->first()->permissions()->attach($editArticlesPermission);
$article = Article::find(1);
if ($user->can('edit', $article)) {
//
}
@role('admin') // @if(Auth::check() && Auth::user()->is('admin'))
// user is admin
@endrole