diff --git a/app/Models/UserModel.php b/app/Models/UserModel.php index 235c47a..0b056ad 100644 --- a/app/Models/UserModel.php +++ b/app/Models/UserModel.php @@ -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