1. Go to this page and download the library: Download latinexus/lx_auth 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/ */
latinexus / lx_auth example snippets
use LxAuth\LxAuth;
use LxAuth\Drivers\Database\EloquentDriver;
use Illuminate\Database\Capsule\Manager as Capsule;
// 1. Tu app resuelve el tenant y configura la conexión a su BD
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => '...',
'database' => E_DB_NAM, // 'cargo_prueba'
'username' => E_DB_USR, // 'cargo_prueba_usr'
'password' => E_DB_PWD,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
// 2. Pasar la instancia de Capsule a LX_AUTH
EloquentDriver::setCapsule($capsule);
// 3. Inicializar LX_AUTH
$auth = LxAuth::getInstance([
'tokens' => [
'jwt' => [
'secret' => 'tu-clave-secreta',
],
],
]);
// Asignar rol
$auth->assignRole($user, 'admin');
// Verificar rol
if ($auth->hasRole('admin')) {
// Es administrador
}
// Otorgar permiso directo
$auth->givePermissionTo($user, 'users.create');
// Verificar permiso
if ($auth->can('users.create')) {
// Puede crear usuarios
}
// Permisos con wildcards
if ($auth->can('users.*')) {
// Puede hacer cualquier operación con usuarios
}