141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Database\Migrations;
|
|
|
|
use CodeIgniter\Database\Migration;
|
|
|
|
class GrantEnrollmentExceptionPermissionToAdminRoles extends Migration
|
|
{
|
|
private string $permissionName = 'enrollment.exception.manage';
|
|
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
private array $roleNames = [
|
|
'administrator',
|
|
'admin',
|
|
'principal',
|
|
'vice principal',
|
|
'vice_principal',
|
|
];
|
|
|
|
public function up(): void
|
|
{
|
|
if (
|
|
! $this->db->tableExists('roles')
|
|
|| ! $this->db->tableExists('permissions')
|
|
|| ! $this->db->tableExists('role_permissions')
|
|
) {
|
|
return;
|
|
}
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
$permission = $this->db->table('permissions')
|
|
->select('id')
|
|
->where('name', $this->permissionName)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($permission === null) {
|
|
$insert = [
|
|
'name' => $this->permissionName,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
];
|
|
if ($this->db->fieldExists('description', 'permissions')) {
|
|
$insert['description'] = 'Manage scoped enrollment eligibility exceptions.';
|
|
}
|
|
|
|
$this->db->table('permissions')->insert($insert);
|
|
$permissionId = (int) $this->db->insertID();
|
|
} else {
|
|
$permissionId = (int) $permission['id'];
|
|
}
|
|
|
|
if ($permissionId <= 0) {
|
|
return;
|
|
}
|
|
|
|
$roles = $this->db->table('roles')
|
|
->select('id')
|
|
->whereIn('name', $this->roleNames)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
foreach ($roles as $role) {
|
|
$roleId = (int) ($role['id'] ?? 0);
|
|
if ($roleId <= 0) {
|
|
continue;
|
|
}
|
|
|
|
$existing = $this->db->table('role_permissions')
|
|
->where('role_id', $roleId)
|
|
->where('permission_id', $permissionId)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
$grant = [
|
|
'can_create' => 1,
|
|
'can_read' => 1,
|
|
'can_update' => 1,
|
|
'can_delete' => 1,
|
|
'updated_at' => $now,
|
|
];
|
|
|
|
if ($this->db->fieldExists('can_manage', 'role_permissions')) {
|
|
$grant['can_manage'] = 1;
|
|
}
|
|
|
|
if ($existing === null) {
|
|
$grant['role_id'] = $roleId;
|
|
$grant['permission_id'] = $permissionId;
|
|
$grant['created_at'] = $now;
|
|
|
|
$this->db->table('role_permissions')->insert($grant);
|
|
continue;
|
|
}
|
|
|
|
$this->db->table('role_permissions')
|
|
->where('id', (int) $existing['id'])
|
|
->update($grant);
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
if (
|
|
! $this->db->tableExists('roles')
|
|
|| ! $this->db->tableExists('permissions')
|
|
|| ! $this->db->tableExists('role_permissions')
|
|
) {
|
|
return;
|
|
}
|
|
|
|
$permission = $this->db->table('permissions')
|
|
->select('id')
|
|
->where('name', $this->permissionName)
|
|
->get()
|
|
->getRowArray();
|
|
|
|
if ($permission === null) {
|
|
return;
|
|
}
|
|
|
|
$roles = $this->db->table('roles')
|
|
->select('id')
|
|
->whereIn('name', $this->roleNames)
|
|
->get()
|
|
->getResultArray();
|
|
$roleIds = array_values(array_filter(array_map(static fn (array $role): int => (int) ($role['id'] ?? 0), $roles)));
|
|
|
|
if ($roleIds === []) {
|
|
return;
|
|
}
|
|
|
|
$this->db->table('role_permissions')
|
|
->where('permission_id', (int) $permission['id'])
|
|
->whereIn('role_id', $roleIds)
|
|
->delete();
|
|
}
|
|
}
|