From bcbe2e1f32fb2b7193e3ad0b884921b39f54f280 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Tue, 7 Jul 2015 18:08:42 +0200 Subject: [PATCH] Add a new log_event helper --- vroom.pl | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/vroom.pl b/vroom.pl index 6285d66..6beb9fc 100755 --- a/vroom.pl +++ b/vroom.pl @@ -143,6 +143,28 @@ helper check_db_version => sub { return ($ver eq Vroom::Constants::DB_VERSION) ? '1' : '0'; }; +# Log an event +helper log_event => sub { + my $self = shift; + my ($event) = @_; + if (!$event->{event} || + !$event->{msg}){ + $self->app->log->debug("Oops, invalid event received"); + return 0; + } + my $sth = eval { + $self->db->prepare('INSERT INTO `audit` (`date`,`event`,`from_ip`,`message`) + VALUES (CONVERT_TZ(NOW(), @@session.time_zone, \'+00:00\'),?,?,?)'); + }; + $sth->execute( + $event->{event}, + $self->tx->remote_address, + $event->{msg} + ); + $self->app->log->info('[' . $self->tx->remote_address . '] [' . $event->{event} . '] ' . $event->{msg}); + return 1; +}; + # Generate and manage rotation of session keys # used to sign cookies helper update_session_keys => sub { @@ -213,7 +235,10 @@ helper login => sub { id => $id, key => $key ); - $self->app->log->info($self->get_name . " logged in from " . $self->tx->remote_address); + $self->log_event({ + event => 'new_session', + msg => $self->get_name . " logged in from " . $self->tx->remote_address + }); return 1; };