nti / audit-bundle
Audit bundle for symfony3 and doctrine orm, logs any database change
Installs: 2 614
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 63
Type:symfony-bundle
Requires
- php: >=5.4.0
- doctrine/orm: ^2.4|^3.0
- symfony/framework-bundle: ^2.6|^3.0|^4.0|^4.4|^5.4|^6.4
Requires (Dev)
- php: >=5.6.0
- ext-pdo: *
- ext-pdo_sqlite: *
- data-dog/pager-bundle: ^0.2
- doctrine/doctrine-bundle: ^1.6|^2.0
- doctrine/doctrine-fixtures-bundle: ^2.2
- doctrine/orm: ^2.5|^3.0
- firehed/dbal-logger: 1.0.1
- knplabs/knp-menu-bundle: ^2.0
- knplabs/knp-time-bundle: ^1.3
- phpunit/phpunit: ~4.7.0|^5.1
- sensio/framework-extra-bundle: ^3.0.2|^5.1
- symfony/symfony: ^3.0|^4.0|^4.4|^5.4|^6.4
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.0.0
- dev-master / 0.1.x-dev
- v0.1.21
- v0.1.20
- 0.1.19
- 0.1.18
- 0.1.17
- 0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-audit-log-repository-performance-fix
- dev-audit-log-repo-sort-by-fix
- dev-adding-unaudited-fields-feature
- dev-audit-time-line-api
This package is auto-updated.
Last update: 2024-10-18 16:43:01 UTC
README
This bundle creates an audit log for all doctrine ORM database related changes:
- inserts ion changes, association and dissociation actions.
- if there is an user and updates including their diffs and relation field diffs.
- many to many relatin token storage, it will link him to the log.
- the audit entries are inserted within the same transaction during flush, if something fails the state remains clean.
Basically you can track any change from these log entries if they were managed through standard ORM operations.
NOTE: audit cannot track DQL or direct SQL updates or delete statement executions.
Pre-Install
To continue using Query Loggers methods within the audit log bundle, Symfony versions up to 6.x require the installation of firehed/dbal-logger v1.0.1. Make sure to have this installed before proceeding to the next step.
Install
First, install it with composer:
composer require nti/audit-bundle
Then, add it in your AppKernel bundles.
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
...
new DataDog\AuditBundle\DataDogAuditBundle(),
...
);
...
}
Using Query:
//SQL
CREATE TABLE audit_logs (id INT AUTO_INCREMENT NOT NULL, source_id INT NOT NULL, target_id INT DEFAULT NULL, blame_id INT DEFAULT NULL, action VARCHAR(12) NOT NULL, tbl VARCHAR(128) NOT NULL, diff LONGTEXT DEFAULT NULL COMMENT'(DC2Type:json_array)', logged_at DATETIME NOT NULL, UNIQUE INDEX UNIQ_D62F2858953C1C61 (source_id), UNIQUE INDEX UNIQ_D62F2858158E0B66 (target_id), UNIQUE INDEX UNIQ_D62F28588C082A2E (blame_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE UTF8_unicode_ci ENGINE = InnoDB;
CREATE TABLE audit_associations (id INT AUTO_INCREMENT NOT NULL, typ VARCHAR(128) NOT NULL, tbl VARCHAR(128) DEFAULT NULL, label VARCHAR(255) DEFAULT NULL, fk VARCHAR(255) NOT NULL, class VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE UTF8_unicode_ci ENGINE = InnoDB;
CREATE TABLE audit_request (id INT AUTO_INCREMENT NOT NULL, method VARCHAR(255) DEFAULT NULL, controller VARCHAR(255) DEFAULT NULL, route VARCHAR(255) DEFAULT NULL, route_params LONGTEXT DEFAULT NULL, ip VARCHAR(255) DEFAULT NULL, user_name VARCHAR(255) DEFAULT NULL,portal VARCHAR(255) DEFAULT NULL, query_data LONGTEXT DEFAULT NULL, data LONGTEXT DEFAULT NULL, created_on DATETIME DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE UTF8_unicode_ci ENGINE = InnoDB; ALTER TABLE audit_logs ADD CONSTRAINT FK_D62F2858953C1C61 FOREIGN KEY (source_id) REFERENCES audit_associations (id);
ALTER TABLE audit_logs ADD CONSTRAINT FK_D62F2858158E0B66 FOREIGN KEY (target_id) REFERENCES audit_associations (id);
ALTER TABLE audit_logs ADD CONSTRAINT FK_D62F28588C082A2E FOREIGN KEY (blame_id) REFERENCES audit_associations(id);
ALTER TABLE audit_associations ADD created_on DATETIME NOT NULL;
Using Doctrine Schema:
// Doctrine
php bin/console doctrine:schema:update -f
Config
Enable the audit request in the project:
// app/config.yml
# DataDog
data_dog_audit:
audit_request:
enabled: true
If you will use a custom database connection use following config:
// app/config.yml
# DataDog
data_dog_audit:
database:
connection_name: nti_logs
audit_request:
enabled: true
See How to Work with multiple Entity Managers and Connections
Annotations
Annotations has to use in the controller, add Annotations NTIAudit in the class:
//src/Bundle/Controller/Controller.php
use DataDog\AuditBundle\Annotations\NTIAudit;
/**
* Class Controller
* @package AppBundle\Controller
* @NTIAudit()
* @Route("/controller")
*/
class Controller extends Controller {
//TODO CODE HERE
}
Unaudited Entities
Sometimes, you might not want to create audit log entries for particular entities.
You can achieve this by listing those entities under the unaudited_entities
configuration
key in your config.yml
, for example:
data_dog_audit:
unaudited_entities:
- AppBundle\Entity\NoAuditForThis
Specify Audited Entities
Sometimes, it is also possible, that you want to create audit log entries only for particular entities. You can achieve it quite similar to unaudited entities. You can list them under the audited_entities
configuration key in your config.yml
, for example:
data_dog_audit:
audited_entities:
- AppBundle\Entity\AuditForThis
You can specify either audited or unaudited entities. If both are specified, only audited entities would be taken into account.
Command
For delete audit data:
php bin/console nti:audit:delete {qtyDays}
License
NTI