1. Go to this page and download the library: Download cuongnd88/lara-guardian 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/ */
Route::group(['middleware' => ['guardian']], function(){
Route::get('/user', function(){
dump("Congratulation. You have the right permission");
})->name('user.read');
});
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Guardian\Traits\HasGuardian;
class User extends Authenticatable
{
use Notifiable;
use HasGuardian;
}
public function joinGroup(Request $request)
{
$user = \App\Models\User::find(10);
$user->joinGroup(2);
}
public function joinManyGroups(Request $request)
{
$user = \App\Models\User::find(10);
$user->joinMultiGroups([
['group_id' => 1],
['group_id' => 3],
]);
}
public function getUserPermissions(Request $request)
{
$user = \App\Models\User::find(10);
$user->hasPermissions()->toArray();
}
public function checkUserAccess(Request $request)
{
$user = \App\Models\User::find(10);
if ($user->rightAccess('product', 'create')) {
dump('Right Access');
} else {
dump('Forbidden');
}
}