Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@
- [Add custom shipping carrier](/docs/{{version}}/add-custom-shipping-carrier)
- [How to add your own product-links providers](/docs/{{version}}/how-to-add-your-own-products-links-providers)

- ## Mage-OS modules
- [Overview](/docs/{{version}}/mage-os-modules)
- [Returns (RMA)](/docs/{{version}}/mage-os-module-rma)
- [Passkey authentication](/docs/{{version}}/mage-os-module-passkey-auth)
- [SEO](/docs/{{version}}/mage-os-module-seo)
- [AI engine optimization](/docs/{{version}}/mage-os-module-aeo)
- [FAQ](/docs/{{version}}/mage-os-module-faq)
- [Shopping feeds](/docs/{{version}}/mage-os-module-shopping-feed)
- [AI base and automatic translation](/docs/{{version}}/mage-os-module-ai-base-and-automatic-translation)
- [Admin activity log](/docs/{{version}}/mage-os-module-admin-activity-log)
- [Theme optimization](/docs/{{version}}/mage-os-module-theme-optimization)
- [Admin theme switcher](/docs/{{version}}/mage-os-module-theme-adminhtml-switcher)
- [Page Builder modules](/docs/{{version}}/mage-os-module-page-builder-widget-and-template-import-export)
- [Reservations grid and Extension Directory](/docs/{{version}}/mage-os-module-inventory-reservations-grid-and-extension-directory)
- [Bundled third-party modules](/docs/{{version}}/mage-os-bundled-third-party-modules)
- ## Community & Support
- [Get help and support](/docs/{{version}}/how-to-get-help-and-support)
- [Community Contribution](/docs/{{version}}/how-to-contribute-to-the-magento-2-community)
Expand Down
289 changes: 289 additions & 0 deletions mage-os-bundled-third-party-modules.md

Large diffs are not rendered by default.

269 changes: 269 additions & 0 deletions mage-os-module-admin-activity-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
---
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.
keywords: Mage-OS, MageOS_AdminActivityLog, admin activity log, audit log, login log, adminactivity.xml, admin_activity, clear log
---

# MageOS_AdminActivityLog (audit log)

[TOC]

`MageOS_AdminActivityLog` (package `mage-os/module-admin-activity-log`, version 2.0.4 in Mage-OS 4.0.0) writes an audit
trail of the Admin. It records who created, changed or deleted an entity such as a product, a CMS page or a system
setting, with the old and new value of each changed field, and it records every Admin sign-in, failed sign-in and
sign-out with IP address and browser. It is a fork of the KiwiCommerce Admin Activity extension. Both logs are read in
the Admin, and nothing is sent anywhere else.
[Core features](/docs/{{version}}/core-features) lists the other modules Mage-OS adds.

## What the module does and whether it is active

- After installation the module is enabled (`bin/magento module:status MageOS_AdminActivityLog`) and logging is on:
`admin/admin_activity/enable` is `1`, and so is `admin/admin_activity/login_activity`. Nothing needs to be
switched on.
- It logs only what happens in the Admin area through the browser. The observers are declared in `etc/adminhtml/`. A
product changed through the REST API (`PUT /V1/products/...`) wrote no row, and neither did requesting an Admin token.
Changes made with CLI commands aren't logged either.
- A change to an entity is logged when the controller action is listed in the module's `etc/adminactivity.xml` (30
entity groups: products, categories, customers, customer groups, catalog and cart price rules, CMS pages and blocks,
widgets, themes, system configuration, stores, attributes, attribute sets, reviews, Admin users and roles, orders,
taxes, URL rewrites, search terms, synonyms, sitemaps, checkout agreements, order statuses, integrations, email and
newsletter templates).
- Passwords, card data, tokens and similar fields are masked: the log shows `******` instead of the value.

## Configuration

**Stores > Configuration > Advanced > Admin > Admin Activity Logs** (section `admin`, group `admin_activity`, default
scope only). Defaults are from `etc/config.xml`, read back on a running store.

