|
| 1 | +--- |
| 2 | +description: MageOS_AdminActivityLog in Mage-OS 4.0.0 records what Admin users change and when they sign in, with settings, cron cleanup, tables and adminactivity.xml. |
| 3 | +keywords: Mage-OS, MageOS_AdminActivityLog, admin activity log, audit log, login log, adminactivity.xml, admin_activity, clear log |
| 4 | +--- |
| 5 | + |
| 6 | +# MageOS_AdminActivityLog (audit log) |
| 7 | + |
| 8 | +[TOC] |
| 9 | + |
| 10 | +`MageOS_AdminActivityLog` (package `mage-os/module-admin-activity-log`, version 2.0.4 in Mage-OS 4.0.0) writes an audit |
| 11 | +trail of the Admin. It records who created, changed or deleted an entity such as a product, a CMS page or a system |
| 12 | +setting, with the old and new value of each changed field, and it records every Admin sign-in, failed sign-in and |
| 13 | +sign-out with IP address and browser. It is a fork of the KiwiCommerce Admin Activity extension. Both logs are read in |
| 14 | +the Admin, and nothing is sent anywhere else. |
| 15 | +[Core features](/docs/{{version}}/core-features) lists the other modules Mage-OS adds. |
| 16 | + |
| 17 | +## What the module does and whether it is active |
| 18 | + |
| 19 | +- After installation the module is enabled (`bin/magento module:status MageOS_AdminActivityLog`) and logging is on: |
| 20 | + `admin/admin_activity/enable` is `1`, and so is `admin/admin_activity/login_activity`. Nothing needs to be |
| 21 | + switched on. |
| 22 | +- It logs only what happens in the Admin area through the browser. The observers are declared in `etc/adminhtml/`. A |
| 23 | + product changed through the REST API (`PUT /V1/products/...`) wrote no row, and neither did requesting an Admin token. |
| 24 | + Changes made with CLI commands aren't logged either. |
| 25 | +- A change to an entity is logged when the controller action is listed in the module's `etc/adminactivity.xml` (30 |
| 26 | + entity groups: products, categories, customers, customer groups, catalog and cart price rules, CMS pages and blocks, |
| 27 | + widgets, themes, system configuration, stores, attributes, attribute sets, reviews, Admin users and roles, orders, |
| 28 | + taxes, URL rewrites, search terms, synonyms, sitemaps, checkout agreements, order statuses, integrations, email and |
| 29 | + newsletter templates). |
| 30 | +- Passwords, card data, tokens and similar fields are masked: the log shows `******` instead of the value. |
| 31 | + |
| 32 | +## Configuration |
| 33 | + |
| 34 | +**Stores > Configuration > Advanced > Admin > Admin Activity Logs** (section `admin`, group `admin_activity`, default |
| 35 | +scope only). Defaults are from `etc/config.xml`, read back on a running store. |
| 36 | + |
| 37 | +| Admin field | Path | Default | |
| 38 | +|-------------|------|---------| |
| 39 | +| Enable Admin Activity Logs | `admin/admin_activity/enable` | `1` | |
| 40 | +| Record Login Activity | `admin/admin_activity/login_activity` | `1` | |
| 41 | +| Record Page Visits | `admin/admin_activity/page_visit` | `0` | |
| 42 | +| Clear Admin Activity Logs After (days) | `admin/admin_activity/clearlog` | `30` | |
| 43 | + |
| 44 | +Notes: |
| 45 | + |
| 46 | +- **Record Page Visits** writes a row with the action type `view` for every Admin page request, including AJAX |
| 47 | + validation requests. With it on, opening one product edit page wrote the rows `Catalog/Product/Edit`, |
| 48 | + `Catalog/Product/Validate` and `Catalog/Product/Edit`. |
| 49 | +- **Clear Admin Activity Logs After** is the retention in days. With `0` or an empty value nothing is deleted. The |
| 50 | + module's README says the default is 90 days. The code and the live store use 30. |
| 51 | +- There is one more group of settings with no field in the Admin: `admin/admin_activity/module/<name>` for `order`, |
| 52 | + `product`, `category`, `customer`, `promotion`, `email`, `page`, `block`, `widget`, `theme`, `system_config`, |
| 53 | + `attribute`, `admin_user` and `seo`, all `1` by default. Each switches one entity group off at the default scope. They |
| 54 | + have no `system.xml` entry, so `bin/magento config:set` refuses them: |
| 55 | + `The "admin/admin_activity/module/product" path doesn't exist.` magerun2 writes them: |
| 56 | + |
| 57 | + ```shell |
| 58 | + vendor/bin/n98-magerun2 config:store:set admin/admin_activity/module/product 0 |
| 59 | + bin/magento cache:flush |
| 60 | + ``` |
| 61 | + |
| 62 | + After that a product save was no longer logged, while other entities still were. Remove the override again with |
| 63 | + `vendor/bin/n98-magerun2 config:store:delete admin/admin_activity/module/product --all`. |
| 64 | + |
| 65 | +### Admin menu and ACL |
| 66 | + |
| 67 | +| Menu item | Route | ACL resource | |
| 68 | +|-----------|-------|--------------| |
| 69 | +| System > Admin Activity > Activity Log | `adminactivity/activity` | `MageOS_AdminActivityLog::activity` | |
| 70 | +| System > Admin Activity > Login Log | `adminactivity/login` | `MageOS_AdminActivityLog::login_activity` | |
| 71 | + |
| 72 | +The parent resource is `MageOS_AdminActivityLog::activity_head`, `MageOS_AdminActivityLog::configuration` is listed as |
| 73 | +"Settings", and `MageOS_AdminActivityLog::activityConfig` ("Admin Activity Section") protects the configuration section. |
| 74 | + |
| 75 | +The Activity Log grid has the columns Date, Admin User, Activity Type, Full Action, Item, IP Address and a View action. |
| 76 | +View opens the details: user, date, module, action, scope, store, the browser parsed from the user agent (name, version, |
| 77 | +platform, device) and a table of field changes with the old and new values. The Login Log grid has Date, Type, Username, |
| 78 | +Admin User, IP Address and Status. |
| 79 | + |
| 80 | +## CLI commands, cron jobs and database tables |
| 81 | + |
| 82 | +### CLI commands |
| 83 | + |
| 84 | +None. The module has no console commands. |
| 85 | + |
| 86 | +### Cron jobs |
| 87 | + |
| 88 | +| Job | Class | Schedule | Purpose | |
| 89 | +|-----|-------|----------|---------| |
| 90 | +| `admin_activity_clearlog` | `MageOS\AdminActivityLog\Cron\ClearLog` | `0 3 * * *` | Deletes log rows older than the retention | |
| 91 | + |
| 92 | +The job is in the `default` cron group and runs once a day at 03:00. Magento creates its `cron_schedule` rows only |
| 93 | +shortly before the run time, so the job doesn't show in the table during the day. `ClearLog::execute()` does nothing |
| 94 | +when the module is off or the retention is empty. Otherwise it deletes `admin_activity` rows with `created_at` at or |
| 95 | +before the cut-off in batches of 1000, and `admin_login_log` rows too when login logging is on. The rows in |
| 96 | +`admin_activity_log` and `admin_activity_detail` go with their activity through the foreign key. |
| 97 | + |
| 98 | +The class was run on the test store after back-dating rows (45 days) with a retention of 30 days. Of 6 activity rows 3 |
| 99 | +remained, of 14 login rows 7 remained, and the field-change rows of the deleted activities were gone. See |
| 100 | +[Cron jobs](/docs/{{version}}/cron-jobs). |
| 101 | + |
| 102 | +### Database tables |
| 103 | + |
| 104 | +| Table | Content | |
| 105 | +|-------|---------| |
| 106 | +| `admin_activity` | One row per logged action: `username`, `name`, `admin_id`, `store_id`, `scope`, `action_type`, `remote_ip`, `forwarded_ip`, `user_agent`, `module` (the entity label), `fullaction`, `item_name`, `item_url`, `item_path`, `created_at` | |
| 107 | +| `admin_activity_log` | Field changes: `activity_id` (foreign key, cascade), `field_name`, `old_value`, `new_value` (values are cut at about 64 KB) | |
| 108 | +| `admin_activity_detail` | The affected object: `activity_id`, `model_class`, `item_id` | |
| 109 | +| `admin_login_log` | Sign-ins: `admin_id`, `username`, `name`, `remote_ip`, `forwarded_ip`, `user_agent`, `type` (`Login` or `Logout`), `status` (`1` success, `0` failure), `remarks` (the failure reason) | |
| 110 | + |
| 111 | +A failed sign-in wrote `type` `Login`, `status` `0` and the `remarks` "The account sign-in was incorrect or your account |
| 112 | +is disabled temporarily. Please wait and try again later." |
| 113 | + |
| 114 | +## How to extend it |
| 115 | + |
| 116 | +The module dispatches no events of its own. It observes core events and plugs into three classes in the Admin area: |
| 117 | + |
| 118 | +| Declared in | Subject | Class | |
| 119 | +|-------------|---------|-------| |
| 120 | +| `etc/adminhtml/events.xml` | `controller_action_postdispatch` | `Observer\PostDispatch` | |
| 121 | +| `etc/adminhtml/events.xml` | `model_save_before`, `model_save_after`, `model_delete_after` | `Observer\SaveBefore`, `SaveAfter`, `DeleteAfter` | |
| 122 | +| `etc/adminhtml/events.xml` | `backend_auth_user_login_success`, `backend_auth_user_login_failed` | `Observer\LoginSuccess`, `LoginFailed` | |
| 123 | +| `etc/adminhtml/di.xml` | plugin on `Magento\Framework\App\Action\AbstractAction` | `Plugin\App\ActionPlugin` | |
| 124 | +| `etc/adminhtml/di.xml` | plugin on `Magento\User\Model\ResourceModel\User` | `Plugin\User\DeletePlugin` | |
| 125 | +| `etc/adminhtml/di.xml` | plugin on `Magento\Backend\Model\Auth` (logout) | `Plugin\AuthPlugin` | |
| 126 | + |
| 127 | +The stable surface is the `@api` interfaces in `Api/`: `ActivityConfigInterface` (enabled state and retention), |
| 128 | +`ActivityRepositoryInterface`, `LoginRepositoryInterface`, `FieldTrackerInterface`, `FieldCheckerInterface` and |
| 129 | +`ModelResolverInterface`, with the data interfaces in `Api/Data/`. Each has a preference in `etc/di.xml`. There is no |
| 130 | +web API route and no GraphQL schema. |
| 131 | + |
| 132 | +### Track another entity |
| 133 | + |
| 134 | +Three things are needed: an `etc/adminactivity.xml` in your module (merged with the module's own file, validated by |
| 135 | +`etc/adminactivity.xsd`), a model entry in the allowlist of `ModelResolver`, and an existing `configpath` constant. |
| 136 | + |
| 137 | +- The `ModelResolver` only creates models that are listed under its `modelFactories` argument. Without the entry, |
| 138 | + `ModelResolver::getModel()` throws `Class "..." is not in the allowed model classes list`. |
| 139 | +- `configpath constant` must be one of the 18 names in `ActivityConfig` (`MODULE_ORDER`, `MODULE_PRODUCT`, |
| 140 | + `MODULE_CATEGORY`, `MODULE_CUSTOMER`, `MODULE_PROMOTION`, `MODULE_EMAIL`, `MODULE_PAGE`, `MODULE_BLOCK`, |
| 141 | + `MODULE_WIDGET`, `MODULE_THEME`, `MODULE_SYSTEM_CONFIG`, `MODULE_ATTRIBUTE`, `MODULE_ADMIN_USER`, `MODULE_SEO`, |
| 142 | + `ACTIVITY_ENABLE`, `LOGIN_ACTIVITY_ENABLE`, `PAGE_VISIT_ENABLE`, `CLEAR_LOG_DAYS`). The list is a private constant, so |
| 143 | + a new switch can't be added. Choose the closest one. |
| 144 | +- `controller_action` is the full action name of the Admin controller, lowercase with underscores, for example |
| 145 | + `rma_reason_save` for `MageOS_RMA` route `rma`, controller `Reason`, action `Save`. |
| 146 | + |
| 147 | +Example 1: log changes to the return reasons of [MageOS_RMA](/docs/{{version}}/mage-os-module-rma). This ran on Mage-OS |
| 148 | +4.0.0 in a throwaway module `Docs_ActivityDemo`. |
| 149 | + |
| 150 | +`etc/module.xml`: |
| 151 | + |
| 152 | +```xml |
| 153 | +<?xml version="1.0"?> |
| 154 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 155 | + xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> |
| 156 | + <module name="Docs_ActivityDemo"> |
| 157 | + <sequence> |
| 158 | + <module name="MageOS_AdminActivityLog"/> |
| 159 | + <module name="MageOS_RMA"/> |
| 160 | + </sequence> |
| 161 | + </module> |
| 162 | +</config> |
| 163 | +``` |
| 164 | + |
| 165 | +`etc/adminactivity.xml`: |
| 166 | + |
| 167 | +```xml |
| 168 | +<?xml version="1.0"?> |
| 169 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 170 | + xsi:noNamespaceSchemaLocation="urn:magento:module:MageOS_AdminActivityLog:etc/adminactivity.xsd"> |
| 171 | + <modules> |
| 172 | + <module name="rma_reason"> |
| 173 | + <label translate="true">RMA Reason</label> |
| 174 | + <models> |
| 175 | + <class name="MageOS\RMA\Model\Reason"/> |
| 176 | + </models> |
| 177 | + <events> |
| 178 | + <event controller_action="rma_reason_save" action_alias="save"/> |
| 179 | + <event controller_action="rma_reason_delete" action_alias="delete"/> |
| 180 | + </events> |
| 181 | + <config> |
| 182 | + <skip_fields> |
| 183 | + <field>form_key</field> |
| 184 | + </skip_fields> |
| 185 | + <configpath constant="MODULE_ORDER"/> |
| 186 | + <editurl url="{{module}}/{{controller}}/edit/entity_id/{{id}}"/> |
| 187 | + <itemfield field="label"/> |
| 188 | + </config> |
| 189 | + </module> |
| 190 | + </modules> |
| 191 | +</config> |
| 192 | +``` |
| 193 | + |
| 194 | +`etc/di.xml`: |
| 195 | + |
| 196 | +```xml |
| 197 | +<?xml version="1.0"?> |
| 198 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 199 | + xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> |
| 200 | + <type name="MageOS\AdminActivityLog\Model\ModelResolver"> |
| 201 | + <arguments> |
| 202 | + <argument name="modelFactories" xsi:type="array"> |
| 203 | + <item name="MageOS\RMA\Model\Reason" xsi:type="object">MageOS\RMA\Model\ReasonFactory</item> |
| 204 | + </argument> |
| 205 | + </arguments> |
| 206 | + </type> |
| 207 | + <type name="MageOS\AdminActivityLog\Model\FieldChecker"> |
| 208 | + <arguments> |
| 209 | + <argument name="protectedFields" xsi:type="array"> |
| 210 | + <item name="sort_order" xsi:type="string">sort_order</item> |
| 211 | + </argument> |
| 212 | + </arguments> |
| 213 | + </type> |
| 214 | +</config> |
| 215 | +``` |
| 216 | + |
| 217 | +The second block in `di.xml` is example 2, a protected field. `FieldChecker` already protects 28 names (`password`, |
| 218 | +`password_hash`, `rp_token`, the `cc_*` card fields, `api_key`, `api_secret`, `access_token`, `form_key` and others). |
| 219 | +Your own names are added to that list. |
| 220 | + |
| 221 | +The three XML files validate: `etc/adminactivity.xml` against the module's `adminactivity.xsd` and the other two against |
| 222 | +the framework schemas. After `module:enable Docs_ActivityDemo`, `setup:upgrade` and deleting `generated/code` and |
| 223 | +`generated/metadata` (compiled plugins, see |
| 224 | +[Mage-OS for Magento developers](/docs/{{version}}/mage-os-for-magento-developers)), a change to a reason in **Sales > |
| 225 | +RMA > RMA Reasons** (new label and sort order 15) wrote these rows: |
| 226 | + |
| 227 | +```text |
| 228 | +admin_activity: action_type=edit module="RMA Reason" fullaction=Rma/Reason/Save |
| 229 | + item_name="Wrong Product Description (renamed)" item_url=rma/reason/edit/entity_id/1 |
| 230 | +admin_activity_log: label "Wrong Product Description" -> "Wrong Product Description (renamed)" |
| 231 | + sort_order "******" -> "******" |
| 232 | +admin_activity_detail: model_class=MageOS\RMA\Model\Reason item_id=1 |
| 233 | +``` |
| 234 | + |
| 235 | +A protected field is not left out. Its name stays in the log and both values are replaced with `******`. |
| 236 | + |
| 237 | +## Luma and Hyvä support |
| 238 | + |
| 239 | +Not applicable in the usual sense: the module has no storefront output. Everything is in the Admin area (layout handles |
| 240 | +`adminactivity_activity_index` and `adminactivity_login_index`, the UI components `admin_activity_listing` and |
| 241 | +`login_activity_listing`, and two templates for the grid and the detail view). Hyvä changes the storefront only, so |
| 242 | +the module works the same with Luma and Hyvä stores. The grids and the configuration section loaded and worked on the |
| 243 | +M137 Admin theme. They were not checked on the stock Magento Admin theme. |
| 244 | + |
| 245 | +## How to turn it off or remove it |
| 246 | + |
| 247 | +- **Switch logging off:** `bin/magento config:set admin/admin_activity/enable 0` and `cache:flush`. This also turns off |
| 248 | + login logging and page-visit logging, and the cron job then deletes nothing. |
| 249 | +- **Disable the module:** `bin/magento module:disable MageOS_AdminActivityLog`. No other Mage-OS module depends on it. |
| 250 | + The Admin kept working (HTTP 200 on the login page). |
| 251 | +- **Disabling and `setup:upgrade` drops the tables.** After `module:disable` and `setup:upgrade`, the four tables |
| 252 | + `admin_activity`, `admin_activity_log`, `admin_activity_detail` and `admin_login_log` were gone, with the whole audit |
| 253 | + trail. Enabling the module again created them empty. If you need the history, export the tables before you disable |
| 254 | + the module. |
| 255 | +- **Remove the package** with `composer remove mage-os/module-admin-activity-log` after disabling it. |
| 256 | + |
| 257 | +## Known problems |
| 258 | + |
| 259 | +- **Noisy field changes.** The save of a product with a new name and price logged 13 fields. Besides `name` and `price` |
| 260 | + they included `qty` (`100500` to `100500`), `has_options`, `required_options`, `current_store_id`, |
| 261 | + `options_container`, `skip_translation`, `drop_options`, `parent_id`, `msrp_display_actual_price_type`, and a |
| 262 | + `description` whose HTML was reported as changed although it wasn't edited. Expect to filter when you read the log. |
| 263 | +- **Direct URL of a detail page is unstyled.** `/adminactivity/activity/log/id/1` is the content of the modal that the |
| 264 | + grid opens. Open it through the grid. |
| 265 | +- **The README differs from the code:** the default retention is 30, not 90 days, and protected fields are masked, not |
| 266 | + left out. |
| 267 | +- **Only Admin requests are logged.** Changes through the REST API, GraphQL, CLI commands, cron jobs and imports are not |
| 268 | + in the log. |
| 269 | + |
0 commit comments