From f43d4a91279c56944af4f5648d8402f7968eda53 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Mon, 19 Oct 2026 00:13:28 -0400 Subject: [PATCH] Audit logs stage 2: record history and a client Audit Log for agents, scoped to their role; auditLogKinds() gives each kind the module that shows it, with new auditLogRoleSql() (everything for an admin, otherwise the kinds the role reads and never Denied), auditLogHistory(), auditLogHistoryUrl(), auditLogHistoryHtml(), auditLogHistoryCard() and auditLogHistorySection(); a History tab on contacts, assets, networks and projects, a History card on racks, documents and the ticket, project, document and license template pages, and a folded History on the file, expense, recurring expense and trip quick looks, each newest first with who, when and the IP for an admin, the latest 50 with how many more and a link to all of them; vendors, locations, licenses, services and products use the shared list, and vendorHistory(), locationHistory(), softwareHistory(), serviceHistory() and productHistory() are removed; new agent/audit_log.php in the client nav after Calendar, with search, who, type, action, one record by name, dates, sortable columns, paging and Changes, Deletions, Sign-ins and Data out views, plus the quick look and a link to Admin > Audit Logs for admins; the client overview's Recent activity is role-scoped and links there for everyone; Your account > Activity rebuilt as the same list over the agent's own entries; the document versions card is now called Versions; the admin audit log quick look links to itself absolutely so it opens from agent pages --- CHANGELOG.md | 10 + admin/document_template.php | 3 + admin/modals/audit_log/audit_log.php | 2 +- admin/project_template.php | 4 + admin/software_template.php | 3 + admin/ticket_template.php | 3 + agent/asset.php | 20 +- agent/audit_log.php | 269 ++++++++++++++++++ agent/client_overview.php | 2 +- agent/contact.php | 14 +- agent/document.php | 11 +- agent/includes/client_side_nav.php | 7 + agent/license.php | 22 +- agent/location.php | 21 +- agent/modals/expense/expense.php | 3 + agent/modals/file/file.php | 3 + .../recurring_expense/recurring_expense.php | 3 + agent/modals/trip/trip.php | 3 + agent/network.php | 15 +- agent/product.php | 18 +- agent/project.php | 17 +- agent/rack.php | 9 +- agent/service.php | 22 +- agent/user/user_activity.php | 243 +++++++++++----- agent/vendor.php | 23 +- functions/audit_log.php | 239 ++++++++++++---- functions/client_overview.php | 10 +- functions/location.php | 16 -- functions/products.php | 16 -- functions/service.php | 16 -- functions/software.php | 16 -- functions/vendor.php | 16 -- 32 files changed, 769 insertions(+), 310 deletions(-) create mode 100644 agent/audit_log.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a0b1ab89b..38b3aea8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -210,6 +210,7 @@ If you are coming from 26.08 or earlier, the database update still has to be run - Deleting an asset, contact, client or location through the API now also cleans up whatever pointed at it, the same as deleting it in ITFlow. - New endpoint to read services, under the Support permission. Vendors take the account manager's phone and email. - A call the key's role doesn't allow is refused with what it needs and what the key's user has - "This needs Modify on Support - the key's user, Tess Tech (Technician), has Read." - in place of "The user linked to this API key does not have permission for this action." It is still a 403. Anything that matches on the old text needs changing. +- Audit log for agents: each client now has an Audit Log, and record pages show their history, to anyone who can open them - not only admins. An agent sees the kinds of entry their role covers - a technician without Financial sees no expenses or trips, someone without Credentials no passwords looked at - and never sign-ins, users, settings or another agent turned away from a client, which stay in Admin > Audit Logs. The client overview's Recent activity follows the same rule, so it can show a restricted role less than before. - Audit log: each thing done now has one name, in the entries already there as well as new ones. Unarchive and Restored are Restore, Deleted is Delete, and Closed, Resolved, Reopened, Merged and Sent are Close, Resolve, Reopen, Merge and Send. An agent turned away from a client they can't open is Denied, where it was Access. A saved card or payment provider deleted is Delete and a card saved is Create, where both were Update, and a contract template changed is Edit. A report or script that reads the `logs` table by the old names needs the new ones. ### New Features & Updates @@ -937,6 +938,11 @@ If you are coming from 26.08 or earlier, the database update still has to be run - The quick look on each entry shows all of it: the whole description, who with their email, the IP address with everything else from it, the browser as it reported itself, the record's other entries with a link to all of them, and what the same person did in the quarter hour either side, each of those opening in turn. - Export downloads what you are looking at, or whatever you pick in the window, as a CSV or PDF, with the columns you choose - who, the record by name and the device among them. Each export is itself in the audit log. - Entries that pointed at the wrong record now point at the right one, old ones included: editing or redacting a ticket reply is on the ticket, a document changed through the API on the document, paying an agent back on the expense that recorded it, and a client restored through the API on the client. + - Records show their own history: a History tab on contacts, assets, networks and projects, a History card on racks, documents and the ticket, project, document and license templates, and History folded at the foot of the file, expense, recurring expense and trip quick looks. Vendors, locations, licenses, services and products keep their History tab, in the same look. Each entry says what was done, who did it with their picture and when - and, for an admin, from which IP address - newest first, the latest 50 with how many more there are and a link to all of them in the audit log. + - Each client has an Audit Log in its menu, after Calendar: everything done for the client, searched and filtered by date, who, type and action, with views for Changes, Deletions, Sign-ins (their contacts signing in to the portal) and Data out, each entry linking to its record. Agents see the kinds of entry their role covers (see Breaking Changes). An admin also gets each entry's quick look and a button to Admin > Audit Logs. A record's history links here for all of it, showing that one record by name. + - The client overview's Recent activity links to the client's Audit Log, for everyone rather than admins only. + - Your account > Activity is rebuilt on the same list: everything you did, searched and filtered by date, a page at a time, with views for Changes, Deletions, Sign-ins and Data out, each entry with its client, its record and where you did it from. Sign-ins says what to do about one that wasn't you. + - A document's earlier versions are now in a card called Versions, with its history under it. - **Emails** (rebuilt) - Ticket emails to clients now look like the billing emails: your logo or name, a coloured heading saying what it is, the ticket's number, subject, status and priority in a box, the reply or the details quoted as they were written with who wrote them, a button to open the ticket, and your details at the foot. This covers a ticket opened by an agent, on a schedule or from an email, a reply, resolved, closed, being made a watcher, replying to a closed ticket, a reply through the API and a task needing their approval. - Each says under it why they get it - the contact on the ticket, watching it, or asking for every update on their company's tickets - in place of the YOU ARE A COLLABORATOR block. @@ -1588,6 +1594,10 @@ If you are coming from 26.08 or earlier, the database update still has to be run - New `functions/audit_log.php`: `auditLogKinds()` (by `log_type`: `icon`, and for a record its `table`, `id`, `name` - a column or expression - `client` and `url`, with `{id}`, `{client}` and `{client_q}`), `auditLogTypeIcon()`, `auditLogActionStyle()` (`tone`, `icon`), `auditLogActionPill()`, `auditLogViews()` (`label`, `icon`, `title`, `where`), `auditLogFiltersSql($src, &$summary)` (`q`, `client`, `user` - an id or `system` - `type`, `action`, `entity`, `view`, `dtf`, `dtt`), `auditLogJoinsSql()`, `auditLogRecords($rows)` (one query per kind), `auditLogRecordLink($row, $records, $label)`, `auditLogDescription($row)` (without the name in front), `auditLogWho($row)` (`name`, `kind` - agent, portal or system - `html`) and `auditLogDevice($user_agent)`. - Admin: `admin/audit_logs.php` is rebuilt (`q`, `client`, `user`, `type`, `action`, `entity`, `view` - `changes`, `deletions`, `signins`, `security` or `data` - the date filter, `sort` - `log_created_at`, `user_name`, `client_name`, `log_type`, `log_action`, `log_ip` or `log_id` - `order` and `page`). New `admin/modals/audit_log/audit_log.php` (`id`) and `audit_log_export.php`, and `admin/post/audit_log.php` with `export_audit_logs` (the same filters, `columns[]` from the `audit_logs` export columns), written 500 rows at a time and audited as Audit Log Export. - `agent/files.php` takes `open` (a file's id) to open its quick look, as the credential, certificate, domain, expense, recurring expense and trip lists do. + - Record history: `auditLogKinds()` gives each kind the `module` whose read access shows it to an agent (none: admins only). New `auditLogRoleSql()` (everything for an admin; else those kinds, and no Denied), `auditLogHistory($type, $id, $limit = 50)` (`type`, `id`, `client_id`, `rows`, `count`; role-scoped), `auditLogHistoryUrl($history, $client_id)` (Admin > Audit Logs for an admin, the client's Audit Log for an agent, '' otherwise), `auditLogHistoryHtml($history, $opts)` (`client_id`, `empty`, `flush`, `compact`), `auditLogHistoryCard()` (compact) and `auditLogHistorySection()` (collapsed, nothing when empty). Contacts, assets, networks and projects add a `history` tab; `agent/rack.php`, `agent/document.php` and the four admin template pages a card; the file, expense, recurring expense and trip quick looks a section. `vendorHistory()`, `locationHistory()`, `softwareHistory()`, `serviceHistory()` and `productHistory()` are removed - their pages use `auditLogHistory()`, whose count is now the tab's. + - New `agent/audit_log.php` (`client_id` - without it, admins go to Admin > Audit Logs and agents to the clients - `q`, `user`, `type`, `action`, `entity`, `view` - `changes`, `deletions`, `signins` or `data` - the date filter, `sort` - `log_created_at`, `user_name`, `log_type`, `log_action`, `log_ip` or `log_id` - `order` and `page`), scoped to the client and `auditLogRoleSql()`, in `client_side_nav.php` after Calendar. `clientOverviewActivity()` applies `auditLogRoleSql()`. + - `agent/user/user_activity.php` is rebuilt (`q`, `view`, the date filter, `sort` - `log_created_at`, `log_type`, `log_action`, `client_name`, `log_ip` or `log_id` - `order` and `page`), on the agent's own entries. + - The admin quick look's links to itself are absolute, so it opens from agent pages too. - Empty states: every list page sets `$empty_state` before including `includes/filter_footer.php`, and `includes/inc_empty_state.php` draws it with `emptyStateHtml()` (`functions/format.php`) when the list has nothing - `thing`, `icon`, `title`, `text`, `actions` (`label`, `modal` or `href`, `size`, `icon`; the first is the main one), `archived`, `view`, `filtered`, `views`, `match_title`, `match_text` and `clear_url`. `emptyStateFiltered($views)` reads the GET string for a search or filter, leaving out paging, sorting, `client_id`, `archived` and the page's views, and `emptyStateClearUrl($views)` is the page without them. `emptyStatePill($pills, $key, $default, $good)` builds a pill's own state from the page's pill array (`label`, `icon`, `title`, `alarm`), with a tick for `alarm` pills and the keys in `$good`. Files, notifications and racks call `emptyStateHtml()` in their own layout; files and notifications set `$empty_state_skip`. A page that sets nothing still gets one, worded from `$page_title`. The pages' own empty blocks (`.itflow-asset-empty` under a list) are gone. CSS: `.itflow-empty-state` with `-icon` (a grey round tile), `-title`, `-text` and `-actions`. - Removed unused helpers and files: - `credentialLink()`, `projectLink()`, `linkCounts()`, `assetContactAvatar()`, `clientOverviewPersonInitials()`, and `getMonthlyTax()` and `getQuarterlyTax()`, which looked a tax up by name diff --git a/admin/document_template.php b/admin/document_template.php index 19130e071..9e175674e 100644 --- a/admin/document_template.php +++ b/admin/document_template.php @@ -326,6 +326,9 @@ $required_url = "modals/document_template/document_template_required.php?id=$doc + + + diff --git a/admin/modals/audit_log/audit_log.php b/admin/modals/audit_log/audit_log.php index 94b9326ee..1027263ca 100644 --- a/admin/modals/audit_log/audit_log.php +++ b/admin/modals/audit_log/audit_log.php @@ -64,7 +64,7 @@ if ($user_id > 0) { $entry_line = function ($entry, $show_when_as = 'date') { $time = strtotime($entry['log_created_at']); $when = $show_when_as === 'time' ? date('g:i A', $time) : date('M j, Y g:i A', $time); - return '' + return '' . '' . escapeHtml($when) . '' . '' . auditLogActionPill($entry['log_action']) . '' . '' . escapeHtml($entry['log_description']) . '' diff --git a/admin/project_template.php b/admin/project_template.php index 0dded67cd..25623fddf 100644 --- a/admin/project_template.php +++ b/admin/project_template.php @@ -159,6 +159,10 @@ $archived = !empty($template['project_template_archived_at']);

