-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_sacrifice.php
More file actions
77 lines (65 loc) · 3.12 KB
/
Copy pathmouse_sacrifice.php
File metadata and controls
77 lines (65 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Sacrifice Mouse
*
* Marks a mouse as sacrificed. Closes the open cage-history interval and
* clears current_cage_id (a sacrificed mouse no longer occupies a cage).
* The record itself is preserved for archival/lineage.
*/
require 'session_config.php';
require 'dbcon.php';
require_once 'log_activity.php';
require_once 'services/roles.php';
require_once 'includes/cage_access.php';
if (!isset($_SESSION['username'])) { header('Location: index.php'); exit; }
if (!role_can_write($_SESSION['role'] ?? null)) { $_SESSION['message'] = 'Your role has view-only access and cannot sacrifice mice.'; header('Location: mouse_dash.php'); exit; }
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: mouse_dash.php'); exit; }
if (!isset($_POST['csrf_token']) || $_POST['csrf_token'] !== $_SESSION['csrf_token']) { die('CSRF token validation failed'); }
$mouse_id = trim($_POST['mouse_id'] ?? '');
$sacrificed_at = trim($_POST['sacrificed_at'] ?? '');
$reason = trim($_POST['reason'] ?? '') ?: null;
$user_id = $_SESSION['user_id'] ?? null;
if ($mouse_id === '' || $sacrificed_at === '') {
$_SESSION['message'] = 'Mouse ID and date are required.';
header('Location: mouse_dash.php'); exit;
}
$stmt = $con->prepare("SELECT status, current_cage_id FROM mice WHERE mouse_id = ?");
$stmt->bind_param("s", $mouse_id);
$stmt->execute();
$res = $stmt->get_result();
if ($res->num_rows !== 1) {
$_SESSION['message'] = 'Mouse not found.';
header('Location: mouse_dash.php'); exit;
}
$row = $res->fetch_assoc();
$stmt->close();
// Per-cage authorization: a non-admin may only act on a mouse in a cage they
// are assigned to (matches the cage pages and the API).
if (!cage_user_can_write_mouse($con, $user_id, $_SESSION['role'] ?? null, $row['current_cage_id'])) {
$_SESSION['message'] = 'Access denied. You can only modify mice in cages you are assigned to.';
header("Location: mouse_view.php?id=" . urlencode($mouse_id)); exit;
}
if ($row['status'] === 'sacrificed') {
$_SESSION['message'] = 'Mouse is already marked sacrificed.';
header("Location: mouse_view.php?id=" . urlencode($mouse_id)); exit;
}
mysqli_begin_transaction($con);
try {
// Close current cage-history interval if open
$close = $con->prepare("UPDATE mouse_cage_history SET moved_out_at = CURRENT_TIMESTAMP, reason = COALESCE(reason, 'sacrifice') WHERE mouse_id = ? AND moved_out_at IS NULL");
$close->bind_param("s", $mouse_id);
$close->execute();
$close->close();
$upd = $con->prepare("UPDATE mice SET status = 'sacrificed', sacrificed_at = ?, sacrifice_reason = ?, current_cage_id = NULL WHERE mouse_id = ?");
$upd->bind_param("sss", $sacrificed_at, $reason, $mouse_id);
$upd->execute();
$upd->close();
mysqli_commit($con);
log_activity($con, 'sacrifice', 'mouse', $mouse_id, "Sacrificed on $sacrificed_at" . ($reason ? ": $reason" : ''));
$_SESSION['message'] = "Mouse marked sacrificed on $sacrificed_at.";
} catch (Exception $e) {
mysqli_rollback($con);
$_SESSION['message'] = 'Failed to mark sacrificed: ' . $e->getMessage();
}
header("Location: mouse_view.php?id=" . urlencode($mouse_id));
exit;