| Admin field | Path | Default |
|-------------|------|---------|
| Enable Admin Activity Logs | `admin/admin_activity/enable` | `1` |
| Record Login Activity | `admin/admin_activity/login_activity` | `1` |
| Record Page Visits | `admin/admin_activity/page_visit` | `0` |
| Clear Admin Activity Logs After (days) | `admin/admin_activity/clearlog` | `30` |

Notes:

- **Record Page Visits** writes a row with the action type `view` for every Admin page request, including AJAX
validation requests. With it on, opening one product edit page wrote the rows `Catalog/Product/Edit`,
`Catalog/Product/Validate` and `Catalog/Product/Edit`.
- **Clear Admin Activity Logs After** is the retention in days. With `0` or an empty value nothing is deleted. The
module's README says the default is 90 days. The code and the live store use 30.
- There is one more group of settings with no field in the Admin: `admin/admin_activity/module/<name>` for `order`,
`product`, `category`, `customer`, `promotion`, `email`, `page`, `block`, `widget`, `theme`, `system_config`,
`attribute`, `admin_user` and `seo`, all `1` by default. Each switches one entity group off at the default scope. They
have no `system.xml` entry, so `bin/magento config:set` refuses them:
`The "admin/admin_activity/module/product" path doesn't exist.` magerun2 writes them:

```shell
vendor/bin/n98-magerun2 config:store:set admin/admin_activity/module/product 0
bin/magento cache:flush
```

After that a product save was no longer logged, while other entities still were. Remove the override again with
`vendor/bin/n98-magerun2 config:store:delete admin/admin_activity/module/product --all`.

### Admin menu and ACL

| Menu item | Route | ACL resource |
|-----------|-------|--------------|
| System > Admin Activity > Activity Log | `adminactivity/activity` | `MageOS_AdminActivityLog::activity` |
| System > Admin Activity > Login Log | `adminactivity/login` | `MageOS_AdminActivityLog::login_activity` |

The parent resource is `MageOS_AdminActivityLog::activity_head`, `MageOS_AdminActivityLog::configuration` is listed as
"Settings", and `MageOS_AdminActivityLog::activityConfig` ("Admin Activity Section") protects the configuration section.

The Activity Log grid has the columns Date, Admin User, Activity Type, Full Action, Item, IP Address and a View action.
View opens the details: user, date, module, action, scope, store, the browser parsed from the user agent (name, version,
platform, device) and a table of field changes with the old and new values. The Login Log grid has Date, Type, Username,
Admin User, IP Address and Status.

## CLI commands, cron jobs and database tables

### CLI commands

None. The module has no console commands.

### Cron jobs

| Job | Class | Schedule | Purpose |
|-----|-------|----------|---------|
| `admin_activity_clearlog` | `MageOS\AdminActivityLog\Cron\ClearLog` | `0 3 * * *` | Deletes log rows older than the retention |

The job is in the `default` cron group and runs once a day at 03:00. Magento creates its `cron_schedule` rows only
shortly before the run time, so the job doesn't show in the table during the day. `ClearLog::execute()` does nothing
when the module is off or the retention is empty. Otherwise it deletes `admin_activity` rows with `created_at` at or
before the cut-off in batches of 1000, and `admin_login_log` rows too when login logging is on. The rows in
`admin_activity_log` and `admin_activity_detail` go with their activity through the foreign key.

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
remained, of 14 login rows 7 remained, and the field-change rows of the deleted activities were gone. See
[Cron jobs](/docs/{{version}}/cron-jobs).

### Database tables