[Client], [Project], [Month], [Quarter] and [Year] in a ticket template's subject are filled in. Anything else in brackets is left for the agent.

+ + + + diff --git a/admin/software_template.php b/admin/software_template.php index 641a09dee..92d40618a 100644 --- a/admin/software_template.php +++ b/admin/software_template.php @@ -362,6 +362,9 @@ $facts = array_filter([ + + + diff --git a/admin/ticket_template.php b/admin/ticket_template.php index 33968b193..4a670a752 100644 --- a/admin/ticket_template.php +++ b/admin/ticket_template.php @@ -402,6 +402,9 @@ $edit_url = "modals/ticket_template/ticket_template_edit.php?id=$ticket_template + + + diff --git a/agent/asset.php b/agent/asset.php index c83fdd64f..adf7c3c0c 100644 --- a/agent/asset.php +++ b/agent/asset.php @@ -7,10 +7,10 @@ * (header), how to get on it (IP, MAC, links - each one copies or opens in a * click), what it physically is and whether it is still under warranty, where * it is and who has it. Everything linked to it - interfaces, credentials, - * tickets, licenses, documents, files, services, notes - sits in tabs with - * counts, so nothing is hidden just because it is empty and the page does not - * run to ten screens. The open tab follows the URL hash (#tickets) and is - * remembered between visits. + * tickets, licenses, documents, files, services, notes - and its history + * sit in tabs with counts, so nothing is hidden just because it is empty and + * the page does not run to ten screens. The open tab follows the URL hash + * (#tickets) and is remembered between visits. */ // If client_id is in URI then show client Side Bar and client header @@ -211,6 +211,9 @@ if (mysqli_num_rows($sql) == 0) { } // The tabs, in the order a tech reaches for them + // Its history, from the audit log + $history = auditLogHistory('Asset', $asset_id); + $asset_tabs = [ 'interfaces' => ['label' => 'Interfaces', 'icon' => 'fa-ethernet', 'count' => $interface_count], 'credentials' => ['label' => 'Credentials', 'icon' => 'fa-key', 'count' => $credential_count], @@ -221,6 +224,7 @@ if (mysqli_num_rows($sql) == 0) { 'files' => ['label' => 'Files', 'icon' => 'fa-paperclip', 'count' => $files_count], 'services' => ['label' => 'Services', 'icon' => 'fa-stream', 'count' => $service_count], 'notes' => ['label' => 'Notes', 'icon' => 'fa-sticky-note', 'count' => $note_count], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$can_credentials) { unset($asset_tabs['credentials']); @@ -1030,6 +1034,14 @@ if (mysqli_num_rows($sql) == 0) { + +
+
+ Changes, moves and notes - newest first. +
+ $client_id]) ?> +
+
diff --git a/agent/audit_log.php b/agent/audit_log.php new file mode 100644 index 000000000..27b6bc187 --- /dev/null +++ b/agent/audit_log.php @@ -0,0 +1,269 @@ + Audit Logs. + * Record pages link here for all of one record's history (type, entity). + */ + +// Default Column Sortby Filter +$sort = "log_id"; +$order = "DESC"; + +if (!isset($_GET['client_id'])) { + // Every client's is Admin > Audit Logs + require_once $_SERVER['DOCUMENT_ROOT'] . '/config.php'; + require_once $_SERVER['DOCUMENT_ROOT'] . '/functions.php'; + require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/check_login.php'; + redirect($session_is_admin ? '/admin/audit_logs.php' : '/agent/clients.php'); +} + +require_once "includes/inc_all_client.php"; + +$page_title = 'Audit Log'; + +$sortable_columns = [ + 'log_id' => "log_id $order", + 'log_created_at' => "log_created_at $order, log_id $order", + 'user_name' => "user_name IS NULL, user_name $order, log_id DESC", + 'log_type' => "log_type $order, log_action $order, log_id DESC", + 'log_action' => "log_action $order, log_id DESC", + 'log_ip' => "log_ip IS NULL, log_ip $order, log_id DESC", +]; +if (!isset($sortable_columns[$sort])) { + $sort = 'log_id'; +} + +// Security is sign-ins, users and settings - an admin's, and in Admin > Audit Logs +$views = auditLogViews(); +unset($views['security']); +$views['signins']['title'] = 'Their contacts signing in to the client portal'; + +$view = isset($_GET['view']) && is_string($_GET['view']) && isset($views[$_GET['view']]) ? $_GET['view'] : ''; +$scope = "log_client_id = $client_id AND " . auditLogRoleSql(); +$filter_src = array_merge($_GET, ['client' => $client_id, 'dtf' => $dtf, 'dtt' => $dtt, 'view' => $view]); +$joins = auditLogJoinsSql(); + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS log_id, log_type, log_action, log_description, log_ip, log_user_agent, log_created_at, log_client_id, log_user_id, log_entity_id, + user_name, user_avatar, user_type, client_name + FROM logs $joins + WHERE $scope AND " . auditLogFiltersSql($filter_src) . " + ORDER BY {$sortable_columns[$sort]} + LIMIT $record_from, $record_to"); +$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); + +$rows = []; +while ($row = mysqli_fetch_assoc($sql)) { + $rows[] = $row; +} +$records = auditLogRecords($rows); +$kinds = auditLogKinds(); + +// Each view counted for the rest of the filters +$count_sql = []; +foreach ($views as $definition) { + $count_sql[] = 'COALESCE(SUM(' . ($definition['where'] === '' ? '1' : $definition['where']) . '), 0)'; +} +$counts = array_combine(array_keys($views), array_map('intval', mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " + FROM logs $joins WHERE $scope AND " . auditLogFiltersSql(array_merge($filter_src, ['view' => ''])))))); + +$user_filter = is_string($_GET['user'] ?? null) ? $_GET['user'] : ''; +$type_filter = is_string($_GET['type'] ?? null) ? $_GET['type'] : ''; +$action_filter = is_string($_GET['action'] ?? null) ? $_GET['action'] : ''; +$entity_filter = is_scalar($_GET['entity'] ?? null) ? intval($_GET['entity']) : 0; +$more_filters = $action_filter !== '' || $entity_filter; +$any_filter = $q !== '' || $user_filter !== '' || $type_filter !== '' || $action_filter !== '' || $entity_filter || $dtf !== '1970-01-01' || $dtt !== '2099-12-31'; + +$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { + return '' + . $label . ($sort === $column ? " $order_icon" : '') . ''; +}; +$view_url = function ($key) { + $query = $_GET; + unset($query['page']); + $query['view'] = $key; + return '?' . http_build_query(array_filter($query, function ($value) { + return $value !== '' && $value !== null && $value !== []; + })); +}; +$view_empty = [ + 'changes' => 'No changes yet', + 'deletions' => 'Nothing deleted yet', + 'signins' => 'No portal sign-ins yet', + 'data' => 'Nothing looked at or taken out yet', +]; + +?> + +
+
+

