Initial commit - CMS Gov Bapenda Garut dengan EditorJS
This commit is contained in:
45
app/Models/AuditLogModel.php
Normal file
45
app/Models/AuditLogModel.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class AuditLogModel extends Model
|
||||
{
|
||||
protected $table = 'audit_logs';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = ['user_id', 'action', 'ip_address', 'user_agent', 'created_at'];
|
||||
|
||||
protected bool $allowEmptyInserts = false;
|
||||
|
||||
protected $useTimestamps = false;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = null;
|
||||
protected $deletedField = null;
|
||||
|
||||
public function logAction(string $action, ?int $userId = null): bool
|
||||
{
|
||||
$request = service('request');
|
||||
|
||||
$data = [
|
||||
'user_id' => $userId,
|
||||
'action' => $action,
|
||||
'ip_address' => $request->getIPAddress(),
|
||||
'user_agent' => $request->getUserAgent()->getAgentString(),
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
try {
|
||||
return $this->insert($data);
|
||||
} catch (\Exception $e) {
|
||||
log_message('error', 'Audit log insert failed: ' . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user