Repository navigation
Expand file tree
/
Copy pathprofile.php
More file actions
192 lines (168 loc) · 9.36 KB
/
Copy pathprofile.php
File metadata and controls
192 lines (168 loc) · 9.36 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
$page_title = "My Profile";
require_once __DIR__ . '/autoload.php';
use App\Core\Bootstrap;
use App\Helpers\AuditHelper;
use App\Helpers\Flash;
use App\Helpers\Html;
use App\Helpers\Layout;
use App\Helpers\SecurityHelper;
Bootstrap::init();
$user_id = (int) $_SESSION['user_id'];
const PROFILE_MAX_PW_FAILS = 5;
const PROFILE_LOCK_SECONDS = 900;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
SecurityHelper::verifyCsrfToken($_POST['csrf_token'] ?? '');
$action = $_POST['action'] ?? '';
if ($action === 'update_name') {
$name = trim((string) ($_POST['name'] ?? ''));
if ($name === '' || mb_strlen($name) > 100) {
Flash::redirect('profile.php', 'error', "Name must be between 1 and 100 characters.");
}
$pdo->prepare("UPDATE users SET name = ? WHERE id = ?")->execute([$name, $user_id]);
$_SESSION['user_name'] = $name;
AuditHelper::log($pdo, 'profile_update', "Changed display name");
Flash::redirect('profile.php', 'success', "Name updated.");
}
if ($action === 'update_notifications') {
$enabled = !empty($_POST['notifications_enabled']) ? 1 : 0;
$pdo->prepare("INSERT INTO user_preferences (user_id, notifications_enabled) VALUES (?, ?)
ON DUPLICATE KEY UPDATE notifications_enabled = VALUES(notifications_enabled)")
->execute([$user_id, $enabled]);
$_SESSION['preferences']['notifications_enabled'] = $enabled;
AuditHelper::log($pdo, 'profile_update', $enabled ? 'Enabled email notifications' : 'Disabled email notifications');
Flash::redirect('profile.php', 'success', $enabled ? 'Email notifications turned on.' : 'Email notifications turned off.');
}
if ($action === 'change_password') {
// Throttle guessing of the current password from a hijacked session.
$fails = $_SESSION['pw_change_fails'] ?? ['count' => 0, 'since' => time()];
if (time() - $fails['since'] > PROFILE_LOCK_SECONDS) {
$fails = ['count' => 0, 'since' => time()];
}
if ($fails['count'] >= PROFILE_MAX_PW_FAILS) {
Flash::redirect('profile.php', 'error', "Too many wrong attempts. Try again in 15 minutes.");
}
$current = (string) ($_POST['current_password'] ?? '');
$new = (string) ($_POST['new_password'] ?? '');
$confirm = (string) ($_POST['confirm_password'] ?? '');
$stmt = $pdo->prepare("SELECT password FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$hash = (string) $stmt->fetchColumn();
if (!password_verify($current, $hash)) {
$fails['count']++;
$_SESSION['pw_change_fails'] = $fails;
AuditHelper::log($pdo, 'password_change_failed', "Wrong current password");
Flash::redirect('profile.php', 'error', "Your current password is incorrect.");
}
$error = SecurityHelper::validatePassword($new);
if ($error === null && !hash_equals($new, $confirm)) {
$error = "The new passwords do not match.";
}
if ($error === null && password_verify($new, $hash)) {
$error = "The new password must be different from the current one.";
}
if ($error !== null) {
Flash::redirect('profile.php', 'error', $error);
}
$newHash = password_hash($new, PASSWORD_DEFAULT);
$pdo->prepare("UPDATE users SET password = ? WHERE id = ?")->execute([$newHash, $user_id]);
// Keep this session signed in; every other session for this account is signed out (see config.php).
$_SESSION['pw_fp'] = hash('sha256', $newHash);
// Outstanding reset links are no longer needed.
$pdo->prepare("UPDATE password_resets SET used_at = NOW() WHERE user_id = ? AND used_at IS NULL")
->execute([$user_id]);
unset($_SESSION['pw_change_fails']);
session_regenerate_id(true);
AuditHelper::log($pdo, 'password_changed', "Password changed from profile");
Flash::redirect('profile.php', 'success', "Password changed successfully.");
}
header("Location: profile.php");
exit();
}
$stmt = $pdo->prepare("SELECT u.name, u.email, u.role, u.permission, u.created_at, t.family_name,
COALESCE(p.notifications_enabled, 1) AS notifications_enabled
FROM users u
LEFT JOIN tenants t ON t.id = u.tenant_id
LEFT JOIN user_preferences p ON p.user_id = u.id
WHERE u.id = ?");
$stmt->execute([$user_id]);
$me = $stmt->fetch();
$role_labels = ['root_admin' => 'Root Admin', 'family_admin' => 'Family Admin', 'user' => 'Member'];
Layout::header();
Layout::sidebar();
?>
<div class="row mb-4">
<div class="col">
<h2 class="fw-bold mb-0"><i class="fa-solid fa-user-gear text-primary me-2"></i>My Profile</h2>
<p class="text-muted">Your account details and password</p>
</div>
</div>
<div class="row g-4">
<div class="col-lg-5">
<div class="glass-panel p-4 h-100">
<h5 class="fw-bold mb-3">Account</h5>
<dl class="row small mb-4">
<dt class="col-5 text-muted">Email</dt>
<dd class="col-7"><?php echo Html::e($me['email']); ?></dd>
<dt class="col-5 text-muted">Family</dt>
<dd class="col-7"><?php echo Html::e($me['family_name'] ?? '—'); ?></dd>
<dt class="col-5 text-muted">Role</dt>
<dd class="col-7"><?php echo Html::e($role_labels[$me['role']] ?? $me['role']); ?></dd>
<dt class="col-5 text-muted">Access</dt>
<dd class="col-7"><?php echo $me['permission'] === 'edit' ? 'Edit' : 'Read-only'; ?></dd>
<dt class="col-5 text-muted">Member since</dt>
<dd class="col-7"><?php echo Html::e(date('d M Y', strtotime($me['created_at']))); ?></dd>
</dl>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="update_name">
<label for="profileName" class="form-label fw-bold small text-muted">Display name</label>
<div class="input-group">
<input type="text" name="name" id="profileName" class="form-control" maxlength="100" required
value="<?php echo Html::e($me['name']); ?>">
<button type="submit" class="btn btn-outline-primary">Save</button>
</div>
<div class="form-text small">To change your email, ask your family admin.</div>
</form>
<form method="POST" class="mt-4">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="update_notifications">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="notificationsEnabled"
name="notifications_enabled" value="1" data-autosubmit
<?php echo $me['notifications_enabled'] ? 'checked' : ''; ?>>
<label class="form-check-label fw-bold small" for="notificationsEnabled">Email notifications</label>
</div>
<div class="form-text small">Budget alerts, card due dates, Zakath and reminder emails.</div>
<noscript><button type="submit" class="btn btn-sm btn-outline-primary mt-2">Save</button></noscript>
</form>
</div>
</div>
<div class="col-lg-7">
<div class="glass-panel p-4 h-100">
<h5 class="fw-bold mb-3">Change Password</h5>
<form method="POST" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?php echo SecurityHelper::generateCsrfToken(); ?>">
<input type="hidden" name="action" value="change_password">
<div class="mb-3">
<label for="currentPassword" class="form-label fw-bold small text-muted">Current password</label>
<input type="password" name="current_password" id="currentPassword" class="form-control" required autocomplete="current-password">
</div>
<div class="mb-3">
<label for="newPassword" class="form-label fw-bold small text-muted">New password</label>
<input type="password" name="new_password" id="newPassword" class="form-control" required
minlength="8" maxlength="72" autocomplete="new-password"
data-oninput="checkPasswordStrength" data-args="<?php echo Html::args('$value'); ?>">
<?php echo Layout::passwordRequirements(); ?>
</div>
<div class="mb-4">
<label for="confirmPassword" class="form-label fw-bold small text-muted">Confirm new password</label>
<input type="password" name="confirm_password" id="confirmPassword" class="form-control" required
minlength="8" maxlength="72" autocomplete="new-password">
</div>
<button type="submit" class="btn btn-primary px-4"><i class="fa-solid fa-key me-2"></i>Update password</button>
</form>
</div>
</div>
</div>
<?php Layout::footer(); ?>