Audit Log

+ + + +
+
+
+ + +
+
+
+ + + +
+
+
+ + + + +
+
+ +
+
+ +
+
+
+
+
+
+ + +
+
+ $type_filter, 'log_entity_id' => $entity_filter]])[$type_filter . '|' . $entity_filter] ?? null; ?> +
+ + Just one record: + + + + # + + Every record +
+ +
+
+
+ +
+
+ + "> + + + + + + + + + + + 0 && isset($kinds[$type]['table']); + $device = auditLogDevice($row['log_user_agent']); + $from = implode(' · ', array_filter([(string) $row['log_ip'], $device])); + ?> + + + + + + + + + + + + +
Description
+
+
+
+
+ +
+
+ +
+ +
No longer here
+ + +
+ +
+
+
+
+ +
+
+ 'audit log entries', + 'icon' => 'fa-history', + 'filtered' => $any_filter, + 'views' => ['view'], + 'view' => $view !== '' ? ['icon' => $views[$view]['icon'], 'title' => $view_empty[$view], 'text' => escapeHtml($views[$view]['title']) . ' show here.'] : null, + 'text' => 'Who changed what for ' . $client_name . ', looked at a password or took something out - when, and from where - shows here.', + ]; + require_once "../includes/filter_footer.php"; + ?> +
+ +

Recent activity

- Audit log + Audit log
diff --git a/agent/contact.php b/agent/contact.php index acbd0fbdf..c667b47e4 100644 --- a/agent/contact.php +++ b/agent/contact.php @@ -7,7 +7,7 @@ * with a click to call or email them; how to reach them and the details a * tech needs on a call (location, PIN, portal access) down the side; and * everything linked to them - assets, credentials, tickets, licenses, - * documents, files, services, notes - in tabs with counts. The open tab + * documents, files, services, notes - and their history in tabs with counts. The open tab * follows the URL hash (#tickets) and is remembered between visits. */ @@ -171,6 +171,9 @@ while ($row = mysqli_fetch_assoc($sql_note_type_icons)) { } // The tabs, in the order a tech reaches for them +// Its history, from the audit log +$history = auditLogHistory('Contact', $contact_id); + $contact_tabs = [ 'assets' => ['label' => 'Assets', 'icon' => 'fa-desktop', 'count' => $asset_count], 'credentials' => ['label' => 'Credentials', 'icon' => 'fa-key', 'count' => $credential_count], @@ -181,6 +184,7 @@ $contact_tabs = [ 'files' => ['label' => 'Files', 'icon' => 'fa-paperclip', 'count' => $file_count], 'services' => ['label' => 'Services', 'icon' => 'fa-stream', 'count' => $service_count], 'notes' => ['label' => 'Notes', 'icon' => 'fa-sticky-note', 'count' => $note_count], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$can_credentials) { unset($contact_tabs['credentials']); @@ -773,6 +777,14 @@ if (!$can_support) {
+ +
+
+ Changes to them, their portal access and notes - newest first. +
+ $client_id]) ?> +
+
diff --git a/agent/document.php b/agent/document.php index 50df50ed8..992d91044 100644 --- a/agent/document.php +++ b/agent/document.php @@ -1,8 +1,8 @@
-

History

+

Versions

  • @@ -279,6 +279,11 @@ $page_title = $row['document_name'];
