PHP code example of nodir / laravel-oneid

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

    

nodir / laravel-oneid example snippets


// app/Models/User.php
use Nodir\OneId\Models\Traits\HasRbac;
use Nodir\OneId\Enums\UserStatus;

class User extends Authenticatable
{
    use HasRbac; // roles(), hasRole(), hasPermission() qo'shiladi

    protected function casts(): array
    {
        return [
            'status' => UserStatus::class,
        ];
    }
}

'user_model' => App\Models\User::class,

// routes/api.php

// JWT tekshirish
Route::middleware('jwt.auth')->group(function () {
    Route::get('/dashboard', DashboardController::class);
});

// Rol tekshirish
Route::middleware(['jwt.auth', 'role:admin'])->group(function () {
    Route::get('/admin/users', UserController::class);
});

// Permission tekshirish
Route::middleware(['jwt.auth', 'permission:users.manage'])->group(function () {
    Route::post('/users/{user}/block', BlockUserController::class);
});

public function index(Request $req)
{
    $user = $req->user();

    if ($user->hasRole('admin')) {
        // admin logika
    }

    if ($user->hasPermission('reports.export')) {
        // export ruxsati bor
    }

    $allPermissions = $user->allPermissions()->pluck('slug');
}

$user->assignRole('sifat_menejeri', auth()->id());
$user->removeRole('tadbirkor');
$user->activate(); // pending → active
bash
php artisan vendor:publish --tag=oneid-config
bash
php artisan vendor:publish --tag=oneid-migrations
php artisan migrate