| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=3 sw=3 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::JobControl::Mojolicious::Plugin::Audit; | ||
| 8 | $Rex::JobControl::Mojolicious::Plugin::Audit::VERSION = '0.18.0'; | ||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | use Mojolicious::Plugin; | ||
| 13 | |||
| 14 | use base 'Mojolicious::Plugin'; | ||
| 15 | use Rex::JobControl::Helper::AuditLog; | ||
| 16 | use Data::Dumper; | ||
| 17 | |||
| 18 | sub register { | ||
| 19 | my ( $plugin, $app ) = @_; | ||
| 20 | |||
| 21 | my $log = Rex::JobControl::Helper::AuditLog->new( | ||
| 22 | path => $app->config->{log}->{audit_log}, | ||
| 23 | level => 'info' | ||
| 24 | ); | ||
| 25 | |||
| 26 | my %audit_calls = %{ $app->config->{audit} }; | ||
| 27 | |||
| 28 | $app->hook( | ||
| 29 | around_action => sub { | ||
| 30 | my ( $next, $c, $action, $last ) = @_; | ||
| 31 | |||
| 32 | my $ctrl_name = $c->stash('controller'); | ||
| 33 | my $action_name = $c->stash('action'); | ||
| 34 | |||
| 35 | if ( exists $audit_calls{$ctrl_name} | ||
| 36 | && exists $audit_calls{$ctrl_name}->{$action_name} ) | ||
| 37 | { | ||
| 38 | my %params; | ||
| 39 | @params{ @{ $audit_calls{$ctrl_name}->{$action_name}->{params} } } = | ||
| 40 | $c->param( $audit_calls{$ctrl_name}->{$action_name}->{params} ); | ||
| 41 | |||
| 42 | $log->audit( | ||
| 43 | { | ||
| 44 | controller => $ctrl_name, | ||
| 45 | action => $action_name, | ||
| 46 | data => \%params, | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | return $next->(); | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | 1; |