PHP code example of muan / laravel-acl

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

    

muan / laravel-acl example snippets


'providers' => [
    // ...
    Muan\Acl\AclServiceProvider::class,
    // ...
],

// ...

use Muan\Acl\Traits\{HasRolesTrait, HasPermissionsTrait};
 
class User extends Authenticatable
{
    use HasRolesTrait, HasPermissionsTrait;
    
    // ... Your User Model Code
}

class User extends Authenticatable
{
    // ...
    
    /**
     * Attach base role
     */
    public $baseRole = 'user';
    
    // ...
}

if ($user->hasRole('admin')) {
    // User is admin
}
// or
if ($user->hasRole('admin', 'writer')) {
    // User is admin or writer
}

$user->attachRole(10, "moderator")

$user->detachRole('moder');
// ...
$user->detachRole('admin', '3', '2');

$user->clearRoles();

if ($user->hasPermission('create post')) {
    // User has permission "create post"
}

$user->attachPermission("update post");

$user->detachPermission("remove post");

$user->clearPermissions();

Route::middleware(['role:admin'])->group(function() {
    
    // Only for user with role admin
    Route::get('/admin', function() {
        // some code
    });

});

Route::middleware(['permission:create post'])->group(function() {
    
    // Only for user with permission create post
    Route::get('/admin/post', function() {
        // some code
    });
    
});

Route::middleware(['role:moderator', 'permission:remove post'])->group(function() {
    
    // Only for user with role moderator and with permission create post
    Route::get('/admin/post/remove', function() {
        // some code
    });
    
});
bash
php artisan migrate
bash
php artisan permission:add "create post"
bash
php artisan permission:rename "create post" create.post
bash
php artisan permission:remove "create post"
bash
php artisan permission:list
bash
php artisan role:add admin
bash
php artisan role:rename admin superuser
bash
php artisan role:remove admin
bash
php artisan role:list
bash
php artisan role:attach admin --id=2 --id=3 --name="create post"
bash
php artisan role:detach admin --id=3 --name="destroy user"
bash
php artisan role:clear
bash
php artisan role:view admin
bash
php artisan user:role-attach 5 --id=2 --name=moderator
bash
php artisan user:role-detach 5 --id=2 --name=admin
bash
php artisan user:role-clear
bash
php artisan user:permission-attach 5 --id=7 --name="remove comment"
bash
php artisan user:permission-detach 5 --id=2 --name="read secret post"
bash
php artisan user:permission-clear
bash
php artisan user:view 5
blade
@can('create post')
    <!-- User can create post -->
@elsecan('edit post')
    <!-- User can edit post  -->
@endcan