| Table | Content |
|-------|---------|
| `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` |
| `admin_activity_log` | Field changes: `activity_id` (foreign key, cascade), `field_name`, `old_value`, `new_value` (values are cut at about 64 KB) |
| `admin_activity_detail` | The affected object: `activity_id`, `model_class`, `item_id` |
| `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) |

A failed sign-in wrote `type` `Login`, `status` `0` and the `remarks` "The account sign-in was incorrect or your account
is disabled temporarily. Please wait and try again later."

## How to extend it

The module dispatches no events of its own. It observes core events and plugs into three classes in the Admin area:

| Declared in | Subject | Class |
|-------------|---------|-------|
| `etc/adminhtml/events.xml` | `controller_action_postdispatch` | `Observer\PostDispatch` |
| `etc/adminhtml/events.xml` | `model_save_before`, `model_save_after`, `model_delete_after` | `Observer\SaveBefore`, `SaveAfter`, `DeleteAfter` |
| `etc/adminhtml/events.xml` | `backend_auth_user_login_success`, `backend_auth_user_login_failed` | `Observer\LoginSuccess`, `LoginFailed` |
| `etc/adminhtml/di.xml` | plugin on `Magento\Framework\App\Action\AbstractAction` | `Plugin\App\ActionPlugin` |
| `etc/adminhtml/di.xml` | plugin on `Magento\User\Model\ResourceModel\User` | `Plugin\User\DeletePlugin` |
| `etc/adminhtml/di.xml` | plugin on `Magento\Backend\Model\Auth` (logout) | `Plugin\AuthPlugin` |

The stable surface is the `@api` interfaces in `Api/`: `ActivityConfigInterface` (enabled state and retention),
`ActivityRepositoryInterface`, `LoginRepositoryInterface`, `FieldTrackerInterface`, `FieldCheckerInterface` and
`ModelResolverInterface`, with the data interfaces in `Api/Data/`. Each has a preference in `etc/di.xml`. There is no
web API route and no GraphQL schema.

### Track another entity

Three things are needed: an `etc/adminactivity.xml` in your module (merged with the module's own file, validated by
`etc/adminactivity.xsd`), a model entry in the allowlist of `ModelResolver`, and an existing `configpath` constant.

- The `ModelResolver` only creates models that are listed under its `modelFactories` argument. Without the entry,
`ModelResolver::getModel()` throws `Class "..." is not in the allowed model classes list`.
- `configpath constant` must be one of the 18 names in `ActivityConfig` (`MODULE_ORDER`, `MODULE_PRODUCT`,
`MODULE_CATEGORY`, `MODULE_CUSTOMER`, `MODULE_PROMOTION`, `MODULE_EMAIL`, `MODULE_PAGE`, `MODULE_BLOCK`,
`MODULE_WIDGET`, `MODULE_THEME`, `MODULE_SYSTEM_CONFIG`, `MODULE_ATTRIBUTE`, `MODULE_ADMIN_USER`, `MODULE_SEO`,
`ACTIVITY_ENABLE`, `LOGIN_ACTIVITY_ENABLE`, `PAGE_VISIT_ENABLE`, `CLEAR_LOG_DAYS`). The list is a private constant, so
a new switch can't be added. Choose the closest one.
- `controller_action` is the full action name of the Admin controller, lowercase with underscores, for example
`rma_reason_save` for `MageOS_RMA` route `rma`, controller `Reason`, action `Save`.

Example 1: log changes to the return reasons of [MageOS_RMA](/docs/{{version}}/mage-os-module-rma). This ran on Mage-OS
4.0.0 in a throwaway module `Docs_ActivityDemo`.

`etc/module.xml`:

```xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Docs_ActivityDemo">
<sequence>
<module name="MageOS_AdminActivityLog"/>
<module name="MageOS_RMA"/>
</sequence>
</module>
</config>
```

`etc/adminactivity.xml`:

```xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:MageOS_AdminActivityLog:etc/adminactivity.xsd">
<modules>
<module name="rma_reason">
<label translate="true">RMA Reason</label>
<models>
<class name="MageOS\RMA\Model\Reason"/>
</models>
<events>
<event controller_action="rma_reason_save" action_alias="save"/>
<event controller_action="rma_reason_delete" action_alias="delete"/>
</events>
<config>
<skip_fields>
<field>form_key</field>
</skip_fields>
<configpath constant="MODULE_ORDER"/>
<editurl url="{{module}}/{{controller}}/edit/entity_id/{{id}}"/>
<itemfield field="label"/>
</config>
</module>
</modules>
</config>
```

`etc/di.xml`:

```xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MageOS\AdminActivityLog\Model\ModelResolver">
<arguments>
<argument name="modelFactories" xsi:type="array">
<item name="MageOS\RMA\Model\Reason" xsi:type="object">MageOS\RMA\Model\ReasonFactory</item>
</argument>
</arguments>
</type>
<type name="MageOS\AdminActivityLog\Model\FieldChecker">
<arguments>
<argument name="protectedFields" xsi:type="array">
<item name="sort_order" xsi:type="string">sort_order</item>
</argument>
</arguments>
</type>
</config>
```

The second block in `di.xml` is example 2, a protected field. `FieldChecker` already protects 28 names (`password`,
`password_hash`, `rp_token`, the `cc_*` card fields, `api_key`, `api_secret`, `access_token`, `form_key` and others).
Your own names are added to that list.

The three XML files validate: `etc/adminactivity.xml` against the module's `adminactivity.xsd` and the other two against
the framework schemas. After `module:enable Docs_ActivityDemo`, `setup:upgrade` and deleting `generated/code` and
`generated/metadata` (compiled plugins, see
[Mage-OS for Magento developers](/docs/{{version}}/mage-os-for-magento-developers)), a change to a reason in **Sales >
RMA > RMA Reasons** (new label and sort order 15) wrote these rows:

```text
admin_activity: action_type=edit module="RMA Reason" fullaction=Rma/Reason/Save
item_name="Wrong Product Description (renamed)" item_url=rma/reason/edit/entity_id/1
admin_activity_log: label "Wrong Product Description" -> "Wrong Product Description (renamed)"
sort_order "******" -> "******"
admin_activity_detail: model_class=MageOS\RMA\Model\Reason item_id=1
```

A protected field is not left out. Its name stays in the log and both values are replaced with `******`.

## Luma and Hyvä support

Not applicable in the usual sense: the module has no storefront output. Everything is in the Admin area (layout handles
`adminactivity_activity_index` and `adminactivity_login_index`, the UI components `admin_activity_listing` and
`login_activity_listing`, and two templates for the grid and the detail view). Hyvä changes the storefront only, so
the module works the same with Luma and Hyvä stores. The grids and the configuration section loaded and worked on the
M137 Admin theme. They were not checked on the stock Magento Admin theme.

## How to turn it off or remove it

- **Switch logging off:** `bin/magento config:set admin/admin_activity/enable 0` and `cache:flush`. This also turns off
login logging and page-visit logging, and the cron job then deletes nothing.
- **Disable the module:** `bin/magento module:disable MageOS_AdminActivityLog`. No other Mage-OS module depends on it.
The Admin kept working (HTTP 200 on the login page).
- **Disabling and `setup:upgrade` drops the tables.** After `module:disable` and `setup:upgrade`, the four tables
`admin_activity`, `admin_activity_log`, `admin_activity_detail` and `admin_login_log` were gone, with the whole audit
trail. Enabling the module again created them empty. If you need the history, export the tables before you disable
the module.
- **Remove the package** with `composer remove mage-os/module-admin-activity-log` after disabling it.

## Known problems

- **Noisy field changes.** The save of a product with a new name and price logged 13 fields. Besides `name` and `price`
they included `qty` (`100500` to `100500`), `has_options`, `required_options`, `current_store_id`,
`options_container`, `skip_translation`, `drop_options`, `parent_id`, `msrp_display_actual_price_type`, and a
`description` whose HTML was reported as changed although it wasn't edited. Expect to filter when you read the log.
- **Direct URL of a detail page is unstyled.** `/adminactivity/activity/log/id/1` is the content of the modal that the
grid opens. Open it through the grid.
- **The README differs from the code:** the default retention is 30, not 90 days, and protected fields are masked, not
left out.
- **Only Admin requests are logged.** Changes through the REST API, GraphQL, CLI commands, cron jobs and imports are not
in the log.

Loading