Add canonical user access profile table
Create user_access_profiles as a denormalized access index over the existing roles and user_roles tables. Store primary_category plus is_admin, is_teacher, and is_parent flags so permission checks can use a single canonical source for broad user groups. Classify teacher and teacher_assistant/TA roles as teacher access, parent as parent access, and every other active staff role as admin access. Backfill all existing users during migration and keep profiles synchronized when role assignments are inserted, updated, removed, or replaced. Create a default access profile whenever a user account is created, then refresh it when roles are assigned. Delete the access profile when a user is deleted so the table does not keep orphaned authorization rows. Expose the computed access profile in web sessions and auth API responses while preserving the existing detailed roles array and route-filter behavior. Add unit coverage for TA, staff-admin, and multi-role parent/teacher classification.
This commit is contained in:
@@ -39,6 +39,36 @@ class UserModel extends Model
|
||||
protected $useTimestamps = true; // Enable automatic timestamps
|
||||
protected $createdField = 'created_at'; // Define the field name for the created timestamp
|
||||
protected $updatedField = 'updated_at'; // Define the field name for the updated timestamp
|
||||
protected $afterInsert = ['syncAccessProfileAfterInsert'];
|
||||
protected $afterDelete = ['deleteAccessProfileAfterDelete'];
|
||||
|
||||
protected function syncAccessProfileAfterInsert(array $data): array
|
||||
{
|
||||
try {
|
||||
$userId = (int) ($data['id'] ?? 0);
|
||||
if ($userId > 0) {
|
||||
model(UserAccessProfileModel::class)->syncUser($userId);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'UserModel access profile sync failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function deleteAccessProfileAfterDelete(array $data): array
|
||||
{
|
||||
try {
|
||||
$ids = array_filter(array_map('intval', (array) ($data['id'] ?? [])));
|
||||
if ($ids !== [] && $this->db->tableExists('user_access_profiles')) {
|
||||
$this->db->table('user_access_profiles')->whereIn('user_id', $ids)->delete();
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'UserModel access profile cleanup failed: ' . $e->getMessage());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
// Existing methods remain unchanged
|
||||
|
||||
|
||||
Reference in New Issue
Block a user