Skip to content

Commit

Permalink
Merge pull request #1022 from uasoft-indonesia/bugfix/v3/add-generate…
Browse files Browse the repository at this point in the history
…-for-table-crud

bugfix [v3] add generate for table crud
  • Loading branch information
rizkiheryandi authored Sep 18, 2023
2 parents f388fac + 2ed51b4 commit 8fbcd11
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
61 changes: 39 additions & 22 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,31 @@ public function __construct(array $attributes = [])

public static function generateFor($table_name, $is_maintenance = false)
{
$permissions = [];
$permissions[] = self::firstOrCreate(['key' => 'browse_'.$table_name, 'description' => 'Browse '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'read_'.$table_name, 'description' => 'Read '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'edit_'.$table_name, 'description' => 'Edit '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'add_'.$table_name, 'description' => 'Add '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'delete_'.$table_name, 'description' => 'Delete '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);

if ($is_maintenance) {
$permissions[] = self::firstOrCreate(['key' => 'maintenance_'.$table_name, 'description' => 'Maintenance '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
}
if (! Permission::where('table_name', $table_name)->first()) {
$permissions = [];
$permissions[] = self::firstOrCreate(['key' => 'browse_'.$table_name, 'description' => 'Browse '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'read_'.$table_name, 'description' => 'Read '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'edit_'.$table_name, 'description' => 'Edit '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'add_'.$table_name, 'description' => 'Add '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
$permissions[] = self::firstOrCreate(['key' => 'delete_'.$table_name, 'description' => 'Delete '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);

if ($is_maintenance) {
$permissions[] = self::firstOrCreate(['key' => 'maintenance_'.$table_name, 'description' => 'Maintenance '.$table_name, 'table_name' => $table_name, 'roles_can_see_all_data' => '["administrator"]', 'field_identify_related_user' => 'user_id']);
}

$administrator = Role::where('name', 'administrator')->firstOrFail();

if (! is_null($administrator)) {
foreach ($permissions as $row) {
$role_permission = RolePermission::where('role_id', $administrator->id)
->where('permission_id', $row->id)
->first();
if (is_null($role_permission)) {
$role_permission = new RolePermission();
$role_permission->role_id = $administrator->id;
$role_permission->permission_id = $row->id;
$role_permission->save();
$administrator = Role::where('name', 'administrator')->firstOrFail();

if (! is_null($administrator)) {
foreach ($permissions as $row) {
$role_permission = RolePermission::where('role_id', $administrator->id)
->where('permission_id', $row->id)
->first();
if (is_null($role_permission)) {
$role_permission = new RolePermission();
$role_permission->role_id = $administrator->id;
$role_permission->permission_id = $row->id;
$role_permission->save();
}
}
}
}
Expand Down Expand Up @@ -95,4 +97,19 @@ public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()->dontSubmitEmptyLogs();
}

public static function generateForTableCRUD()
{
$get_all_table_name = DataType::all();
foreach ($get_all_table_name as $key => $table) {
$permission_table_name = Permission::where('table_name', $table->name)->get();
foreach ($permission_table_name as $key => $table_name) {
if ($table->roles_can_see_all_data == null && $table_name->field_identify_related_user == null) {
$table_name->roles_can_see_all_data = '["administrator"]';
$table_name->field_identify_related_user = 'user_id';
$table_name->save();
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/Seeder/Configurations/PermissionsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ public function run()
Permission::generateFor('configurations');

Permission::generateFor('database');

Permission::generateForTableCRUD();
}
}

0 comments on commit 8fbcd11

Please sign in to comment.