+ +
+ $client_id]) ?> +
+
diff --git a/agent/includes/client_side_nav.php b/agent/includes/client_side_nav.php index a25d12d16..452509b67 100644 --- a/agent/includes/client_side_nav.php +++ b/agent/includes/client_side_nav.php @@ -122,6 +122,13 @@ + + = 1) { ?> diff --git a/agent/license.php b/agent/license.php index 35834369c..0a31f3c3c 100644 --- a/agent/license.php +++ b/agent/license.php @@ -75,7 +75,7 @@ if (!$license || ($client_url && intval($license['software_client_id']) !== intv $credentials = $can_credentials ? credentialRows("credential_id IN (SELECT credential_id FROM software_credentials WHERE software_id = $software_id)") : []; $documents = linkedRecords('software', $software_id, 'document'); $files = linkedRecords('software', $software_id, 'file'); - $history = softwareHistory($software_id, 100); + $history = auditLogHistory('Software', $software_id); $license_tabs = [ 'seats' => ['label' => 'Seats', 'icon' => 'fa-users', 'count' => $seats['used']], @@ -83,7 +83,7 @@ if (!$license || ($client_url && intval($license['software_client_id']) !== intv 'credentials' => ['label' => 'Credentials', 'icon' => 'fa-key', 'count' => count($credentials)], 'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => count($documents)], 'files' => ['label' => 'Files', 'icon' => 'fa-paperclip', 'count' => count($files)], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$can_credentials) { unset($license_tabs['credentials']); @@ -571,23 +571,7 @@ if (!$license || ($client_url && intval($license['software_client_id']) !== intv
Renewals, seat and key changes, edits - newest first.
- 'fa-plus', 'Edit' => 'fa-edit', 'Renew' => 'fa-redo-alt', 'Seats' => 'fa-users', 'Keys' => 'fa-key', - 'Link' => 'fa-link', 'Unlink' => 'fa-unlink', 'Archive' => 'fa-archive', 'Restore' => 'fa-redo', 'Export' => 'fa-download']; ?> -
    - -
  • - -
    -
    -
    ·
    -
    -
  • - -
- -
Nothing logged yet.
- + $client_id]) ?>
diff --git a/agent/location.php b/agent/location.php index 7b7ce6b2b..6c9ba0799 100644 --- a/agent/location.php +++ b/agent/location.php @@ -111,7 +111,7 @@ if (!$location || ($client_url && intval($location['location_client_id']) !== in $documents = $can_support ? linkedRecords('location', $location_id, 'document') : []; $files = $can_support ? linkedRecords('location', $location_id, 'file') : []; - $history = locationHistory($location_id, 100); + $history = auditLogHistory('Location', $location_id); $location_tabs = [ 'people' => ['label' => 'People', 'icon' => 'fa-users', 'count' => count($people)], @@ -122,7 +122,7 @@ if (!$location || ($client_url && intval($location['location_client_id']) !== in 'trips' => ['label' => 'Trips', 'icon' => 'fa-route', 'count' => $trip_count], 'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => count($documents)], 'files' => ['label' => 'Files', 'icon' => 'fa-paperclip', 'count' => count($files)], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$show_trips) { unset($location_tabs['trips']); @@ -641,22 +641,7 @@ if (!$location || ($client_url && intval($location['location_client_id']) !== in
Edits, moves and notes - newest first.
- 'fa-plus', 'Edit' => 'fa-edit', 'Archive' => 'fa-archive', 'Restore' => 'fa-redo', 'Link' => 'fa-link', 'Unlink' => 'fa-unlink']; ?> - - -
Nothing logged yet.
- + $client_id]) ?> diff --git a/agent/modals/expense/expense.php b/agent/modals/expense/expense.php index b2c167e2d..1447a76d1 100644 --- a/agent/modals/expense/expense.php +++ b/agent/modals/expense/expense.php @@ -262,6 +262,9 @@ ob_start(); + + $client_id]) ?> + + + $client_id]) ?> + + + $client_id]) ?> + + + $client_id]) ?> + + +
+
+ Changes to the network - newest first. +
+ $client_id]) ?> +
+
diff --git a/agent/product.php b/agent/product.php index be4b91056..aff612deb 100644 --- a/agent/product.php +++ b/agent/product.php @@ -64,7 +64,7 @@ if (!$product) { $usage = productUsage($product_id); $moves = $type === 'product' ? productStockMoves($product_id, 200) : []; - $history = productHistory($product_id, 100); + $history = auditLogHistory('Product', $product_id); $sold = $usage['sold']; // Recurring first for what is billed each month, quarter or year, invoices first for the rest @@ -73,7 +73,7 @@ if (!$product) { 'invoices' => ['label' => 'Invoices', 'icon' => 'fa-file-invoice-dollar', 'count' => count($usage['invoices'])], 'quotes' => ['label' => 'Open quotes', 'icon' => 'fa-comment-dollar', 'count' => count($usage['quotes'])], 'prices' => ['label' => 'Prices', 'icon' => 'fa-tags', 'count' => count($prices)], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$product['product_frequency']) { $tabs = ['invoices' => $tabs['invoices'], 'quotes' => $tabs['quotes'], 'recurring' => $tabs['recurring'], 'prices' => $tabs['prices'], 'history' => $tabs['history']]; @@ -645,19 +645,7 @@ if (!$product) {
- -
    - -
  • - -
    - -
  • - -
- -
Nothing recorded yet.
- + 0]) ?>
diff --git a/agent/project.php b/agent/project.php index b6fd7ea3f..a8a684cbb 100644 --- a/agent/project.php +++ b/agent/project.php @@ -3,8 +3,9 @@ /* * A project - what it is, who it is for and who runs it, how far along it * is, the time logged against the hours budgeted and when it is due, then - * its tickets, all their tasks and what has been said on them lately. The - * side shows the scope, the details and the team that has worked on it. + * its tickets, all their tasks, what has been said on them lately and its + * history. The side shows the scope, the details and the team that has + * worked on it. * * Progress and time count every ticket on the project; the lists show the * tickets this agent may reach (an internal project can hold any client's). @@ -118,10 +119,14 @@ $team = projectTeam($project_id); // Templates to add tickets from $has_templates = $can_edit && $is_open && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM project_templates WHERE project_template_archived_at IS NULL LIMIT 1")); +// Its history, from the audit log +$history = auditLogHistory('Project', $project_id); + $project_tabs = [ 'tickets' => ['label' => 'Tickets', 'icon' => 'fa-life-ring', 'count' => count($tickets), 'badge' => $stats['open']], 'tasks' => ['label' => 'Tasks', 'icon' => 'fa-tasks', 'count' => $stats['tasks']], 'activity' => ['label' => 'Activity', 'icon' => 'fa-comments', 'count' => count($activity)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; $new_ticket_url = "modals/ticket/ticket_add.php?" . ($project_client_id ? "client_id=$project_client_id&" : '') . "project_id=$project_id"; @@ -543,6 +548,14 @@ $contact_archived = !empty($project['contact_archived_at']);
+ +
+
+ Changes to the project - newest first. +
+ $project_client_id]) ?> +
+
diff --git a/agent/rack.php b/agent/rack.php index 4f7e58793..a9dd04063 100644 --- a/agent/rack.php +++ b/agent/rack.php @@ -7,8 +7,8 @@ * position on the face it is mounted on, every empty U a click away from * adding something there. Around it, how full the rack is (units, the largest * gap a full-depth item could still go in, power against the budget), the - * rack's details and photos, the item list for print and search, and the - * client's network gear that isn't racked anywhere yet. + * rack's details and photos, the item list for print and search, the + * client's network gear that isn't racked anywhere yet, and its history. */ require_once "includes/inc_all_client.php"; @@ -441,6 +441,11 @@ if (mysqli_num_rows($sql) == 0) {
+ +
+ $client_id]) ?> +
+ diff --git a/agent/service.php b/agent/service.php index a33be187d..40b2ea78f 100644 --- a/agent/service.php +++ b/agent/service.php @@ -119,7 +119,7 @@ if (!$service || ($client_url && intval($service['service_client_id']) !== intva $linked_ticket_ids[intval($row[0])] = true; } $open_tickets = count(array_filter($tickets, 'ticketIsOpen')); - $history = serviceHistory($service_id, 100); + $history = auditLogHistory('Service', $service_id); $service_tabs = [ 'assets' => ['label' => 'Runs on', 'icon' => 'fa-desktop', 'count' => count($assets)], @@ -127,7 +127,7 @@ if (!$service || ($client_url && intval($service['service_client_id']) !== intva 'web' => ['label' => 'Domains & SSL', 'icon' => 'fa-globe', 'count' => count($domains) + count($certificates)], 'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => count($documents)], 'tickets' => ['label' => 'Tickets', 'icon' => 'fa-life-ring', 'count' => count($tickets), 'alarm' => $open_tickets], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$can_credentials) { unset($service_tabs['logins']); @@ -640,23 +640,7 @@ if (!$service || ($client_url && intval($service['service_client_id']) !== intva
Reviews, links, edits - newest first.
- 'fa-plus', 'Edit' => 'fa-edit', 'Review' => 'fa-clipboard-check', 'Link' => 'fa-link', 'Unlink' => 'fa-unlink', - 'Archive' => 'fa-archive', 'Restore' => 'fa-redo', 'Export' => 'fa-download']; ?> - - -
Nothing logged yet.
- + $client_id]) ?> diff --git a/agent/user/user_activity.php b/agent/user/user_activity.php index b42d587b7..abb8bf0bb 100644 --- a/agent/user/user_activity.php +++ b/agent/user/user_activity.php @@ -1,92 +1,197 @@ Activity - your latest sign ins (and wrong MFA codes), and - * the latest things you did. Yours only. + * Your account > Activity - everything you did, from the audit log: your + * changes, your sign ins (and wrong MFA codes), the passwords you looked at + * and what you exported, with when, the client and from where. Searched, + * filtered by date, in views, a page at a time, each entry linking to its + * record while it still exists. Yours only. */ +// Default Column Sortby Filter +$sort = "log_id"; +$order = "DESC"; + require_once "includes/inc_all_user.php"; -$logins = userRecentLogins($session_user_id, 20); +$page_title = 'Activity'; -$activity = []; -$sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_created_at, log_client_id, client_name FROM logs - LEFT JOIN clients ON client_id = log_client_id AND log_client_id > 0 - WHERE log_user_id = $session_user_id AND log_type NOT IN ('Login', 'Login 2FA', 'Logout') - ORDER BY log_id DESC LIMIT 25"); -while ($row = mysqli_fetch_assoc($sql)) { - $activity[] = $row; +$sortable_columns = [ + 'log_id' => "log_id $order", + 'log_created_at' => "log_created_at $order, log_id $order", + 'client_name' => "client_name IS NULL, client_name $order, log_id DESC", + 'log_type' => "log_type $order, log_action $order, log_id DESC", + 'log_action' => "log_action $order, log_id DESC", + 'log_ip' => "log_ip IS NULL, log_ip $order, log_id DESC", +]; +if (!isset($sortable_columns[$sort])) { + $sort = 'log_id'; } -$action_icons = [ - 'Create' => 'fa-plus text-success', - 'Edit' => 'fa-pen text-info', - 'Modify' => 'fa-pen text-info', - 'Delete' => 'fa-trash-alt text-danger', - 'Archive' => 'fa-archive text-secondary', - 'Restore' => 'fa-redo text-secondary', - 'Export' => 'fa-download text-secondary', - 'Upload' => 'fa-upload text-secondary', - 'View' => 'fa-eye text-secondary', - 'Reply' => 'fa-reply text-primary', +$views = auditLogViews(); +unset($views['security']); +$views['signins']['title'] = 'Signing in and out, and wrong MFA codes'; +$views['data']['title'] = 'Passwords you looked at, and what you exported, downloaded and shared'; + +$view = isset($_GET['view']) && is_string($_GET['view']) && isset($views[$_GET['view']]) ? $_GET['view'] : ''; +$scope = "log_user_id = " . intval($session_user_id); +$filter_src = array_merge($_GET, ['user' => '', 'dtf' => $dtf, 'dtt' => $dtt, 'view' => $view]); +$joins = auditLogJoinsSql(); + +$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS log_id, log_type, log_action, log_description, log_ip, log_user_agent, log_created_at, log_client_id, log_user_id, log_entity_id, + user_name, client_name + FROM logs $joins + WHERE $scope AND " . auditLogFiltersSql($filter_src) . " + ORDER BY {$sortable_columns[$sort]} + LIMIT $record_from, $record_to"); +$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); + +$rows = []; +while ($row = mysqli_fetch_assoc($sql)) { + $rows[] = $row; +} +$records = auditLogRecords($rows); +$kinds = auditLogKinds(); + +// Each view counted for the search and dates +$count_sql = []; +foreach ($views as $definition) { + $count_sql[] = 'COALESCE(SUM(' . ($definition['where'] === '' ? '1' : $definition['where']) . '), 0)'; +} +$counts = array_combine(array_keys($views), array_map('intval', mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " + FROM logs $joins WHERE $scope AND " . auditLogFiltersSql(array_merge($filter_src, ['view' => ''])))))); + +$any_filter = $q !== '' || $dtf !== '1970-01-01' || $dtt !== '2099-12-31'; + +$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { + return '' + . $label . ($sort === $column ? " $order_icon" : '') . ''; +}; +$view_url = function ($key) { + $query = $_GET; + unset($query['page']); + $query['view'] = $key; + return '?' . http_build_query(array_filter($query, function ($value) { + return $value !== '' && $value !== null && $value !== []; + })); +}; +$view_empty = [ + 'changes' => 'No changes yet', + 'deletions' => 'Nothing deleted yet', + 'signins' => 'No sign ins yet', + 'data' => 'Nothing looked at or taken out yet', ]; ?> -
- -
-
-
-

Your sign ins

+
+
+

Your activity

+ + - -
    - -
  • - -
- - -
None yet.
- -
+
- -
-
-
-

What you did

- -
- Audit log +
+
+ +
+
+
+ +
- +
+
+ + + + +
- -
    - -
  • - -
    -
    -
    - - · - - - -
    -
    -
  • - -
- -
Nothing yet.
+
+
+
- +
+ + "> + + + + + + + + + + 0 && isset($kinds[$type]['table']); + $device = auditLogDevice($row['log_user_agent']); + $from = implode(' · ', array_filter([(string) $row['log_ip'], $device])); + ?> + + + + + + + + + +
Description
+
+
+
+
+ +
+
+ +
+ +
No longer here
+ + +
+ +
+ + + + - + + +
+
+
+
+ 0) { ?> + + + 'entries', + 'icon' => 'fa-history', + 'filtered' => $any_filter, + 'views' => ['view'], + 'view' => $view !== '' ? ['icon' => $views[$view]['icon'], 'title' => $view_empty[$view], 'text' => escapeHtml($views[$view]['title']) . ' show here.'] : null, + 'title' => 'Nothing yet', + 'text' => 'What you change, your sign ins, the passwords you look at and what you export show here.', + ]; + require_once "../../includes/filter_footer.php"; + ?>
= 1 ? vendorElsewhere($vendor) : []; - $history = vendorHistory($vendor_id, 100); + $history = auditLogHistory('Vendor', $vendor_id); if ($is_ours) { $vendor_tabs = [ @@ -175,7 +175,7 @@ if (!$vendor || (isset($_GET['client_id']) && $vendor_client_id !== intval($clie 'products' => ['label' => 'Products', 'icon' => 'fa-box-open', 'count' => count($vendor_products)], 'cases' => ['label' => 'Cases', 'icon' => 'fa-life-ring', 'count' => $case_count, 'badge' => $case_open_count], 'domains' => ['label' => 'Client domains', 'icon' => 'fa-globe', 'count' => count($domains)], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$domains) { unset($vendor_tabs['domains']); @@ -199,7 +199,7 @@ if (!$vendor || (isset($_GET['client_id']) && $vendor_client_id !== intval($clie 'logins' => ['label' => 'Logins', 'icon' => 'fa-key', 'count' => count($logins)], 'documents' => ['label' => 'Documents', 'icon' => 'fa-file-alt', 'count' => count($documents)], 'files' => ['label' => 'Files', 'icon' => 'fa-paperclip', 'count' => count($files)], - 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => count($history)], + 'history' => ['label' => 'History', 'icon' => 'fa-history', 'count' => $history['count']], ]; if (!$can_support) { unset($vendor_tabs['cases'], $vendor_tabs['licenses'], $vendor_tabs['assets'], $vendor_tabs['domains'], $vendor_tabs['services'], $vendor_tabs['documents'], $vendor_tabs['files']); @@ -794,22 +794,7 @@ if (!$vendor || (isset($_GET['client_id']) && $vendor_client_id !== intval($clie
Edits, links and notes - newest first.
- 'fa-plus', 'Edit' => 'fa-edit', 'Archive' => 'fa-archive', 'Restore' => 'fa-redo', 'Link' => 'fa-link', 'Unlink' => 'fa-unlink']; ?> -
    - -
  • - -
    -
    -
    ·
    -
    -
  • - -
- -
Nothing logged yet.
- + $client_id]) ?>
diff --git a/functions/audit_log.php b/functions/audit_log.php index 45b358fd6..670589573 100644 --- a/functions/audit_log.php +++ b/functions/audit_log.php @@ -6,7 +6,10 @@ * writes to the logs table. Admin > Audit Logs lists it (admin/audit_logs.php) * in views, filtered and exported the same way (auditLogFiltersSql()), each * entry with its record when it still exists (auditLogRecords()) and a quick - * look (admin/modals/audit_log/audit_log.php). + * look (admin/modals/audit_log/audit_log.php). A client's Audit Log + * (agent/audit_log.php) and Your account > Activity list the same way, and + * record pages show their own history (auditLogHistory()) - for an agent, + * only the kinds of entry their role can see (auditLogRoleSql()). * * An entry's log_type says what kind of record it is about and log_entity_id * which one; log_action is what was done, one verb (Create, Edit, Delete, @@ -16,38 +19,40 @@ /* * The kinds of record an entry can be about, by its log_type - each with its - * icon and, for the ones that are records, where to find the record: its - * table, id and name columns, its client column, and its page ('url') - for - * a record with a quick look rather than a page, its list with ?open= (or - * ?share=), which opens it. {id} is the record, {client} its client, and - * {client_q} "client_id=N&" when it has one. + * icon, the part of ITFlow ('module') whose read access lets an agent see + * entries of that kind (none: admins only - sign-ins, users, settings, + * templates and the like), and, for the ones that are records, where to find + * the record: its table, id and name columns, its client column, and its + * page ('url') - for a record with a quick look rather than a page, its list + * with ?open= (or ?share=), which opens it. {id} is the record, {client} its + * client, and {client_q} "client_id=N&" when it has one. */ function auditLogKinds() { return [ - 'Client' => ['icon' => 'fa-user-friends', 'table' => 'clients', 'id' => 'client_id', 'name' => 'client_name', 'url' => '/agent/client_overview.php?client_id={id}'], - 'Contact' => ['icon' => 'fa-address-card', 'table' => 'contacts', 'id' => 'contact_id', 'name' => 'contact_name', 'client' => 'contact_client_id', 'url' => '/agent/contact.php?client_id={client}&contact_id={id}'], - 'Location' => ['icon' => 'fa-map-marker-alt', 'table' => 'locations', 'id' => 'location_id', 'name' => 'location_name', 'client' => 'location_client_id', 'url' => '/agent/location.php?client_id={client}&location_id={id}'], - 'Vendor' => ['icon' => 'fa-building', 'table' => 'vendors', 'id' => 'vendor_id', 'name' => 'vendor_name', 'client' => 'vendor_client_id', 'url' => '/agent/vendor.php?{client_q}vendor_id={id}'], - 'Asset' => ['icon' => 'fa-desktop', 'table' => 'assets', 'id' => 'asset_id', 'name' => 'asset_name', 'client' => 'asset_client_id', 'url' => '/agent/asset.php?client_id={client}&asset_id={id}'], - 'Network' => ['icon' => 'fa-network-wired', 'table' => 'networks', 'id' => 'network_id', 'name' => 'network_name', 'client' => 'network_client_id', 'url' => '/agent/network.php?client_id={client}&network_id={id}'], - 'Rack' => ['icon' => 'fa-server', 'table' => 'racks', 'id' => 'rack_id', 'name' => 'rack_name', 'client' => 'rack_client_id', 'url' => '/agent/rack.php?{client_q}rack_id={id}'], - 'Credential' => ['icon' => 'fa-key', 'table' => 'credentials', 'id' => 'credential_id', 'name' => 'credential_name', 'client' => 'credential_client_id', 'url' => '/agent/credentials.php?{client_q}open={id}'], - 'Certificate' => ['icon' => 'fa-lock', 'table' => 'certificates', 'id' => 'certificate_id', 'name' => 'certificate_name', 'client' => 'certificate_client_id', 'url' => '/agent/certificates.php?{client_q}open={id}'], - 'Domain' => ['icon' => 'fa-globe', 'table' => 'domains', 'id' => 'domain_id', 'name' => 'domain_name', 'client' => 'domain_client_id', 'url' => '/agent/domains.php?{client_q}open={id}'], - 'Software' => ['icon' => 'fa-cube', 'table' => 'software', 'id' => 'software_id', 'name' => 'software_name', 'client' => 'software_client_id', 'url' => '/agent/license.php?client_id={client}&software_id={id}'], - 'Service' => ['icon' => 'fa-stream', 'table' => 'services', 'id' => 'service_id', 'name' => 'service_name', 'client' => 'service_client_id', 'url' => '/agent/service.php?client_id={client}&service_id={id}'], - 'Document' => ['icon' => 'fa-file-alt', 'table' => 'documents', 'id' => 'document_id', 'name' => 'document_name', 'client' => 'document_client_id', 'url' => '/agent/document.php?client_id={client}&document_id={id}'], - 'File' => ['icon' => 'fa-file', 'table' => 'files', 'id' => 'file_id', 'name' => 'file_name', 'client' => 'file_client_id', 'url' => '/agent/files.php?client_id={client}&open={id}'], - 'Share' => ['icon' => 'fa-share-alt', 'table' => 'shared_items', 'id' => 'item_id', 'name' => 'item_name', 'client' => 'item_client_id', 'url' => '/agent/shares.php?{client_q}share={id}'], - 'Ticket' => ['icon' => 'fa-life-ring', 'table' => 'tickets', 'id' => 'ticket_id', 'name' => "CONCAT(ticket_prefix, ticket_number, ' - ', ticket_subject)", 'client' => 'ticket_client_id', 'url' => '/agent/ticket.php?{client_q}ticket_id={id}'], - 'Project' => ['icon' => 'fa-project-diagram', 'table' => 'projects', 'id' => 'project_id', 'name' => "CONCAT(project_prefix, project_number, ' - ', project_name)", 'client' => 'project_client_id', 'url' => '/agent/project.php?{client_q}project_id={id}'], - 'Quote' => ['icon' => 'fa-comment-dollar', 'table' => 'quotes', 'id' => 'quote_id', 'name' => "CONCAT(quote_prefix, quote_number)", 'client' => 'quote_client_id', 'url' => '/agent/quote.php?quote_id={id}'], - 'Invoice' => ['icon' => 'fa-file-invoice', 'table' => 'invoices', 'id' => 'invoice_id', 'name' => "CONCAT(invoice_prefix, invoice_number)", 'client' => 'invoice_client_id', 'url' => '/agent/invoice.php?invoice_id={id}'], - 'Recurring Invoice' => ['icon' => 'fa-redo-alt', 'table' => 'recurring_invoices', 'id' => 'recurring_invoice_id', 'name' => "CONCAT(recurring_invoice_prefix, recurring_invoice_number)", 'client' => 'recurring_invoice_client_id', 'url' => '/agent/recurring_invoice.php?recurring_invoice_id={id}'], - 'Product' => ['icon' => 'fa-box-open', 'table' => 'products', 'id' => 'product_id', 'name' => 'product_name', 'url' => '/agent/product.php?product_id={id}'], - 'Expense' => ['icon' => 'fa-shopping-cart', 'table' => 'expenses', 'id' => 'expense_id', 'name' => 'expense_description', 'client' => 'expense_client_id', 'url' => '/agent/expenses.php?open={id}'], - 'Recurring Expense' => ['icon' => 'fa-redo-alt', 'table' => 'recurring_expenses', 'id' => 'recurring_expense_id', 'name' => 'recurring_expense_description', 'client' => 'recurring_expense_client_id', 'url' => '/agent/recurring_expenses.php?open={id}'], - 'Trip' => ['icon' => 'fa-route', 'table' => 'trips', 'id' => 'trip_id', 'name' => 'trip_purpose', 'client' => 'trip_client_id', 'url' => '/agent/trips.php?open={id}'], + 'Client' => ['icon' => 'fa-user-friends', 'module' => 'module_client', 'table' => 'clients', 'id' => 'client_id', 'name' => 'client_name', 'url' => '/agent/client_overview.php?client_id={id}'], + 'Contact' => ['icon' => 'fa-address-card', 'module' => 'module_client', 'table' => 'contacts', 'id' => 'contact_id', 'name' => 'contact_name', 'client' => 'contact_client_id', 'url' => '/agent/contact.php?client_id={client}&contact_id={id}'], + 'Location' => ['icon' => 'fa-map-marker-alt', 'module' => 'module_client', 'table' => 'locations', 'id' => 'location_id', 'name' => 'location_name', 'client' => 'location_client_id', 'url' => '/agent/location.php?client_id={client}&location_id={id}'], + 'Vendor' => ['icon' => 'fa-building', 'module' => 'module_client', 'table' => 'vendors', 'id' => 'vendor_id', 'name' => 'vendor_name', 'client' => 'vendor_client_id', 'url' => '/agent/vendor.php?{client_q}vendor_id={id}'], + 'Asset' => ['icon' => 'fa-desktop', 'module' => 'module_support', 'table' => 'assets', 'id' => 'asset_id', 'name' => 'asset_name', 'client' => 'asset_client_id', 'url' => '/agent/asset.php?client_id={client}&asset_id={id}'], + 'Network' => ['icon' => 'fa-network-wired', 'module' => 'module_support', 'table' => 'networks', 'id' => 'network_id', 'name' => 'network_name', 'client' => 'network_client_id', 'url' => '/agent/network.php?client_id={client}&network_id={id}'], + 'Rack' => ['icon' => 'fa-server', 'module' => 'module_support', 'table' => 'racks', 'id' => 'rack_id', 'name' => 'rack_name', 'client' => 'rack_client_id', 'url' => '/agent/rack.php?{client_q}rack_id={id}'], + 'Credential' => ['icon' => 'fa-key', 'module' => 'module_credential', 'table' => 'credentials', 'id' => 'credential_id', 'name' => 'credential_name', 'client' => 'credential_client_id', 'url' => '/agent/credentials.php?{client_q}open={id}'], + 'Certificate' => ['icon' => 'fa-lock', 'module' => 'module_support', 'table' => 'certificates', 'id' => 'certificate_id', 'name' => 'certificate_name', 'client' => 'certificate_client_id', 'url' => '/agent/certificates.php?{client_q}open={id}'], + 'Domain' => ['icon' => 'fa-globe', 'module' => 'module_support', 'table' => 'domains', 'id' => 'domain_id', 'name' => 'domain_name', 'client' => 'domain_client_id', 'url' => '/agent/domains.php?{client_q}open={id}'], + 'Software' => ['icon' => 'fa-cube', 'module' => 'module_support', 'table' => 'software', 'id' => 'software_id', 'name' => 'software_name', 'client' => 'software_client_id', 'url' => '/agent/license.php?client_id={client}&software_id={id}'], + 'Service' => ['icon' => 'fa-stream', 'module' => 'module_support', 'table' => 'services', 'id' => 'service_id', 'name' => 'service_name', 'client' => 'service_client_id', 'url' => '/agent/service.php?client_id={client}&service_id={id}'], + 'Document' => ['icon' => 'fa-file-alt', 'module' => 'module_support', 'table' => 'documents', 'id' => 'document_id', 'name' => 'document_name', 'client' => 'document_client_id', 'url' => '/agent/document.php?client_id={client}&document_id={id}'], + 'File' => ['icon' => 'fa-file', 'module' => 'module_client', 'table' => 'files', 'id' => 'file_id', 'name' => 'file_name', 'client' => 'file_client_id', 'url' => '/agent/files.php?client_id={client}&open={id}'], + 'Share' => ['icon' => 'fa-share-alt', 'module' => 'module_support', 'table' => 'shared_items', 'id' => 'item_id', 'name' => 'item_name', 'client' => 'item_client_id', 'url' => '/agent/shares.php?{client_q}share={id}'], + 'Ticket' => ['icon' => 'fa-life-ring', 'module' => 'module_support', 'table' => 'tickets', 'id' => 'ticket_id', 'name' => "CONCAT(ticket_prefix, ticket_number, ' - ', ticket_subject)", 'client' => 'ticket_client_id', 'url' => '/agent/ticket.php?{client_q}ticket_id={id}'], + 'Project' => ['icon' => 'fa-project-diagram', 'module' => 'module_support', 'table' => 'projects', 'id' => 'project_id', 'name' => "CONCAT(project_prefix, project_number, ' - ', project_name)", 'client' => 'project_client_id', 'url' => '/agent/project.php?{client_q}project_id={id}'], + 'Quote' => ['icon' => 'fa-comment-dollar', 'module' => 'module_sales', 'table' => 'quotes', 'id' => 'quote_id', 'name' => "CONCAT(quote_prefix, quote_number)", 'client' => 'quote_client_id', 'url' => '/agent/quote.php?quote_id={id}'], + 'Invoice' => ['icon' => 'fa-file-invoice', 'module' => 'module_sales', 'table' => 'invoices', 'id' => 'invoice_id', 'name' => "CONCAT(invoice_prefix, invoice_number)", 'client' => 'invoice_client_id', 'url' => '/agent/invoice.php?invoice_id={id}'], + 'Recurring Invoice' => ['icon' => 'fa-redo-alt', 'module' => 'module_sales', 'table' => 'recurring_invoices', 'id' => 'recurring_invoice_id', 'name' => "CONCAT(recurring_invoice_prefix, recurring_invoice_number)", 'client' => 'recurring_invoice_client_id', 'url' => '/agent/recurring_invoice.php?recurring_invoice_id={id}'], + 'Product' => ['icon' => 'fa-box-open', 'module' => 'module_sales', 'table' => 'products', 'id' => 'product_id', 'name' => 'product_name', 'url' => '/agent/product.php?product_id={id}'], + 'Expense' => ['icon' => 'fa-shopping-cart', 'module' => 'module_financial', 'table' => 'expenses', 'id' => 'expense_id', 'name' => 'expense_description', 'client' => 'expense_client_id', 'url' => '/agent/expenses.php?open={id}'], + 'Recurring Expense' => ['icon' => 'fa-redo-alt', 'module' => 'module_financial', 'table' => 'recurring_expenses', 'id' => 'recurring_expense_id', 'name' => 'recurring_expense_description', 'client' => 'recurring_expense_client_id', 'url' => '/agent/recurring_expenses.php?open={id}'], + 'Trip' => ['icon' => 'fa-route', 'module' => 'module_financial', 'table' => 'trips', 'id' => 'trip_id', 'name' => 'trip_purpose', 'client' => 'trip_client_id', 'url' => '/agent/trips.php?open={id}'], 'Ticket Template' => ['icon' => 'fa-life-ring', 'table' => 'ticket_templates', 'id' => 'ticket_template_id', 'name' => 'ticket_template_name', 'url' => '/admin/ticket_template.php?ticket_template_id={id}'], 'Project Template' => ['icon' => 'fa-project-diagram', 'table' => 'project_templates', 'id' => 'project_template_id', 'name' => 'project_template_name', 'url' => '/admin/project_template.php?project_template_id={id}'], 'Document Template' => ['icon' => 'fa-file-alt', 'table' => 'document_templates', 'id' => 'document_template_id', 'name' => 'document_template_name', 'url' => '/admin/document_template.php?document_template_id={id}'], @@ -57,7 +62,7 @@ function auditLogKinds() { 'Login' => ['icon' => 'fa-sign-in-alt'], 'Login 2FA' => ['icon' => 'fa-shield-alt'], 'Logout' => ['icon' => 'fa-sign-out-alt'], - 'Client Login' => ['icon' => 'fa-door-open'], + 'Client Login' => ['icon' => 'fa-door-open', 'module' => 'module_client'], 'User' => ['icon' => 'fa-user'], 'User Account' => ['icon' => 'fa-user-cog'], 'User Role' => ['icon' => 'fa-user-shield'], @@ -68,28 +73,28 @@ function auditLogKinds() { 'Backup' => ['icon' => 'fa-cloud-upload-alt'], 'App' => ['icon' => 'fa-download'], 'Cron' => ['icon' => 'fa-clock'], - 'Calendar' => ['icon' => 'fa-calendar-alt'], - 'Calendar Event' => ['icon' => 'fa-calendar-day'], - 'Recurring Ticket' => ['icon' => 'fa-redo-alt'], - 'Task' => ['icon' => 'fa-tasks'], - 'Ticket Reply' => ['icon' => 'fa-reply'], - 'Folder' => ['icon' => 'fa-folder'], - 'Document Version' => ['icon' => 'fa-history'], - 'Asset Interface' => ['icon' => 'fa-ethernet'], - 'IP Address' => ['icon' => 'fa-sitemap'], - 'Payment' => ['icon' => 'fa-credit-card'], - 'Income' => ['icon' => 'fa-hand-holding-usd'], - 'Revenue' => ['icon' => 'fa-hand-holding-usd'], - 'Credit' => ['icon' => 'fa-wallet'], - 'Account' => ['icon' => 'fa-piggy-bank'], - 'Account Transfer' => ['icon' => 'fa-exchange-alt'], - 'Transaction' => ['icon' => 'fa-exchange-alt'], - 'Reconciliation' => ['icon' => 'fa-balance-scale'], - 'Budget' => ['icon' => 'fa-chart-pie'], + 'Calendar' => ['icon' => 'fa-calendar-alt', 'module' => 'module_client'], + 'Calendar Event' => ['icon' => 'fa-calendar-day', 'module' => 'module_client'], + 'Recurring Ticket' => ['icon' => 'fa-redo-alt', 'module' => 'module_support'], + 'Task' => ['icon' => 'fa-tasks', 'module' => 'module_support'], + 'Ticket Reply' => ['icon' => 'fa-reply', 'module' => 'module_support'], + 'Folder' => ['icon' => 'fa-folder', 'module' => 'module_client'], + 'Document Version' => ['icon' => 'fa-history', 'module' => 'module_support'], + 'Asset Interface' => ['icon' => 'fa-ethernet', 'module' => 'module_support'], + 'IP Address' => ['icon' => 'fa-sitemap', 'module' => 'module_support'], + 'Payment' => ['icon' => 'fa-credit-card', 'module' => 'module_financial'], + 'Income' => ['icon' => 'fa-hand-holding-usd', 'module' => 'module_financial'], + 'Revenue' => ['icon' => 'fa-hand-holding-usd', 'module' => 'module_financial'], + 'Credit' => ['icon' => 'fa-wallet', 'module' => 'module_sales'], + 'Account' => ['icon' => 'fa-piggy-bank', 'module' => 'module_financial'], + 'Account Transfer' => ['icon' => 'fa-exchange-alt', 'module' => 'module_financial'], + 'Transaction' => ['icon' => 'fa-exchange-alt', 'module' => 'module_financial'], + 'Reconciliation' => ['icon' => 'fa-balance-scale', 'module' => 'module_financial'], + 'Budget' => ['icon' => 'fa-chart-pie', 'module' => 'module_financial'], 'Tax' => ['icon' => 'fa-percent'], - 'Stripe' => ['icon' => 'fa-stripe-s'], + 'Stripe' => ['icon' => 'fa-stripe-s', 'module' => 'module_financial'], 'Payment Provider' => ['icon' => 'fa-credit-card'], - 'Payment Method' => ['icon' => 'fa-credit-card'], + 'Payment Method' => ['icon' => 'fa-credit-card', 'module' => 'module_financial'], 'Email' => ['icon' => 'fa-envelope'], 'Bulk Mail' => ['icon' => 'fa-mail-bulk'], 'Notification' => ['icon' => 'fa-bell'], @@ -107,7 +112,7 @@ function auditLogKinds() { 'AI Model' => ['icon' => 'fa-robot'], 'Starter Content' => ['icon' => 'fa-seedling'], 'Demo Data' => ['icon' => 'fa-flask'], - 'Report' => ['icon' => 'fa-chart-bar'], + 'Report' => ['icon' => 'fa-chart-bar', 'module' => 'module_reporting'], 'Sharing' => ['icon' => 'fa-share-alt'], 'Audit Log' => ['icon' => 'fa-history'], ]; @@ -341,3 +346,133 @@ function auditLogDevice($user_agent) { $device = implode(' · ', array_filter([$os !== '-' ? $os : '', $browser !== '-' ? $browser : ''])); return $device !== '' ? $device : mb_strimwidth(strtok($user_agent, ' '), 0, 40, '…'); } + +/* + * What of the audit log this agent may see, as SQL on logs: everything for + * an admin; otherwise the kinds of entry whose part of ITFlow their role can + * read (auditLogKinds() 'module') - not sign-ins, users, settings or the + * like - and never another agent turned away from a client. + */ +function auditLogRoleSql() { + global $session_is_admin; + if (!empty($session_is_admin)) { + return '1 = 1'; + } + $types = []; + foreach (auditLogKinds() as $type => $kind) { + if (!empty($kind['module']) && lookupUserPermission($kind['module']) >= 1) { + $types[] = "'" . escapeSql($type) . "'"; + } + } + return $types ? "log_type IN (" . implode(', ', $types) . ") AND log_action <> 'Denied'" : '1 = 0'; +} + +/* + * A record's history - its audit log entries, newest first, that this agent + * may see: ['type', 'id', 'client_id', 'rows' (up to $limit, with who did + * it), 'count' (all of them)]. + */ +function auditLogHistory($type, $id, $limit = 50) { + global $mysqli; + $type = (string) $type; + $id = intval($id); + $limit = max(1, intval($limit)); + $where = "log_type = '" . escapeSql($type) . "' AND log_entity_id = $id AND " . auditLogRoleSql(); + $history = ['type' => $type, 'id' => $id, 'client_id' => 0, 'rows' => [], 'count' => 0]; + if ($id <= 0) { + return $history; + } + $history['count'] = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM logs WHERE $where"))[0]); + $sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_ip, log_user_agent, log_created_at, log_client_id, log_user_id, log_entity_id, + user_name, user_avatar, user_type + FROM logs LEFT JOIN users ON user_id = log_user_id AND log_user_id > 0 + WHERE $where + ORDER BY log_id DESC LIMIT $limit"); + while ($row = mysqli_fetch_assoc($sql)) { + $history['rows'][] = $row; + $history['client_id'] = $history['client_id'] ?: intval($row['log_client_id']); + } + return $history; +} + +/* + * Where all of a record's history is: Admin > Audit Logs for an admin, the + * client's Audit Log for anyone else when the record is a client's, and + * nowhere ('') otherwise. + */ +function auditLogHistoryUrl(array $history, $client_id = 0) { + global $session_is_admin; + $query = 'type=' . rawurlencode($history['type']) . '&entity=' . intval($history['id']); + if (!empty($session_is_admin)) { + return '/admin/audit_logs.php?' . $query; + } + $client_id = intval($client_id) ?: intval($history['client_id']); + return $client_id ? "/agent/audit_log.php?client_id=$client_id&$query" : ''; +} + +/* + * A record's history as a list - each entry's action as a tile in its tone, + * what it says, who did it with their picture, when and, for an admin, from + * where - with how many more there are and a link to all of them. $opts: + * 'client_id' (the record's client, for the link), 'empty' (what to say when + * there is nothing), 'flush' (false for a list with its own border, in a + * quick look), 'compact' (no IP address, for a narrow card). + */ +function auditLogHistoryHtml(array $history, array $opts = []) { + global $session_is_admin; + if (!$history['rows']) { + return '
' . escapeHtml($opts['empty'] ?? 'Nothing logged yet.') . '
'; + } + $flush = $opts['flush'] ?? true; + $html = '
    '; + foreach ($history['rows'] as $row) { + $style = auditLogActionStyle($row['log_action']); + $who = auditLogWho($row); + $ip = trim((string) $row['log_ip']); + $html .= '
  • ' + . '' + . '
    ' + . '
    ' . escapeHtml(auditLogDescription($row)) . '
    ' + . '
    ' . $who['html'] + . '' . escapeHtml(timeAgo($row['log_created_at'])) . '' + . (!empty($session_is_admin) && empty($opts['compact']) && $ip !== '' ? '' . escapeHtml($ip) . '' : '') + . '
  • '; + } + $html .= '
'; + + $more = $history['count'] - count($history['rows']); + $url = auditLogHistoryUrl($history, $opts['client_id'] ?? 0); + if ($more > 0 || $url !== '') { + $html .= '
' + . ($more > 0 ? 'and ' . number_format($more) . ' more' : '') + . ($url !== '' ? '' . ($history['count'] > 1 ? 'All ' . number_format($history['count']) . ' in the audit log' : 'In the audit log') . '' : '') + . '
'; + } + return $html; +} + +/** A record's history as a card of its own, for a page without tabs - narrow, so compact. $opts as auditLogHistoryHtml(), and 'title'. */ +function auditLogHistoryCard(array $history, array $opts = []) { + $opts += ['compact' => true]; + return '
' + . '

' . escapeHtml($opts['title'] ?? 'History') + . ($history['count'] ? ' (' . number_format($history['count']) . ')' : '') . '

' + . auditLogHistoryHtml($history, $opts) + . '
'; +} + +/* + * A record's history folded away at the foot of its quick look - History + * with its count, opening to the list. Nothing when it has none. + */ +function auditLogHistorySection(array $history, array $opts = []) { + if (!$history['count']) { + return ''; + } + $id = 'auditHistory' . bin2hex(random_bytes(4)); + return '
' + . '' + . '
' . auditLogHistoryHtml($history, ['flush' => false] + $opts) . '
' + . '
'; +} diff --git a/functions/client_overview.php b/functions/client_overview.php index e1fec4a25..6fbbd2af5 100644 --- a/functions/client_overview.php +++ b/functions/client_overview.php @@ -526,16 +526,18 @@ function clientOverviewSharedItems($client_id) { } /** - * The latest changes for this client from the audit log. Views (opening a - * credential, a page) are left out - they would push every change off the - * card - and stay in the full audit log. + * The latest changes for this client from the audit log - the ones this + * agent's role may see (auditLogRoleSql()). Views (opening a credential, a + * page) are left out - they would push every change off the card - and stay + * in the client's Audit Log. */ function clientOverviewActivity($client_id, $limit = 6) { global $mysqli; + $client_id = intval($client_id); $sql = mysqli_query($mysqli, "SELECT log_created_at, log_description, user_name FROM logs LEFT JOIN users ON log_user_id = user_id - WHERE log_client_id = $client_id AND log_action != 'View' + WHERE log_client_id = $client_id AND log_action != 'View' AND " . auditLogRoleSql() . " ORDER BY log_id DESC LIMIT " . intval($limit)); diff --git a/functions/location.php b/functions/location.php index 00e4d577f..ebe537b70 100644 --- a/functions/location.php +++ b/functions/location.php @@ -275,19 +275,3 @@ function locationUnassign($location, $kind, $id) { } return $row[0]; } - -/** Who did what to a location - its audit log entries, newest first. */ -function locationHistory($location_id, $limit = 50) { - global $mysqli; - $location_id = intval($location_id); - $limit = max(1, intval($limit)); - $sql = mysqli_query($mysqli, "SELECT log_action, log_description, log_created_at, user_name FROM logs - LEFT JOIN users ON user_id = log_user_id - WHERE log_type = 'Location' AND log_entity_id = $location_id - ORDER BY log_id DESC LIMIT $limit"); - $rows = []; - while ($row = mysqli_fetch_assoc($sql)) { - $rows[] = $row; - } - return $rows; -} diff --git a/functions/products.php b/functions/products.php index 604d94ebc..a589da2c5 100644 --- a/functions/products.php +++ b/functions/products.php @@ -417,22 +417,6 @@ function productUsage($product_id) { return $usage; } -/** A product's history from the audit log, newest first. */ -function productHistory($product_id, $limit = 100) { - global $mysqli; - $product_id = intval($product_id); - $limit = max(1, intval($limit)); - $rows = []; - $sql = mysqli_query($mysqli, "SELECT log_action, log_description, log_created_at, user_name FROM logs - LEFT JOIN users ON user_id = log_user_id - WHERE log_type = 'Product' AND log_entity_id = $product_id - ORDER BY log_id DESC LIMIT $limit"); - while ($row = mysqli_fetch_assoc($sql)) { - $rows[] = $row; - } - return $rows; -} - /* ------------------------------------------------------------- Stock ledger */ /** Kinds of stock move => label, icon and tone. */ diff --git a/functions/service.php b/functions/service.php index 5563206a9..5dd691fae 100644 --- a/functions/service.php +++ b/functions/service.php @@ -586,22 +586,6 @@ function serviceUrls($service_id, $with_credentials) { return array_values($out); } -/** Its history from the audit log, newest first. */ -function serviceHistory($service_id, $limit = 50) { - global $mysqli; - $service_id = intval($service_id); - $limit = max(1, intval($limit)); - $sql = mysqli_query($mysqli, "SELECT log_action, log_description, log_created_at, user_name, user_avatar, user_id FROM logs - LEFT JOIN users ON user_id = log_user_id - WHERE log_type = 'Service' AND log_entity_id = $service_id - ORDER BY log_id DESC LIMIT $limit"); - $rows = []; - while ($row = mysqli_fetch_assoc($sql)) { - $rows[] = $row; - } - return $rows; -} - /** * The fields of the add and edit forms, checked, as a SET clause - or '' * with $error. Notes are plain text. $current is the row being edited. diff --git a/functions/software.php b/functions/software.php index 38141868a..6a7f5d760 100644 --- a/functions/software.php +++ b/functions/software.php @@ -521,22 +521,6 @@ function softwareAddKeys($software_id, $text) { return $added; } -/** Who did what to a license - its audit log entries, newest first. */ -function softwareHistory($software_id, $limit = 50) { - global $mysqli; - $software_id = intval($software_id); - $limit = max(1, intval($limit)); - $sql = mysqli_query($mysqli, "SELECT log_action, log_description, log_created_at, user_name, user_avatar, user_id FROM logs - LEFT JOIN users ON user_id = log_user_id - WHERE log_type = 'Software' AND log_entity_id = $software_id - ORDER BY log_id DESC LIMIT $limit"); - $rows = []; - while ($row = mysqli_fetch_assoc($sql)) { - $rows[] = $row; - } - return $rows; -} - /** Date from a form - Y-m-d or null. */ function softwareFormDate($value) { $value = trim((string) $value); diff --git a/functions/vendor.php b/functions/vendor.php index 70285ac2c..f8fd25331 100644 --- a/functions/vendor.php +++ b/functions/vendor.php @@ -417,19 +417,3 @@ function vendorSpend($vendor_id) { FROM expenses WHERE expense_vendor_id = $vendor_id AND expense_archived_at IS NULL")); return ['year' => floatval($row[0]), 'last_year' => floatval($row[1]), 'total' => floatval($row[2]), 'count' => intval($row[3]), 'last' => $row[4]]; } - -/** Who did what to a vendor - its audit log entries, newest first. */ -function vendorHistory($vendor_id, $limit = 50) { - global $mysqli; - $vendor_id = intval($vendor_id); - $limit = max(1, intval($limit)); - $sql = mysqli_query($mysqli, "SELECT log_action, log_description, log_created_at, user_name FROM logs - LEFT JOIN users ON user_id = log_user_id - WHERE log_type = 'Vendor' AND log_entity_id = $vendor_id - ORDER BY log_id DESC LIMIT $limit"); - $rows = []; - while ($row = mysqli_fetch_assoc($sql)) { - $rows[] = $row; - } - return $rows; -}