Users: an API or service account keeps its role for API keys but is left out of agent lists, pickers, notifications and admin alerts, cannot sign in, and has its own API accounts tab on Admin Users; database 3.8.1 adds users.user_service_account; 26.11.9 changelog entries

This commit is contained in:
johnnyq
2026-11-17 19:24:04 -05:00
parent 46a2dfe96c
commit 1c4c8be8de
53 changed files with 201 additions and 49 deletions

View File

@@ -407,7 +407,7 @@ function calendarSetAttendees($event_id, $client_id, array $user_ids, array $con
$user_ids = array_values(array_unique(array_filter(array_map('intval', $user_ids))));
if ($user_ids) {
$valid = [];
$sql = mysqli_query($mysqli, "SELECT user_id FROM users WHERE user_id IN (" . implode(',', $user_ids) . ") AND user_type = 1 AND user_archived_at IS NULL");
$sql = mysqli_query($mysqli, "SELECT user_id FROM users WHERE user_id IN (" . implode(',', $user_ids) . ") AND user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL");
while ($row = mysqli_fetch_row($sql)) {
$valid[] = intval($row[0]);
}

View File

@@ -187,7 +187,7 @@ function cronHealthRecipients($to = null) {
}
$recipients = [];
$result = mysqli_query($mysqli, "SELECT user_name, user_email FROM users JOIN user_roles ON role_id = user_role_id
WHERE role_is_admin = 1 AND user_type = 1 AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name");
WHERE role_is_admin = 1 AND user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name");
while ($row = mysqli_fetch_assoc($result)) {
if (filter_var($row['user_email'], FILTER_VALIDATE_EMAIL)) {
$recipients[$row['user_email']] = $row['user_name'];

View File

@@ -1460,7 +1460,7 @@ function expensePayers($keep_id = 0) {
global $mysqli;
$keep_id = intval($keep_id);
$payers = [];
$sql = mysqli_query($mysqli, "SELECT user_id, user_name FROM users WHERE (user_type = 1 AND user_archived_at IS NULL) OR user_id = $keep_id ORDER BY user_name ASC");
$sql = mysqli_query($mysqli, "SELECT user_id, user_name FROM users WHERE (user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL) OR user_id = $keep_id ORDER BY user_name ASC");
while ($row = mysqli_fetch_row($sql)) {
$payers[intval($row[0])] = $row[1];
}

View File

@@ -160,7 +160,7 @@ function gettingStartedSteps() {
$status = $done ? 'Response times for ' . $slas . ' ' . ($slas === 1 ? 'priority' : 'priorities') : 'No response times yet';
break;
case 'team':
$agents = $count("SELECT COUNT(*) FROM users WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL");
$agents = $count("SELECT COUNT(*) FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL");
$invited = userInvitesHoldingSeats();
$done = $agents > 1 || $invited > 0;
$status = ($agents === 1 ? 'Just you' : "$agents people") . ($invited ? ", $invited invited" : '');

View File

@@ -70,7 +70,7 @@ function appNotify($type, $details, $action = null, $client_id = 0, $entity_id =
$action = logSqlText($action, 250);
$sql = mysqli_query($mysqli, "SELECT user_id FROM users
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL
");
while ($row = mysqli_fetch_assoc($sql)) {

View File

@@ -141,7 +141,7 @@ function quickTicketTimerScript($ticket_id) {
function quickTicketAssignChoices() {
global $mysqli;
$choices = ['me' => 'The agent making it', 'nobody' => 'Nobody'];
$sql = mysqli_query($mysqli, "SELECT user_id, user_name FROM users WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name ASC");
$sql = mysqli_query($mysqli, "SELECT user_id, user_name FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$choices[(string) intval($row['user_id'])] = (string) $row['user_name'];
}

View File

@@ -1211,7 +1211,7 @@ function reportAgentControl($user_id, $all_label = 'All agents') {
global $mysqli;
$user_id = intval($user_id);
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users
WHERE user_type = 1 AND ((user_archived_at IS NULL AND user_status = 1) OR user_id = $user_id)
WHERE user_type = 1" . serviceAccountsOutSql() . " AND ((user_archived_at IS NULL AND user_status = 1) OR user_id = $user_id)
ORDER BY user_name ASC");
$html = '<div class="col-sm-6 col-lg-2"><select class="form-select select2" name="agent" data-report-autosubmit aria-label="Agent">'
. '<option value="">- ' . escapeHtml($all_label) . ' -</option>';

View File

@@ -213,7 +213,7 @@ function roleAdminBlocker(array $row) {
return 'It is your own role - another admin can change it.';
}
$others = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM users JOIN user_roles ON role_id = user_role_id
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL AND role_is_admin = 1 AND role_id <> " . intval($row['role_id'])))[0]);
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL AND role_is_admin = 1 AND role_id <> " . intval($row['role_id'])))[0]);
if ($others === 0 && intval($row['agents'])) {
return 'Its agents are the only admins - nobody would be left to run Admin.';
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* ITFlow - API / service accounts
*
* An API key acts as an agent (api_keys.api_key_user_id - an active agent with a role decides
* what the key may do), so a key for an integration needs a login of its own - which then showed
* up in the calendar, every assign picker, the reports... An admin marks such a login an "API /
* service account" in Admin > Users (users.user_service_account, database 3.8.1). It stays an
* active agent with its role, so its keys keep working, but:
* - the agent lists and pickers leave it out - serviceAccountsOutSql() in their queries, and the
* notifications and alert emails that go to every agent or admin
* - it can't sign in to ITFlow in a browser (login.php; includes/load_user_session.php ends a
* session it already had)
* - Admin > Users lists it in a section of its own; Roles and API Keys show it as before
*/
/** Whether the 3.8.1 update has run. */
function serviceAccountsReady() {
global $mysqli;
static $ready = null;
if ($ready === null) {
if (defined('CURRENT_DATABASE_VERSION')) {
$version = CURRENT_DATABASE_VERSION;
} else {
$result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1");
$version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0';
}
$ready = version_compare($version, '3.8.1', '>=');
}
return $ready;
}
/*
* " AND <column> = 0" - leaves the API / service accounts out of an agent list. '' before the
* update. $column is the query's, with its table where it needs one ('u.user_service_account').
*/
function serviceAccountsOutSql($column = 'user_service_account') {
if (!serviceAccountsReady() || !preg_match('/^[A-Za-z_][A-Za-z0-9_.]*$/', $column)) {
return '';
}
return " AND $column = 0";
}
/** Is this login an API / service account? */
function serviceAccountIs($user_id) {
global $mysqli;
if (!serviceAccountsReady() || !intval($user_id)) {
return false;
}
$row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT user_service_account FROM users WHERE user_id = " . intval($user_id)));
return intval($row[0] ?? 0) === 1;
}
/*
* Set it from the New / Edit user window's switch - a no-op when the form didn't have it (before
* the update). Returns what changed, for the audit log, or ''.
*/
function serviceAccountSave($user_id, array $post) {
global $mysqli;
$user_id = intval($user_id);
if (!$user_id || !isset($post['service_account_present']) || !serviceAccountsReady()) {
return '';
}
$want = !empty($post['service_account']);
if ($want === serviceAccountIs($user_id)) {
return '';
}
mysqli_query($mysqli, "UPDATE users SET user_service_account = " . ($want ? 1 : 0) . " WHERE user_id = $user_id AND user_type = 1");
return $want ? 'now an API / service account' : 'no longer an API / service account';
}

View File

@@ -161,7 +161,7 @@ function signInAdmins() {
$admins = [];
$sql = mysqli_query($mysqli, "SELECT users.user_id, user_name, user_email FROM users
JOIN user_roles ON role_id = user_role_id AND role_is_admin = 1
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL");
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL");
while ($row = mysqli_fetch_assoc($sql)) {
$admins[intval($row['user_id'])] = ['name' => $row['user_name'], 'email' => $row['user_email']];
}

View File

@@ -282,7 +282,7 @@ function quickPickAgents($none_label = 'Unassigned') {
global $mysqli, $session_user_id;
$options = [['value' => '0', 'label' => $none_label, 'icon' => 'fas fa-user-slash', 'none' => true]];
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE user_type = 1 AND user_archived_at IS NULL ORDER BY user_name ASC");
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$user_id = intval($row['user_id']);
$option = ['value' => (string) $user_id, 'label' => $row['user_name'] . ($user_id == $session_user_id ? ' (me)' : '')];

View File

@@ -52,7 +52,7 @@ function ticketMentionAgents($client_id) {
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar, role_name, role_is_admin,
COALESCE((SELECT user_role_permission_level FROM user_role_permissions WHERE user_role_permissions.user_role_id = users.user_role_id AND module_id = $support_id LIMIT 1), 0) AS support_level
FROM users LEFT JOIN user_roles ON role_id = user_role_id
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL
ORDER BY user_name ASC");
$agents = [];
while ($row = mysqli_fetch_assoc($sql)) {

View File

@@ -239,7 +239,7 @@ function tripDrivers($keep_id = 0) {
global $mysqli;
$keep_id = intval($keep_id);
$drivers = [];
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE (user_type = 1 AND user_archived_at IS NULL) OR user_id = $keep_id ORDER BY user_name ASC");
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE (user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL) OR user_id = $keep_id ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$drivers[intval($row['user_id'])] = $row;
}

View File

@@ -141,7 +141,7 @@ function selectOptionRole($role) {
function userActiveAdminCount($except_user_id = 0) {
global $mysqli;
return intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM users JOIN user_roles ON role_id = user_role_id
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL AND role_is_admin = 1 AND user_id <> " . intval($except_user_id)))[0]);
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL AND role_is_admin = 1 AND user_id <> " . intval($except_user_id)))[0]);
}
/** Would taking this agent out of the admins (a new role, disabled, archived) leave nobody to run the place? */