coolsam726 /
jetstream-inertia-generator
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | @php echo "<?php" |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It is not recommend to use PHP's short opening tag
<?, better use <?php, or <?= in case of outputting.
Short opening tags are disabled in PHP’s default configuration. In such a case, all content of this file is output verbatim to the browser without being parsed, or executed. As a precaution to avoid these problems better use the long opening tag Loading history...
|
|||
| 2 | @endphp |
||
| 3 | |||
| 4 | |||
| 5 | use Carbon\Carbon; |
||
| 6 | use Illuminate\Config\Repository; |
||
| 7 | use Illuminate\Database\Migrations\Migration; |
||
| 8 | use Illuminate\Database\Schema\Blueprint; |
||
| 9 | use Illuminate\Support\Facades\DB; |
||
| 10 | use Illuminate\Support\Facades\Schema; |
||
| 11 | |||
| 12 | class {{ $className }} extends Migration |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * {{'@'}}var Repository|mixed |
||
| 16 | */ |
||
| 17 | protected $guardName; |
||
| 18 | /** |
||
| 19 | * {{'@'}}var array |
||
| 20 | */ |
||
| 21 | protected $permissions; |
||
| 22 | /** |
||
| 23 | * {{'@'}}var array |
||
| 24 | */ |
||
| 25 | protected $roles; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {{ $className }} constructor. |
||
| 29 | */ |
||
| 30 | public function __construct() |
||
| 31 | { |
||
| 32 | $this->guardName = "web"; |
||
| 33 | |||
| 34 | $permissions = collect([ |
||
| 35 | '{{$titlePlural}} Module' => '{{ $modelDotNotation }}', |
||
| 36 | 'List All {{$titlePlural}}' => '{{ $modelDotNotation }}.index', |
||
| 37 | 'Create New {{$titlePlural}}' => '{{ $modelDotNotation }}.create', |
||
| 38 | 'View Any {{$titleSingular}}' => '{{ $modelDotNotation }}.show', |
||
| 39 | 'Edit or Update a Single {{$titleSingular}}' => '{{ $modelDotNotation }}.edit', |
||
| 40 | 'Delete {{$titlePlural}}' => '{{ $modelDotNotation }}.delete', |
||
| 41 | ]); |
||
| 42 | |||
| 43 | //Add New permissions |
||
| 44 | $this->permissions = $permissions->map(function ($permission, $title) { |
||
| 45 | return [ |
||
| 46 | 'name' => $permission, |
||
| 47 | 'title' => $title, |
||
| 48 | 'guard_name' => $this->guardName, |
||
| 49 | 'created_at' => Carbon::now(), |
||
| 50 | 'updated_at' => Carbon::now(), |
||
| 51 | ]; |
||
| 52 | })->toArray(); |
||
| 53 | |||
| 54 | //Role should already exists |
||
| 55 | $this->roles = [ |
||
| 56 | [ |
||
| 57 | 'name' => 'administrator', |
||
| 58 | 'guard_name' => $this->guardName, |
||
| 59 | 'permissions' => $permissions, |
||
| 60 | ], |
||
| 61 | ]; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Run the migrations. |
||
| 66 | * |
||
| 67 | * {{'@'}}return void |
||
| 68 | */ |
||
| 69 | public function up(): void |
||
| 70 | { |
||
| 71 | $tableNames = config('permission.table_names', [ |
||
| 72 | 'roles' => 'roles', |
||
| 73 | 'permissions' => 'permissions', |
||
| 74 | 'model_has_permissions' => 'model_has_permissions', |
||
| 75 | 'model_has_roles' => 'model_has_roles', |
||
| 76 | 'role_has_permissions' => 'role_has_permissions', |
||
| 77 | ]); |
||
| 78 | // Add Title columns in case they don't exist. |
||
| 79 | $roles = $tableNames['roles']; |
||
| 80 | $permissions = $tableNames['permissions']; |
||
| 81 | Schema::table($roles, function (Blueprint $table) use($roles) { |
||
| 82 | $hasTitle = Schema::hasColumn($roles,"title"); |
||
| 83 | if (!$hasTitle) $table->string('title')->nullable(); |
||
| 84 | }); |
||
| 85 | Schema::table($permissions, function (Blueprint $table) use ($permissions) { |
||
| 86 | if (!Schema::hasColumn($permissions,"title")) { |
||
| 87 | $table->string('title')->nullable(); |
||
| 88 | } |
||
| 89 | }); |
||
| 90 | // End add title |
||
| 91 | |||
| 92 | DB::transaction(function () use($tableNames) { |
||
| 93 | foreach ($this->permissions as $permission) { |
||
| 94 | $permissionItem = DB::table($tableNames['permissions'])->where([ |
||
| 95 | 'name' => $permission['name'], |
||
| 96 | 'guard_name' => $permission['guard_name'] |
||
| 97 | ])->first(); |
||
| 98 | if ($permissionItem === null) { |
||
| 99 | DB::table($tableNames['permissions'])->insert($permission); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | foreach ($this->roles as $role) { |
||
| 104 | $permissions = $role['permissions']; |
||
| 105 | unset($role['permissions']); |
||
| 106 | |||
| 107 | $roleItem = DB::table($tableNames['roles'])->where([ |
||
| 108 | 'name' => $role['name'], |
||
| 109 | 'guard_name' => $role['guard_name'] |
||
| 110 | ])->first(); |
||
| 111 | if ($roleItem !== null) { |
||
| 112 | $roleId = $roleItem->id; |
||
| 113 | |||
| 114 | $permissionItems = DB::table($tableNames['permissions'])->whereIn('name', $permissions)->where( |
||
| 115 | 'guard_name', |
||
| 116 | $role['guard_name'] |
||
| 117 | )->get(); |
||
| 118 | foreach ($permissionItems as $permissionItem) { |
||
| 119 | $roleHasPermissionData = [ |
||
| 120 | 'permission_id' => $permissionItem->id, |
||
| 121 | 'role_id' => $roleId |
||
| 122 | ]; |
||
| 123 | $roleHasPermissionItem = DB::table($tableNames['role_has_permissions'])->where($roleHasPermissionData)->first(); |
||
| 124 | if ($roleHasPermissionItem === null) { |
||
| 125 | DB::table($tableNames['role_has_permissions'])->insert($roleHasPermissionData); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | }); |
||
| 131 | app()['cache']->forget(config('permission.cache.key')); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Reverse the migrations. |
||
| 136 | * |
||
| 137 | * {{'@'}}return void |
||
| 138 | */ |
||
| 139 | public function down(): void |
||
| 140 | { |
||
| 141 | $tableNames = config('permission.table_names', [ |
||
| 142 | 'roles' => 'roles', |
||
| 143 | 'permissions' => 'permissions', |
||
| 144 | 'model_has_permissions' => 'model_has_permissions', |
||
| 145 | 'model_has_roles' => 'model_has_roles', |
||
| 146 | 'role_has_permissions' => 'role_has_permissions', |
||
| 147 | ]); |
||
| 148 | DB::transaction(function () use ($tableNames){ |
||
| 149 | foreach ($this->permissions as $permission) { |
||
| 150 | $permissionItem = DB::table($tableNames['permissions'])->where([ |
||
| 151 | 'name' => $permission['name'], |
||
| 152 | 'guard_name' => $permission['guard_name'] |
||
| 153 | ])->first(); |
||
| 154 | if ($permissionItem !== null) { |
||
| 155 | DB::table($tableNames['permissions'])->where('id', $permissionItem->id)->delete(); |
||
| 156 | DB::table($tableNames['model_has_permissions'])->where('permission_id', $permissionItem->id)->delete(); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | }); |
||
| 160 | app()['cache']->forget(config('permission.cache.key')); |
||
| 161 | } |
||
| 162 | } |
||
| 163 |