added loging

This commit is contained in:
Jürg Hallenbarter
2026-04-29 16:59:52 +02:00
parent 6cbb6a2e3e
commit 092bb53324
9 changed files with 55 additions and 46 deletions

View File

@@ -21,7 +21,7 @@ class ActivityLogController extends BaseController
public function index() public function index()
{ {
$userId = $this->getUserId(); $userId = $this->getUserId();
$limit = $this->request->getVar('limit') ?? 50; $limit = (int)($this->request->getVar('limit') ?? 50);
$logs = $this->activityLogModel->getByUser($userId, $limit); $logs = $this->activityLogModel->getByUser($userId, $limit);
return $this->successResponse($logs, 'Activity logs retrieved successfully'); return $this->successResponse($logs, 'Activity logs retrieved successfully');

View File

@@ -62,6 +62,23 @@ class TodoController extends BaseController
]; ];
$this->todoModel->insert($data); $this->todoModel->insert($data);
// Manually log the activity
try {
$activityLogModel = new \App\Models\ActivityLogModel();
$activityLogModel->logActivity([
'user_id' => $userId,
'action' => 'todo_created',
'entity_type' => 'todo',
'entity_id' => $data['id'],
'details' => json_encode(['action' => 'created', 'title' => $data['title']]),
'ip_address' => $this->request->getIPAddress(),
'user_agent' => $this->request->getUserAgent()->getAgentString(),
]);
} catch (\Exception $e) {
log_message('error', 'Failed to log activity: ' . $e->getMessage());
}
$todo = $this->todoModel->getByUserWithCategories($userId, $data['id']); $todo = $this->todoModel->getByUserWithCategories($userId, $data['id']);
return $this->successResponse($todo, 'Todo created successfully', 201); return $this->successResponse($todo, 'Todo created successfully', 201);
@@ -105,6 +122,23 @@ class TodoController extends BaseController
} }
$this->todoModel->update($id, $updateData); $this->todoModel->update($id, $updateData);
// Manually log the activity
try {
$activityLogModel = new \App\Models\ActivityLogModel();
$activityLogModel->logActivity([
'user_id' => $userId,
'action' => 'todo_updated',
'entity_type' => 'todo',
'entity_id' => $id,
'details' => json_encode(['action' => 'updated', 'title' => $todo['title'] ?? 'Unknown']),
'ip_address' => $this->request->getIPAddress(),
'user_agent' => $this->request->getUserAgent()->getAgentString(),
]);
} catch (\Exception $e) {
log_message('error', 'Failed to log activity: ' . $e->getMessage());
}
$todo = $this->todoModel->getByUserWithCategories($userId, $id); $todo = $this->todoModel->getByUserWithCategories($userId, $id);
return $this->successResponse($todo, 'Todo updated successfully'); return $this->successResponse($todo, 'Todo updated successfully');
@@ -125,6 +159,22 @@ class TodoController extends BaseController
$this->todoModel->delete($id); $this->todoModel->delete($id);
// Manually log the activity
try {
$activityLogModel = new \App\Models\ActivityLogModel();
$activityLogModel->logActivity([
'user_id' => $userId,
'action' => 'todo_deleted',
'entity_type' => 'todo',
'entity_id' => $id,
'details' => json_encode(['action' => 'deleted', 'title' => $todo['title'] ?? 'Unknown']),
'ip_address' => $this->request->getIPAddress(),
'user_agent' => $this->request->getUserAgent()->getAgentString(),
]);
} catch (\Exception $e) {
log_message('error', 'Failed to log activity: ' . $e->getMessage());
}
return $this->successResponse(null, 'Todo deleted successfully'); return $this->successResponse(null, 'Todo deleted successfully');
} }

View File

