Skip to content
Snippets Groups Projects
Commit 7f52b582 authored by Guillaume ROUSSE's avatar Guillaume ROUSSE
Browse files

connect to the database at request time, instead of applicating launch time

parent 7e26fad8
No related branches found
No related tags found
No related merge requests found
...@@ -12,8 +12,6 @@ use constant { ...@@ -12,8 +12,6 @@ use constant {
ACCESSCHECK_VERSION => '1.2.0' ACCESSCHECK_VERSION => '1.2.0'
}; };
has 'db';
sub startup { sub startup {
my $self = shift; my $self = shift;
...@@ -61,13 +59,6 @@ sub startup { ...@@ -61,13 +59,6 @@ sub startup {
options => [ $self->string_to_list($config->{database}->{options}) ] options => [ $self->string_to_list($config->{database}->{options}) ]
); );
try {
$self->db(AccessCheck::Data::DB->new());
} catch ($error) {
$self->log()->fatal("unable to access database, aborting");
return;
}
my $theme = $config->{setup}->{templates_theme} || 'default'; my $theme = $config->{setup}->{templates_theme} || 'default';
my $base_templates_dir = $self->home()->child('templates'); my $base_templates_dir = $self->home()->child('templates');
...@@ -90,7 +81,6 @@ sub startup { ...@@ -90,7 +81,6 @@ sub startup {
}, },
); );
my $routes = $self->routes(); my $routes = $self->routes();
$routes->get('/')->to(controller => 'home', action => 'run')->name('home'); $routes->get('/')->to(controller => 'home', action => 'run')->name('home');
...@@ -101,7 +91,6 @@ sub startup { ...@@ -101,7 +91,6 @@ sub startup {
$routes->get('/step4')->to(controller => 'step4', action => 'run')->name('step4'); $routes->get('/step4')->to(controller => 'step4', action => 'run')->name('step4');
$routes->get('/step5')->to(controller => 'step5', action => 'run')->name('step5'); $routes->get('/step5')->to(controller => 'step5', action => 'run')->name('step5');
} }
1; 1;
...@@ -5,6 +5,7 @@ use Mojo::Base qw(Mojolicious::Controller); ...@@ -5,6 +5,7 @@ use Mojo::Base qw(Mojolicious::Controller);
use English qw(-no_match_vars); use English qw(-no_match_vars);
use Syntax::Keyword::Try; use Syntax::Keyword::Try;
use AccessCheck::Data::DB;
use AccessCheck::Data::Entity; use AccessCheck::Data::Entity;
use AccessCheck::Data::Token; use AccessCheck::Data::Token;
use AccessCheck::L10N; use AccessCheck::L10N;
...@@ -64,6 +65,16 @@ sub init_user { ...@@ -64,6 +65,16 @@ sub init_user {
return $user; return $user;
} }
sub init_db {
my $self = shift;
my $db = AccessCheck::Data::DB->new();
$self->stash(db => $db);
return $db;
}
sub check_authentication { sub check_authentication {
my $self = shift; my $self = shift;
...@@ -80,7 +91,7 @@ sub check_token { ...@@ -80,7 +91,7 @@ sub check_token {
my ($self, %args) = @_; my ($self, %args) = @_;
my $secret = $args{token}; my $secret = $args{token};
my $db = $self->app()->db(); my $db = $self->stash('db');
my $token = AccessCheck::Data::Token->new( my $token = AccessCheck::Data::Token->new(
db => $db, db => $db,
...@@ -115,6 +126,7 @@ sub get_sp { ...@@ -115,6 +126,7 @@ sub get_sp {
my ($self, %args) = @_; my ($self, %args) = @_;
my $entityid = $args{entityid}; my $entityid = $args{entityid};
my $db = $self->stash('db');
return $self->abort( return $self->abort(
log_message => "Missing parameter: entityid", log_message => "Missing parameter: entityid",
...@@ -126,7 +138,6 @@ sub get_sp { ...@@ -126,7 +138,6 @@ sub get_sp {
user_message => "invalid_entityid" user_message => "invalid_entityid"
) if $entityid !~ $AccessCheck::Regexp::entityid; ) if $entityid !~ $AccessCheck::Regexp::entityid;
my $db = $self->app()->db();
my $sp = AccessCheck::Data::Entity->new( my $sp = AccessCheck::Data::Entity->new(
db => $db, db => $db,
...@@ -147,8 +158,8 @@ sub abort { ...@@ -147,8 +158,8 @@ sub abort {
my $status = $args{status} || 200; my $status = $args{status} || 200;
my $format = $args{format} || 'html'; my $format = $args{format} || 'html';
my $db = $self->stash('db');
my $db = $self->app()->db();
$db->rollback() if $db && $db->in_transaction(); $db->rollback() if $db && $db->in_transaction();
$self->app()->log()->error($args{log_message}) if $args{log_message}; $self->app()->log()->error($args{log_message}) if $args{log_message};
......
...@@ -13,16 +13,17 @@ sub run { ...@@ -13,16 +13,17 @@ sub run {
my $app = $self->app(); my $app = $self->app();
my $config = $app->config(); my $config = $app->config();
my $log = $app->log(); my $log = $app->log();
my $db = $app->db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
} }
my $user = $self->stash('user'); my $user = $self->stash('user');
my $db = $self->stash('db');
my $sps = AccessCheck::Data::Entity->get_entities( my $sps = AccessCheck::Data::Entity->get_entities(
db => $db, db => $db,
......
...@@ -14,6 +14,7 @@ sub run { ...@@ -14,6 +14,7 @@ sub run {
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
......
...@@ -19,10 +19,10 @@ sub run { ...@@ -19,10 +19,10 @@ sub run {
my $app = $self->app(); my $app = $self->app();
my $config = $app->config(); my $config = $app->config();
my $log = $app->log(); my $log = $app->log();
my $db = $app->db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
...@@ -32,6 +32,7 @@ sub run { ...@@ -32,6 +32,7 @@ sub run {
my $email = $self->param('email'); my $email = $self->param('email');
my $l10n = $self->stash('l10n'); my $l10n = $self->stash('l10n');
my $user = $self->stash('user'); my $user = $self->stash('user');
my $db = $self->stash('db');
my $sp = $self->get_sp(entityid => $entityid); my $sp = $self->get_sp(entityid => $entityid);
return if !$sp; return if !$sp;
......
...@@ -19,10 +19,10 @@ sub run { ...@@ -19,10 +19,10 @@ sub run {
my $app = $self->app(); my $app = $self->app();
my $config = $app->config(); my $config = $app->config();
my $log = $app->log(); my $log = $app->log();
my $db = $app->db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
...@@ -34,6 +34,7 @@ sub run { ...@@ -34,6 +34,7 @@ sub run {
my $validity = $self->param('validity'); my $validity = $self->param('validity');
my $profiles = $self->every_param('profiles'); my $profiles = $self->every_param('profiles');
my $l10n = $self->stash('l10n'); my $l10n = $self->stash('l10n');
my $db = $self->stash('db');
my $sp = $self->get_sp(entityid => $entityid); my $sp = $self->get_sp(entityid => $entityid);
return if !$sp; return if !$sp;
......
...@@ -18,10 +18,10 @@ sub run { ...@@ -18,10 +18,10 @@ sub run {
my $app = $self->app(); my $app = $self->app();
my $config = $app->config(); my $config = $app->config();
my $log = $app->log(); my $log = $app->log();
my $db = $app->db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) { if ($config->{app}->{login_url}) {
return if !$self->check_authentication(); return if !$self->check_authentication();
...@@ -30,6 +30,7 @@ sub run { ...@@ -30,6 +30,7 @@ sub run {
my $entityid = $self->param('entityid'); my $entityid = $self->param('entityid');
my $token = $self->param('token'); my $token = $self->param('token');
my $key = $self->param('key'); my $key = $self->param('key');
my $db = $self->stash('db');
return if !$self->check_token(token => $token, entityid => $entityid); return if !$self->check_token(token => $token, entityid => $entityid);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment