PHP code example of xdroidteam / xtrust
1. Go to this page and download the library: Download xdroidteam/xtrust 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/ */
xdroidteam / xtrust example snippets
XdroidTeam\XTrust\XTrustServiceProvider::class,
'XTrust' => XdroidTeam\XTrust\XTrust::class,
'permission' => \XdroidTeam\XTrust\Middleware\XTrustPermissionMiddleware::class,
namespace App\Models;
use XdroidTeam\XTrust\Models\XTrustRole;
class Role extends XTrustRole
{
...
}
namespace App\Models;
use XdroidTeam\XTrust\Models\XTrustPermission;
class Permission extends XTrustPermission
{
...
}
use XdroidTeam\XTrust\Traits\XTrustUserTrait;
class User extends Eloquent
{
use XTrustUserTrait; // add this trait to your user model
...
}
XTrust::hasPermission('can_delete');
XTrust::hasPermissions(['can_delete', 'can_edit']);
XTrust::hasOneOfPermissions(['can_delete', 'can_edit']);
$user = User::find(1);
$user->hasPermission('can_delete');
// OR
$user->hasPermissions(['can_delete', 'can_edit']);
// OR
$user->hasOneOfPermissions(['can_delete', 'can_edit']);
Route::group(['middleware' => ['auth', 'permission:can_show']], function(){
Route::get('/', 'HomeController@index');
});
Route::group([
'middleware' => [
'auth',
'permission:can_show|can_create|can_edit|can_delete'
]
], function(){
Route::get('/admin', 'AdminController@index');
});
Route::group([
'middleware' => [
'auth',
'permission:can_show',
'permission:can_create'
]
], function(){
Route::get('/admin', 'AdminController@index');
});
@permission('can_delete')
{!! Form::open([
'url' => '/users/'.$user->id,
'method'=> "DELETE"
] ) !!}
<button class="btn btn-sm btn-danger">
Delete
</button>
{!! Form::close() !!}
@endpermission
@permissions(['can_show', 'can_delete'])
<span>Something</span>
@endpermissions
@oneofpermissions(['can_show', 'can_delete'])
<span>Something</span>
@endoneofpermissions
XTrust::hasRole('can_delete');
XTrust::hasRoles(['can_delete', 'can_edit']);
XTrust::hasOneOfRoles(['can_delete', 'can_edit']);
$user = User::find(1);
$user->hasRole('can_delete');
// OR
$user->hasRoles(['can_delete', 'can_edit']);
// OR
$user->hasOneOfRoles(['can_delete', 'can_edit']);
$user->attachRole(1);
$user->attachRoles([1,2]);
$user->attachPermission(1);
$user->attachPermissions([1,2]);
$user->detachRole(1);
$user->detachRoles([1,2]);
$user->detachPermission(1);
$user->detachPermissions([1,2]);
$role->attachPermission(1);
$role->attachPermissions([1,2]);
$role->detachPermission(1);
$role->detachPermissions([1,2]);
bash
php artisan vendor:publish --tag=xdroidteam-xtrust
bash
php artisan migrate
bash
composer dump-autoload