Skip to content

Commit a3b5b2f

Browse files
committed
Prevent HoO user from downgrading their usergroup if they're the only HoO
1 parent ea1fd7a commit a3b5b2f

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

include/cms_users_edit.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,34 @@
7474
WHERE ug.id = :id AND (NOT ug.deleted OR ug.deleted IS NULL)', ['id' => $_POST['cms_usergroups_id'][0]]);
7575
$is_admin = $_SESSION['user']['is_admin'];
7676
$organisation_allowed = ($_SESSION['organisation']['id'] == $posteduser['organisation_id']);
77-
// allow admins to create another admin account
77+
// allow HoO to create another HoO account
7878
// related to this trello card https://trello.com/c/YAF3Az4P
7979
$userlevel_allowed = ($_SESSION['usergroup']['userlevel'] > $posteduser['userlevel']) || ($_SESSION['usergroup']['userlevel'] == $posteduser['userlevel'] && '100' == $_SESSION['usergroup']['userlevel']);
8080

81+
// Prevent HoO user from downgrading their usergroup if they're the only HoO
82+
if (!$is_admin && $_POST['id'] == $_SESSION['user']['id'] && 100 == $_SESSION['usergroup']['userlevel'] && $posteduser['userlevel'] < $_SESSION['usergroup']['userlevel']) {
83+
// Count how many HoO users exist in this organization
84+
$hoo_count = db_value(
85+
'
86+
SELECT COUNT(DISTINCT u.id)
87+
FROM cms_users AS u
88+
LEFT JOIN cms_usergroups AS ug ON ug.id = u.cms_usergroups_id
89+
LEFT JOIN cms_usergroups_levels AS ugl ON ugl.id = ug.userlevel
90+
WHERE ug.organisation_id = :org_id
91+
AND ugl.level = :highest_level
92+
AND (NOT u.deleted OR u.deleted IS NULL)
93+
AND (NOT ug.deleted OR ug.deleted IS NULL)
94+
AND (NOT u.valid_lastday < CURDATE() OR UNIX_TIMESTAMP(u.valid_lastday) = 0)',
95+
['org_id' => $_SESSION['organisation']['id'], 'highest_level' => $highest_level]
96+
);
97+
98+
// If this is the last HoO, prevent the change
99+
if ($hoo_count <= 1) {
100+
redirect('?action=cms_users_edit&id='.$_POST['id'].'&origin='.$_POST['_origin'].'&warning=1&message=You cannot downgrade yourself. Your organisation must have at least one Head of Operations user.');
101+
trigger_error('You cannot downgrade yourself. Your organisation must have at least one Head of Operations user.', E_USER_NOTICE);
102+
}
103+
}
104+
81105
if ($is_admin || ($organisation_allowed && $userlevel_allowed)) {
82106
$keys = ['naam', 'email', 'cms_usergroups_id', 'valid_firstday', 'valid_lastday'];
83107
$userId = db_transaction(function () use ($table, $keys, $userId) {

0 commit comments

Comments
 (0)