PHP code example of napp / aclcore

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

    

napp / aclcore example snippets


return [
    /**
     * Define which Eloquent models used by the package
     */
    'models' => [
        'role' => Napp\Core\Acl\Model\Role::class,
        'user' => Illuminate\Foundation\Auth\User::class,
    ],

    /**
     * Table names for the package
     */
    'table_names' => [
        'roles' => 'roles',
        'users_roles' => 'users_roles',
    ],

    /**
     * The default guard used to authorize users
     */
    'guard' => 'web'
];

use Illuminate\Foundation\Auth\User as Authenticatable;
use Napp\Core\Acl\Contract\Role as RoleContract;
use Napp\Core\Acl\Role\HasRole;

class User extends Authenticatable implements RoleContract
{
    use HasRole;
}

Napp\Core\Acl\PermissionRegistrar::register([
    'users.create', 
    'users.view'
]);

Napp\Core\Acl\PermissionRegistrar::register([
    'users.create' => 'My\App\Users\Permissions@create',
    'users.update' => 'My\App\Users\Permissions@edit',
    'users.view'
]);

protected $routeMiddleware = [
    'may' => \Napp\Core\Acl\Middleware\Authorize::class,

Route::get('users', ['uses' => 'UsersController@index'])->middleware('may:users.view');


// authorize a single permission
if (may('users.view')) {
    // do something
}

// authorize if **any** of the permissions are valid
if (may(['users.view', 'users.create'])) {
    // do something
}

// authorize if **all** of the permissions are valid
if (mayall(['users.view', 'users.create'])) {
    // do something
}

// reverse - not logic
if (maynot('users.view')) {
    return abort();
}

// check for user role
if (has_role($user, 'manager')) {
    // do something
}

// check if user has many roles
if (has_role($user, ['support', 'hr'])) {
    // do something
}


@may('users.create')
    <a href="my-link">Create</a>
@endmay

@may(['users.create', 'users.update'])
    <a href="my-link">Create</a>
@endmay

@mayall(['users.create', 'users.update'])
    <a href="my-link">Create</a>
@endmayall

@maynot('users.create')
    <a href="my-link">Create</a>
@endmaynot

@hasrole('admin')
    <a href="my-link">Create</a>
@endhasrole
bash
php artisan vendor:publish --provider="Napp\Core\Acl\AclServiceProvider" --tag="config"