Added authentication functionality and simple role based model
This commit is contained in:
@@ -1,18 +1,32 @@
|
||||
<?php
|
||||
require 'db.php';
|
||||
require 'auth.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
require_login();
|
||||
|
||||
$user_id = current_user_id();
|
||||
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
$name = trim($data['name'] ?? '');
|
||||
$task_id = intval($data['task_id'] ?? 0);
|
||||
|
||||
if ($name === '' || $task_id <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Invalid input']);
|
||||
exit;
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'Invalid input']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("INSERT INTO subtasks (task_id, name) VALUES (?, ?)");
|
||||
$stmt->execute([$task_id, $name]);
|
||||
// Ensure task belongs to this user
|
||||
$stmt = $pdo->prepare("SELECT id FROM tasks WHERE id = ? AND user_id = ? LIMIT 1");
|
||||
$stmt->execute([$task_id, $user_id]);
|
||||
if (!$stmt->fetchColumn()) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['success' => false, 'error' => 'Forbidden']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$stmt = $pdo->prepare("INSERT INTO subtasks (user_id, task_id, name) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$user_id, $task_id, $name]);
|
||||
|
||||
echo json_encode(['success' => true, 'id' => $pdo->lastInsertId()]);
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user