Skip to content

Commit dcf960c

Browse files
committed
Prevent HoO user from downgrading their usergroup if they're the only HoO
1 parent 99bb086 commit dcf960c

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

include/cms_users_edit.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,37 @@
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
83+
&& $_POST['id'] == $_SESSION['user']['id']
84+
&& 100 == $_SESSION['usergroup']['userlevel']
85+
&& $posteduser['userlevel'] < $_SESSION['usergroup']['userlevel']) {
86+
// Count how many HoO users exist in this organization
87+
$hoo_count = db_value(
88+
'
89+
SELECT COUNT(DISTINCT u.id)
90+
FROM cms_users AS u
91+
LEFT JOIN cms_usergroups AS ug ON ug.id = u.cms_usergroups_id
92+
LEFT JOIN cms_usergroups_levels AS ugl ON ugl.id = ug.userlevel
93+
WHERE ug.organisation_id = :org_id
94+
AND ugl.level = 100
95+
AND (NOT u.deleted OR u.deleted IS NULL)
96+
AND (NOT ug.deleted OR ug.deleted IS NULL)
97+
AND NOT (u.valid_lastday < CURDATE() AND UNIX_TIMESTAMP(u.valid_lastday) != 0)',
98+
['org_id' => $_SESSION['organisation']['id']]
99+
);
100+
101+
// If this is the last HoO, prevent the change
102+
if ($hoo_count <= 1) {
103+
trigger_error('You cannot downgrade yourself. Your organisation must have at least one Head of Operations user.', E_USER_NOTICE);
104+
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.');
105+
}
106+
}
107+
81108
if ($is_admin || ($organisation_allowed && $userlevel_allowed)) {
82109
$keys = ['naam', 'email', 'cms_usergroups_id', 'valid_firstday', 'valid_lastday'];
83110
$userId = db_transaction(function () use ($table, $keys, $userId) {

0 commit comments

Comments
 (0)