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

@@ -0,0 +1,25 @@
<?php
/*
* ITFlow - Database update to version 3.8.1 (from 3.8.0)
* Included by admin/database_updates.php - do not access directly
*
* API / service accounts (functions/service_account.php): an agent login that
* exists only for API keys to act as. It keeps its role, so its keys work, but
* it's left out of every agent list and can't sign in to ITFlow.
* users.user_service_account 1 = an API / service account
*
* Re-runnable: the column is added only where it isn't there yet.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
$itflow_column = function ($table, $column) use ($mysqli) {
$result = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$table' AND COLUMN_NAME = '$column' LIMIT 1");
return $result && mysqli_num_rows($result) > 0;
};
if (!$itflow_column('users', 'user_service_account')) {
mysqli_query($mysqli, "ALTER TABLE `users` ADD `user_service_account` tinyint(1) NOT NULL DEFAULT 0 AFTER `user_type`");
}

View File

@@ -12,7 +12,7 @@
defined('FROM_GETTING_STARTED') || die("Direct file access is not allowed");
$gs_agents = mysqli_query($mysqli, "SELECT user_id, user_name, user_email, role_name, role_is_admin 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 ORDER BY user_name ASC");
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL ORDER BY user_name ASC");
$gs_roles = userRoles();
// Technician, when there is one - most people added are - or the first role that isn't an admin one
$gs_role_default = '';

View File

@@ -35,7 +35,7 @@ $today = date('Y-m-d');
$search = mb_strtolower(trim(stripslashes((string) ($q ?? ''))));
$rows = [];
$view_counts = ['' => 0, 'archived' => 0];
$result = mysqli_query($mysqli, "SELECT user_id, user_name, user_email, user_avatar, user_archived_at FROM users WHERE user_type = 1
$result = mysqli_query($mysqli, "SELECT user_id, user_name, user_email, user_avatar, user_archived_at FROM users WHERE user_type = 1" . serviceAccountsOutSql() . "
AND (user_archived_at IS NULL OR user_id IN (SELECT user_cost_rate_user_id FROM user_cost_rates))");
while ($row = mysqli_fetch_assoc($result)) {
if ($search !== '' && mb_strpos(mb_strtolower($row['user_name'] . ' ' . $row['user_email']), $search) === false) {

View File

@@ -129,7 +129,7 @@ $portal_client_ids = !empty($t['ticket_template_id']) ? ticketTemplatePortalClie
<select class="form-select select2" name="assigned_to">
<option value="0">- Leave as it is -</option>
<?php
$sql_agents = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE user_type = 1 AND user_status = 1 AND (user_archived_at IS NULL OR user_id = $assigned_to) ORDER BY user_name ASC");
$sql_agents = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND (user_archived_at IS NULL OR user_id = $assigned_to) ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql_agents)) { ?>
<option value="<?= intval($row['user_id']) ?>" <?= intval($row['user_id']) === $assigned_to ? 'selected' : '' ?><?= selectOptionUser($row['user_id'], $row['user_name'], $row['user_avatar'] ?? '') ?>><?= escapeHtml($row['user_name']) ?></option>
<?php } ?>

View File

@@ -8,7 +8,7 @@
require_once '../../includes/modal_header.php';
$agent_count = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE user_type = 1 AND user_archived_at IS NULL AND user_id <> " . intval($session_user_id)))[0]);
$agent_count = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL AND user_id <> " . intval($session_user_id)))[0]);
ob_start();

View File

@@ -15,7 +15,7 @@ $is_last_admin = userIsLastAdmin($user);
// Who can take their work - active agents, not them
$agents = [];
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users
WHERE user_type = 1 AND user_status = 1 AND user_archived_at IS NULL AND user_id <> $user_id ORDER BY user_name ASC");
WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_status = 1 AND user_archived_at IS NULL AND user_id <> $user_id ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$agents[] = $row;
}

View File

@@ -171,6 +171,24 @@ $value = function ($key) use ($form) {
<?php } ?>
</div>
</div>
<?php if (serviceAccountsReady()) {
$form_service = $form_user_id && serviceAccountIs($form_user_id); ?>
<!-- A login only for API keys to act as (functions/service_account.php) -->
<div class="col-12" data-user-service-account>
<input type="hidden" name="service_account_present" value="1">
<div class="border rounded p-3">
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" name="service_account" value="1" id="<?= $form_id ?>ServiceAccount" <?= $form_service ? 'checked' : '' ?>>
<label class="form-check-label fw-semibold" for="<?= $form_id ?>ServiceAccount"><i class="fas fa-fw fa-plug text-body-secondary me-1"></i>API / service account</label>
<div class="form-text mt-0">
For a login that API keys act as, for an integration. It keeps its role, so its keys work, but it's left out of the calendar, every assign picker, the reports and the other agent lists, gets no notifications, and can't sign in to ITFlow.
<?= $is_new ? 'Set a password for it - it isn\'t invited.' : '' ?>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>

View File

@@ -46,6 +46,12 @@ if (isset($_POST['add_user'])) {
$password = (string) ($_POST['password'] ?? '');
$how = ($_POST['sign_in'] ?? ($password !== '' ? 'password' : 'invite')) === 'invite' && userInvitesReady() ? 'invite' : 'password';
// An API / service account never signs in, so it has no one to invite (functions/service_account.php)
if ($how === 'invite' && !empty($_POST['service_account']) && serviceAccountsReady()) {
flashAlert("An API / service account isn't invited - choose Set a password now.", 'error');
redirect();
}
// An invite waiting for them holds a seat already - it is this one
$open = userInviteFor($email);
if (agentUserLimitReached($open && userInviteState($open) === 'pending' ? intval($open['invite_id']) : 0)) {
@@ -114,7 +120,9 @@ if (isset($_POST['add_user'])) {
$notes[] = 'the invite waiting for them is cancelled';
}
logAudit("User", "Create", "$session_name created user " . escapeSql($name), 0, $user_id);
$service_change = serviceAccountSave($user_id, $_POST);
logAudit("User", "Create", "$session_name created user " . escapeSql($name) . ($service_change !== '' ? ' as an API / service account' : ''), 0, $user_id);
flashAlert("User <strong>$name</strong> created - give them their password yourself" . ($notes ? ' - ' . implode('; ', $notes) : ''), $avatar_error ? 'warning' : 'success');
@@ -228,6 +236,11 @@ if (isset($_POST['edit_user'])) {
}
}
$service_change = serviceAccountSave($user_id, $_POST);
if ($service_change !== '') {
$changes[] = $service_change;
}
logAudit("User", "Edit", "$session_name edited user " . escapeSql($name) . ($changes ? ' - ' . escapeSql(implode(', ', $changes)) : ''), 0, $user_id);
flashAlert("User <strong>$name</strong> saved" . ($changes ? ' - ' . implode(', ', $changes) : '') . ($avatar_error ? ". $avatar_error" : ''), $avatar_error ? 'warning' : 'success');
@@ -564,7 +577,7 @@ if (isset($_POST['ir_reset_user_password'])) {
// Agents only - client portal logins have their own reset, and no vault key to rewrite
$reset = [];
$sql_users = mysqli_query($mysqli, "SELECT user_id, user_name, user_email FROM users WHERE user_type = 1 AND user_archived_at IS NULL AND user_id <> $session_user_id ORDER BY user_name");
$sql_users = mysqli_query($mysqli, "SELECT user_id, user_name, user_email FROM users WHERE user_type = 1" . serviceAccountsOutSql() . " AND user_archived_at IS NULL AND user_id <> $session_user_id ORDER BY user_name");
while ($row = mysqli_fetch_assoc($sql_users)) {
$new_password = generateReadablePassword(4);
userSetPassword($row['user_id'], $new_password);

View File

@@ -56,6 +56,15 @@ $shows = [
if (!userInvitesReady()) {
unset($shows['invited']);
}
// API / service accounts (functions/service_account.php) - a view of their own, out of the others but Archived
if (serviceAccountsReady()) {
foreach (['', 'admins', 'nomfa', 'disabled'] as $view_key) {
$shows[$view_key]['where'] .= ' AND user_service_account = 0';
}
$shows = array_slice($shows, 0, array_search('archived', array_keys($shows), true), true)
+ ['service' => ['label' => 'API accounts', 'icon' => 'fa-plug', 'where' => 'user_archived_at IS NULL AND user_service_account = 1', 'title' => 'Logins only for API keys to act as - left out of every agent list, and unable to sign in']]
+ $shows;
}
$show = isset($shows[$_GET['show'] ?? '']) ? ($_GET['show'] ?? '') : '';
// Client portal - contacts, not agents, so a list of their own: narrowed to a client, and by how they sign in
@@ -136,7 +145,7 @@ if ($show === 'portal') {
}
} else {
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS users.user_id, user_name, user_email, user_status, user_token, user_avatar, user_created_at, user_archived_at,
user_role_id, role_name, role_is_admin, user_config_force_mfa
user_role_id, role_name, role_is_admin, user_config_force_mfa" . (serviceAccountsReady() ? ', user_service_account' : '') . "
FROM users $joins
WHERE $filters AND {$shows[$show]['where']}
ORDER BY {$sortable_columns[$sort]}, users.user_id ASC
@@ -361,6 +370,10 @@ $csrf = $_SESSION['csrf_token'];
<nav class="nav nav-pills itflow-asset-tabs mt-2 flex-nowrap overflow-auto">
<?php foreach ($shows as $key => $view) {
// API accounts only once there is one
if ($key === 'service' && !$view_counts['service'] && $show !== 'service') {
continue;
}
if ($key === 'archived' || $key === 'portal') { ?><span class="vr mx-1 my-1"></span><?php }
// The search and role narrow agents, and the search, client and sign-in narrow portal users - none follow to the other side
$view_filters = ($key === 'portal') === ($show === 'portal') ? [] : ['q' => null, 'role' => null, 'client' => null, 'access' => null, 'sort' => null, 'order' => null]; ?>
@@ -707,6 +720,7 @@ $csrf = $_SESSION['csrf_token'];
<div class="min-w-0">
<a class="ajax-modal <?= $is_active ? 'fw-semibold text-body' : 'text-body-secondary' ?>" href="#" data-modal-url="<?= $quick_url ?>"><?= $name ?></a>
<?php if ($is_self) { ?><span class="badge rounded-pill bg-primary-subtle text-primary-emphasis fw-normal ms-1">You</span><?php } ?>
<?php if (!empty($row['user_service_account'])) { ?><span class="badge rounded-pill bg-secondary-subtle text-secondary-emphasis fw-normal ms-1" data-user-service-account-badge title="Only for API keys to act as - left out of every agent list, and can't sign in"><i class="fas fa-plug me-1"></i>API</span><?php } ?>
<?php if ($user_id === $managed_support_id) { ?><span class="badge rounded-pill bg-info-subtle text-info-emphasis fw-normal ms-1" data-managed-support-user title="<?= escapeHtml(managedManagerName()) ?> signs in as this user for support - only from there, with no password. It takes no seat."><i class="fas fa-headset me-1"></i>Support</span><?php } ?>
<div class="small text-body-secondary text-truncate">
<?= escapeHtml($row['user_email']) ?><button type="button" class="itflow-copy clipboardjs" title="Copy email" data-clipboard-text="<?= escapeHtml($row['user_email']) ?>"><i class="far fa-copy"></i></button>