-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminlib_accounts.php
More file actions
353 lines (322 loc) · 15 KB
/
adminlib_accounts.php
File metadata and controls
353 lines (322 loc) · 15 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
// adminlib_accounts.php — Manage SEAL WordPress user accounts by Home System
// ------------------
// Security + Session
// ------------------
if (session_status() === PHP_SESSION_NONE) {
session_name('seal_admin_session');
session_start();
}
if (empty($_SESSION['csrf'])) {
$_SESSION['csrf'] = bin2hex(random_bytes(16));
}
$csrf_token = $_SESSION['csrf'];
// ------------------
// Includes
// ------------------
require '/var/www/seal_wp_script/seal_function.php';
require '/var/www/seal_wp_script/seal_db.inc';
// ------------------
// Helpers
// ------------------
function h($s)
{
return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8');
}
function post($k, $d = '')
{
return isset($_POST[$k]) ? $_POST[$k] : $d;
}
function req($k, $d = '')
{
return isset($_REQUEST[$k]) ? $_REQUEST[$k] : $d;
}
if (!function_exists('self_url')) {
function self_url()
{
if (function_exists('get_permalink')) {
return get_permalink();
}
return $_SERVER['REQUEST_URI'];
}
}
// ------------------
// Access Control
// ------------------
$current_user = wp_get_current_user();
$allowed_roles = ['administrator', 'libsys'];
if (! array_intersect($allowed_roles, (array) $current_user->roles)) {
echo '<div style="padding:30px;text-align:center;color:#a00;font-family:sans-serif;">
<h2>🚫 Access Denied</h2>
<p>You must have the <strong>Lib Systems Staff</strong> or <strong>Administrator</strong> role to access this page.</p>
</div>';
exit;
}
$SYSTEM = $SYSTEM ?? '';
$action = req('action', 'list');
$recnum = (int)req('recnum', 0);
?>
<style>
.seal-shell {font-family:system-ui,Segoe UI,Roboto,Ubuntu,Arial,sans-serif;color:#111;}
.seal-wrap {max-width:1100px;margin:0 auto;padding:18px;}
.seal-card {background:#fff;border:1px solid #e5e7eb;border-radius:14px;box-shadow:0 1px 2px rgba(0,0,0,.04);padding:18px;margin-bottom:16px;}
.seal-title {font-size:22px;font-weight:700;margin:0 0 8px;}
.seal-sub {font-size:14px;color:#555;margin-bottom:12px;}
.seal-row {display:flex;gap:12px;flex-wrap:wrap;align-items:flex-end;}
.seal-row .input {padding:8px 10px;border:1px solid #d1d5db;border-radius:8px;min-width:220px;}
.seal-btn {display:inline-block;padding:9px 14px;border-radius:10px;border:1px solid #0f766e;background:#0d9488;color:#fff;text-decoration:none;font-weight:600;cursor:pointer;}
.seal-btn.secondary {background:#fff;color:#0f766e;}
.seal-btn.danger {background:#b91c1c;border-color:#991b1b;}
.seal-table {width:100%;border-collapse:collapse;}
.seal-table th,.seal-table td {padding:10px 8px;border-bottom:1px solid #eef2f7;text-align:left;}
.seal-table th {font-size:12px;text-transform:uppercase;letter-spacing:.04em;color:#6b7280;background:#fafafa;}
@media (max-width:720px){.seal-row .input{min-width:100%;}}
</style>
<div class="seal-shell"><div class="seal-wrap">
<?php
// --------------------------------------------------
// DELETE
// --------------------------------------------------
if ($action === 'delete' && $recnum) {
if (!isset($_GET['csrf']) || $_GET['csrf'] !== $_SESSION['csrf']) {
echo '<div class="seal-card"><div class="seal-title">Security Error</div><div class="seal-sub">Invalid token.</div></div>';
} else {
wp_delete_user($recnum);
echo '<div class="seal-card"><div class="seal-title">✅ Account Deleted</div>
<a class="seal-btn" href="'.h(self_url()).'">Return to List</a></div>';
}
}
// --------------------------------------------------
// ADD NEW ACCOUNT (POST)
// --------------------------------------------------
if ($action === 'add' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST['csrf']) || $_POST['csrf'] !== $_SESSION['csrf']) {
echo '<div class="seal-card"><div class="seal-title">Security Error</div><div class="seal-sub">Invalid token.</div></div>';
} else {
$username = sanitize_user(post('user_login', ''));
$email = sanitize_email(post('user_email', ''));
$first_name = sanitize_text_field(post('first_name', ''));
$last_name = sanitize_text_field(post('last_name', ''));
$phone = sanitize_text_field(post('phone', ''));
$password = (string)post('user_pass', '');
$institution = sanitize_text_field(post('institution', ''));
$address_loc_code = sanitize_text_field(post('address_loc_code', ''));
if (empty($username) || empty($email) || empty($password) || empty($institution) || empty($first_name) || empty($last_name)) {
echo '<div class="seal-card"><div class="seal-title">❌ Missing Required Fields</div><div class="seal-sub">Please fill out all required fields.</div></div>';
} elseif (username_exists($username) || email_exists($email)) {
echo '<div class="seal-card"><div class="seal-title">⚠️ Account Exists</div><div class="seal-sub">A user with that username or email already exists.</div></div>';
} else {
$uid = wp_create_user($username, $password, $email);
if (is_wp_error($uid)) {
echo '<div class="seal-card"><div class="seal-title">❌ Error Creating Account</div><div class="seal-sub">'.h($uid->get_error_message()).'</div></div>';
} else {
$user = new WP_User($uid);
$user->set_role('libstaff');
// New User Approve: mark as approved (skip pending)
update_user_meta($uid, 'pw_user_status', 'approved');
wp_update_user([
'ID' => $uid,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => trim($first_name . ' ' . $last_name),
]);
update_user_meta($uid, 'phone', $phone);
update_user_meta($uid, 'institution', $institution);
update_user_meta($uid, 'home_system', $SYSTEM);
update_user_meta($uid, 'address_loc_code', $address_loc_code);
echo '<div class="seal-card"><div class="seal-title">✅ Account Created</div>
<div class="seal-sub">User <strong>'.h($username).'</strong> added successfully as Library Staff.</div>
<a class="seal-btn" href="'.h(self_url()).'">Back to List</a></div>';
}
}
}
}
// --------------------------------------------------
// SHOW ADD FORM
// --------------------------------------------------
if ($action === 'new') {
?>
<div class="seal-card">
<div class="seal-title">Add New Library Staff Account</div>
<form method="post" action="<?php echo h(self_url()); ?>?action=add">
<input type="hidden" name="csrf" value="<?php echo h($csrf_token); ?>">
<div class="seal-row">
<label><b>Username*</b><br><input class="input" type="text" name="user_login" required></label>
<label><b>Email*</b><br><input class="input" type="email" name="user_email" required></label>
</div>
<div class="seal-row">
<label><b>First Name*</b><br><input class="input" type="text" name="first_name" required></label>
<label><b>Last Name*</b><br><input class="input" type="text" name="last_name" required></label>
</div>
<div class="seal-row">
<label><b>Phone</b><br><input class="input" type="text" name="phone"></label>
</div>
<div class="seal-row">
<label><b>Password*</b><br><input class="input" type="password" name="user_pass" required></label>
<label><b>Institution*</b><br><input class="input" type="text" name="institution" required></label>
</div>
<div class="seal-row">
<label><b>LOC Code</b><br><input class="input" type="text" name="address_loc_code"></label>
</div>
<div style="margin-top:12px;">
<button class="seal-btn" type="submit">Create Account</button>
<a class="seal-btn secondary" href="<?php echo h(self_url()); ?>">Cancel</a>
</div>
</form>
</div>
<?php
}
// --------------------------------------------------
// SAVE (EDIT)
// --------------------------------------------------
if ($action === 'save' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!isset($_POST['csrf']) || $_POST['csrf'] !== $_SESSION['csrf']) {
echo '<div class="seal-card"><div class="seal-title">Security Error</div><div class="seal-sub">Invalid token.</div></div>';
} else {
$uid = (int)post('recnum', 0);
$email = sanitize_email(post('user_email', ''));
$password = (string)post('user_pass', '');
$fields = [
'institution','phone','alt_email','address_loc_code','oclc_symbol'
];
$meta = [];
foreach ($fields as $f) {
$meta[$f] = sanitize_text_field(post($f, ''));
}
$first_name = sanitize_text_field(post('first_name', ''));
$last_name = sanitize_text_field(post('last_name', ''));
$userdata = [
'ID' => $uid,
'user_email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'display_name' => trim($first_name . ' ' . $last_name),
];
if (!empty($password)) {
$userdata['user_pass'] = $password;
}
wp_update_user($userdata);
foreach ($meta as $k => $v) {
update_user_meta($uid, $k, $v);
}
echo '<div class="seal-card"><div class="seal-title">✅ Account Updated</div>
<a class="seal-btn" href="'.h(self_url()).'">Back to List</a></div>';
}
}
// --------------------------------------------------
// EDIT
// --------------------------------------------------
if ($action === 'edit' && $recnum) {
$user = get_userdata($recnum);
if (!$user) {
echo '<div class="seal-card"><div class="seal-title">❌ User not found</div></div>';
} else {
$fields = [
'institution','home_system','phone','alt_email','address_loc_code','oclc_symbol'
];
$meta = [];
foreach ($fields as $key) {
$meta[$key] = get_user_meta($recnum, $key, true) ?: '';
}
$display_name = $user->display_name ?: $user->user_login;
$first_name = $user->first_name ?? '';
$last_name = $user->last_name ?? '';
?>
<div class="seal-card">
<div class="seal-title">Edit Account: <?php echo h($display_name); ?> (<?php echo h($user->user_login); ?>)</div>
<div class="seal-sub">Home System: <b><?php echo h($meta['home_system']); ?></b> • User ID: <?php echo (int)$recnum; ?></div>
<form method="post" action="<?php echo h(self_url()); ?>?action=save">
<input type="hidden" name="csrf" value="<?php echo h($csrf_token); ?>">
<input type="hidden" name="recnum" value="<?php echo (int)$recnum; ?>">
<div class="seal-row">
<label><b>Email</b><br><input class="input" type="email" name="user_email" value="<?php echo h($user->user_email); ?>"></label>
<label><b>New Password</b><br><input class="input" type="password" name="user_pass" placeholder="Leave blank to keep existing"></label>
</div>
<div class="seal-row">
<label><b>First Name</b><br>
<input class="input" type="text" name="first_name" value="<?php echo h($first_name); ?>">
</label>
<label><b>Last Name</b><br>
<input class="input" type="text" name="last_name" value="<?php echo h($last_name); ?>">
</label>
</div>
<div class="seal-row">
<label><b>Phone</b><br>
<input class="input" type="text" name="phone" value="<?php echo h($meta['phone']); ?>">
<label><b>Institution</b><br><input class="input" type="text" name="institution" value="<?php echo h($meta['institution']); ?>"></label>
</label>
</div>
<div class="seal-row">
<label><b>Alt Email</b><br>
<input class="input" type="email" name="alt_email" value="<?php echo h($meta['alt_email']); ?>">
</label>
</div>
<div class="seal-row">
<label><b>LOC Code</b><br><input class="input" type="text" name="address_loc_code" value="<?php echo h($meta['address_loc_code']); ?>"></label>
<label><b>OCLC Symbol</b><br><input class="input" type="text" name="oclc_symbol" value="<?php echo h($meta['oclc_symbol']); ?>"></label>
</div>
<div style="margin-top:12px;">
<button class="seal-btn" type="submit">Save Changes</button>
<a class="seal-btn secondary" href="<?php echo h(self_url()); ?>">Cancel</a>
<a class="seal-btn danger"
href="<?php echo h(self_url()); ?>?action=delete&recnum=<?php echo (int)$recnum; ?>&csrf=<?php echo h($csrf_token); ?>"
onclick="return confirm('Are you sure you want to permanently delete this account?');">Delete</a>
</div>
</form>
</div>
<?php
}
}
// --------------------------------------------------
// LIST + SEARCH
// --------------------------------------------------
if ($action === 'list') {
global $wpdb;
$search = trim(req('search', ''));
$where = "WHERE 1=1";
if (!empty($search)) {
$like = '%' . $wpdb->esc_like($search) . '%';
$where .= $wpdb->prepare(" AND (
u.user_email LIKE %s OR
u.display_name LIKE %s OR
m1.meta_value LIKE %s OR
m2.meta_value LIKE %s
)", $like, $like, $like, $like);
}
$sql = "
SELECT DISTINCT u.ID, u.display_name, u.user_login, u.user_email,
m1.meta_value AS institution,
m2.meta_value AS address_loc_code,
m3.meta_value AS home_system
FROM {$wpdb->users} u
LEFT JOIN {$wpdb->usermeta} m1 ON (u.ID=m1.user_id AND m1.meta_key='institution')
LEFT JOIN {$wpdb->usermeta} m2 ON (u.ID=m2.user_id AND m2.meta_key='address_loc_code')
LEFT JOIN {$wpdb->usermeta} m3 ON (u.ID=m3.user_id AND m3.meta_key='home_system')
$where
HAVING m3.meta_value=%s
ORDER BY u.display_name ASC
";
$rows = $wpdb->get_results($wpdb->prepare($sql, $SYSTEM));
echo '<div class="seal-card"><div class="seal-title">Accounts in '.h($SYSTEM).'</div>';
echo '<form method="get" action="'.h(self_url()).'" class="seal-row" style="margin-bottom:15px;">';
echo '<input type="hidden" name="action" value="list">';
echo '<input class="input" type="text" name="search" placeholder="Search by name, email, LOC, or institution" value="'.h($search).'">';
echo '<button class="seal-btn" type="submit">Search</button>';
echo '<a class="seal-btn secondary" href="'.h(self_url()).'">Reset</a>';
echo '<a class="seal-btn" style="margin-left:auto;" href="'.h(self_url()).'?action=new">+ Add Account</a>';
echo '</form>';
echo '<table class="seal-table">';
echo '<tr><th>Name</th><th>Email</th><th>Institution</th><th>LOC Code</th><th>Action</th></tr>';
foreach ($rows as $row) {
echo '<tr>';
echo '<td>'.h($row->display_name).'</td>';
echo '<td>'.h($row->user_email).'</td>';
echo '<td>'.h($row->institution).'</td>';
echo '<td>'.h($row->address_loc_code).'</td>';
echo '<td><a class="seal-btn secondary" href="'.h(self_url()).'?action=edit&recnum='.(int)$row->ID.'">Edit</a></td>';
echo '</tr>';
}
echo '</table></div>';
}
?>
</div></div>