Skip to content

Instantly share code, notes, and snippets.

@zalviandyr
Last active May 22, 2023 06:04
Show Gist options
  • Save zalviandyr/1982e2e60faaf68c517a72791e5cb1ea to your computer and use it in GitHub Desktop.
Save zalviandyr/1982e2e60faaf68c517a72791e5cb1ea to your computer and use it in GitHub Desktop.
vendor/alurkerja-laravolt/rbac/src/Commands/SyncUrlPermissions.php
<?php
namespace Laravolt\Rbac\Commands;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Laravolt\Rbac\Models\Permission;
use Laravolt\Crud\Helper\TableSpecHelper;
use Laravolt\Crud\Schema\ColumnFactory;
class SyncUrlPermissions extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'alurkerja:sync-url-permission';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$routeCollection = Route::getRoutes();
$data = [];
$permission = [];
foreach ($routeCollection as $value) {
if(str_contains( $value->uri,"api")) {
$seg = explode("/" , $value->uri);
if(sizeof($seg) < 3) {
continue;
}
if($seg[0] != 'api') {
continue;
}
if($seg[1] == 'crud') {
continue;
}
if($seg[1] == 'bpmn') {
continue;
}
$module = $seg[1];
$table = $seg[2];
$segUrl = $module. "/" . $table;
if(!in_array($segUrl , $data)) {
$data[] = $segUrl;
foreach (['LIST', 'CREATE', 'SHOW', 'DELETE', 'UPDATE'] as $item) {
$this->info("Generating Permission For " . ucfirst( $item ) . " Action For Module " .ucfirst($module) . " Table " .ucfirst($table));
// dynamic model
$permissionModel = app(config('rbac.models.permission'));
$permissionModel
->newModelQuery()
->updateOrCreate([
'name' => strtoupper( $item ) . "_" .strtoupper($module) . "_" .strtoupper($table),
], [
'module' => $module,
'table' => $table,
'action' => $item,
'description' => ucfirst( $item ) . " Action For Module " .ucfirst($module) . " Table " .ucfirst($table),
]);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment