diff --git a/api.php b/api.php index e6b7bd1..c7942b7 100644 --- a/api.php +++ b/api.php @@ -2290,35 +2290,34 @@ switch ($action) { requireAuth(); $companyId = requireCompany(); if ($method !== 'POST') { echo json_encode(['error' => 'POST required']); break; } - $rawInput = file_get_contents('php://input'); - $input = json_decode($rawInput, true); - if (!$input) { echo json_encode(['error' => 'Invalid JSON input', 'raw' => substr($rawInput, 0, 200)]); break; } - $id = $input['id'] ?? ''; - $status = $input['status'] ?? ''; - $todo = dbLoadTodo($id); - if (!$todo || $todo['company_id'] !== $companyId) { - http_response_code(404); - echo json_encode(['error' => 'Tehtävää ei löytynyt']); - break; - } - // Feature request status: vain admin - if ($todo['type'] === 'feature_request' && !isAdmin()) { - http_response_code(403); - echo json_encode(['error' => 'Vain admin voi muuttaa ehdotuksen statusta']); - break; - } - // Task status: vain admin - if ($todo['type'] === 'task' && !isAdmin()) { - http_response_code(403); - echo json_encode(['error' => 'Vain admin voi muuttaa tehtävän statusta']); - break; - } try { + $input = json_decode(file_get_contents('php://input'), true); + $id = $input['id'] ?? ''; + $status = $input['status'] ?? ''; + if (!$id || !$status) { echo json_encode(['error' => 'id ja status vaaditaan']); break; } + // Tarkista oikeudet kevyellä querylla (ei dbLoadTodo joka voi kaatua) + $rows = _dbFetchAll("SELECT type, company_id FROM todos WHERE id = ?", [$id]); + if (empty($rows) || $rows[0]['company_id'] !== $companyId) { + http_response_code(404); + echo json_encode(['error' => 'Tehtävää ei löytynyt']); + break; + } + $type = $rows[0]['type']; + if ($type === 'feature_request' && !isAdmin()) { + http_response_code(403); + echo json_encode(['error' => 'Vain admin voi muuttaa ehdotuksen statusta']); + break; + } + if ($type === 'task' && !isAdmin()) { + http_response_code(403); + echo json_encode(['error' => 'Vain admin voi muuttaa tehtävän statusta']); + break; + } _dbExecute("UPDATE todos SET status = ?, muokattu = NOW(), muokkaaja = ? WHERE id = ?", [$status, currentUser(), $id]); echo json_encode(['success' => true]); } catch (\Throwable $e) { http_response_code(500); - echo json_encode(['error' => 'Tallennus epäonnistui: ' . $e->getMessage()]); + echo json_encode(['error' => 'Virhe: ' . $e->getMessage()]); } break; @@ -2326,22 +2325,22 @@ switch ($action) { requireAuth(); requireAdmin(); $companyId = requireCompany(); - if ($method !== 'POST') break; - $input = json_decode(file_get_contents('php://input'), true); - $id = $input['id'] ?? ''; - $todo = dbLoadTodo($id); - if (!$todo || $todo['company_id'] !== $companyId) { - http_response_code(404); - echo json_encode(['error' => 'Tehtävää ei löytynyt']); - break; - } + if ($method !== 'POST') { echo json_encode(['error' => 'POST required']); break; } try { + $input = json_decode(file_get_contents('php://input'), true); + $id = $input['id'] ?? ''; $assignedTo = $input['assigned_to'] ?? ''; + $rows = _dbFetchAll("SELECT company_id FROM todos WHERE id = ?", [$id]); + if (empty($rows) || $rows[0]['company_id'] !== $companyId) { + http_response_code(404); + echo json_encode(['error' => 'Tehtävää ei löytynyt']); + break; + } _dbExecute("UPDATE todos SET assigned_to = ?, muokattu = NOW(), muokkaaja = ? WHERE id = ?", [$assignedTo, currentUser(), $id]); echo json_encode(['success' => true]); } catch (\Throwable $e) { http_response_code(500); - echo json_encode(['error' => 'Tallennus epäonnistui: ' . $e->getMessage()]); + echo json_encode(['error' => 'Virhe: ' . $e->getMessage()]); } break;