PHP code example of techlify-inc / laravel-rbac

1. Go to this page and download the library: Download techlify-inc/laravel-rbac 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/ */

    

techlify-inc / laravel-rbac example snippets



class User extends Authenticatable
{
    use TechlifyInc\LaravelRbac\Traits\LaravelRbac;
}

use \TechlifyInc\LaravelRbac\Models\Role;

$adminRole = Role::create([
    'name' => 'Administrator',
    'slug' => 'admin'
]);

$managerRole = Role::create([
    'name' => 'Manager',
    'slug' => 'manager'
]);

use App\User;

$user = User::find(1);
$user->attachRole($adminRole);
//or you can attach using the role slug
$user->attachRole("admin");

$user->detachRole($adminRole);
//or you can remove using the role slug
$user->detachRole("admin");

use App\User;

$user = User::find(1);
if ($user->hasRole('admin')) {
    
}

use \TechlifyInc\LaravelRbac\Models\Permission;

$createPermission = Permission::create([
    'name' => 'Create product',
    'slug' => 'product.create'
]);

$removePermission = Permission::create([
    'name' => 'Delete product',
    'slug' => 'product.remove'
]);

use \TechlifyInc\LaravelRbac\Models\Role;

$adminRole = Role::find(1);
$adminRole->attachPermission($createPermission);
//or you can insert only slug
$adminRole->attachPermission("product.create");

$adminRole->detachPermission($createPermission);
$adminRole->detachPermission("product.create");

use App\User;

$user = User::find(1);
if ($user->hasPermission('product.create')) {
    
}

// OR for currently logged in user
if (auth()->user()->hasPermission('product.create'))

Route::get("customers", "CustomerController@index")->middleware("LaravelRbacEnforcePermission:customer_view");


// Get the set of users
GET api/users

// Get a single user
GET api/users/{id}

// Add a new user
POST api/users

// Update a user record
PATCH api/users/{id}

// Delete a user record
DELETE api/users/{id}



// Change the current user password
POST api/user/current/update-password {current_password, new_password}



// Log out the currently logged in user
POST api/user/logout

// Get the User record of the currently logged in user
GET api/user/current


$ php artisan migrate

$ php artisan migrate