@@ -34,9 +34,6 @@ class ActivityLogModel extends Model
// Log an activity // Log an activity
public function logActivity($data) public function logActivity($data)
{ {
// Disable events to prevent any recursive logging
$this->skipEvents();
if (!isset($data['id'])) { if (!isset($data['id'])) {
$data['id'] = $this->generateUuid(); $data['id'] = $this->generateUuid();
} }
@@ -44,12 +41,9 @@ class ActivityLogModel extends Model
$data['created_at'] = date('Y-m-d H:i:s'); $data['created_at'] = date('Y-m-d H:i:s');
} }
$result = $this->insert($data); // Use builder directly to avoid triggering events
$builder = $this->db->table($this->table);
// Re-enable events return $builder->insert($data);
$this->skipEvents(false);
return $result;
} }
// Get logs by user // Get logs by user

View File

@@ -6,8 +6,6 @@ use CodeIgniter\Model;
class CategoryModel extends Model class CategoryModel extends Model
{ {
use LoggableTrait;
protected $table = 'categories'; protected $table = 'categories';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
@@ -30,9 +28,4 @@ class CategoryModel extends Model
'user_id' => 'required', 'user_id' => 'required',
'name' => 'required|max_length[255]', 'name' => 'required|max_length[255]',
]; ];
protected function getEntityType(): string
{
return 'category';
}
} }

View File

@@ -134,7 +134,7 @@ trait LoggableTrait
{ {
try { try {
$request = \Config\Services::request(); $request = \Config\Services::request();
return $request->getUserAgent()->toString(); return $request->getUserAgent()->getAgentString();
} catch (\Exception $e) { } catch (\Exception $e) {
return 'CLI/Script'; return 'CLI/Script';
} }

View File

@@ -6,8 +6,6 @@ use CodeIgniter\Model;
class ProjectModel extends Model class ProjectModel extends Model
{ {
use LoggableTrait;
protected $table = 'projects'; protected $table = 'projects';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
@@ -30,9 +28,4 @@ class ProjectModel extends Model
'user_id' => 'required', 'user_id' => 'required',
'name' => 'required|max_length[255]', 'name' => 'required|max_length[255]',
]; ];
protected function getEntityType(): string
{
return 'project';
}
} }

View File

@@ -6,8 +6,6 @@ use CodeIgniter\Model;
class RecurringTaskModel extends Model class RecurringTaskModel extends Model
{ {
use LoggableTrait;
protected $table = 'recurring_tasks'; protected $table = 'recurring_tasks';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
@@ -35,11 +33,6 @@ class RecurringTaskModel extends Model
'schedule' => 'required|in_list[daily,weekly,monthly,custom]', 'schedule' => 'required|in_list[daily,weekly,monthly,custom]',
]; ];
protected function getEntityType(): string
{
return 'recurring_task';
}
// Get recurring tasks with categories // Get recurring tasks with categories
public function getWithCategories($taskId = null) public function getWithCategories($taskId = null)
{ {

View File

@@ -6,8 +6,6 @@ use CodeIgniter\Model;
class TodoModel extends Model class TodoModel extends Model
{ {
use LoggableTrait;
protected $table = 'todos'; protected $table = 'todos';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
@@ -39,11 +37,6 @@ class TodoModel extends Model
'status' => 'permit_empty|in_list[open,in_progress,completed,archived]', 'status' => 'permit_empty|in_list[open,in_progress,completed,archived]',
]; ];
protected function getEntityType(): string
{
return 'todo';
}
// Get todos with categories // Get todos with categories
public function getWithCategories($todoId = null) public function getWithCategories($todoId = null)
{ {

View File

@@ -6,8 +6,6 @@ use CodeIgniter\Model;
class UserModel extends Model class UserModel extends Model
{ {
use LoggableTrait;
protected $table = 'users'; protected $table = 'users';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $useAutoIncrement = false; protected $useAutoIncrement = false;
@@ -40,9 +38,4 @@ class UserModel extends Model
'is_unique' => 'This email is already registered', 'is_unique' => 'This email is already registered',
], ],
]; ];
protected function getEntityType(): string
{
return 'user';
}
} }