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 {
ACCESSCHECK_VERSION => '1.2.0'
};
has 'db';
sub startup {
my $self = shift;
......@@ -61,13 +59,6 @@ sub startup {
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 $base_templates_dir = $self->home()->child('templates');
......@@ -90,7 +81,6 @@ sub startup {
},
);
my $routes = $self->routes();
$routes->get('/')->to(controller => 'home', action => 'run')->name('home');
......@@ -101,7 +91,6 @@ sub startup {
$routes->get('/step4')->to(controller => 'step4', action => 'run')->name('step4');
$routes->get('/step5')->to(controller => 'step5', action => 'run')->name('step5');
}
1;
......@@ -5,6 +5,7 @@ use Mojo::Base qw(Mojolicious::Controller);
use English qw(-no_match_vars);
use Syntax::Keyword::Try;
use AccessCheck::Data::DB;
use AccessCheck::Data::Entity;
use AccessCheck::Data::Token;
use AccessCheck::L10N;
......@@ -64,6 +65,16 @@ sub init_user {
return $user;
}
sub init_db {
my $self = shift;
my $db = AccessCheck::Data::DB->new();
$self->stash(db => $db);
return $db;
}
sub check_authentication {
my $self = shift;
......@@ -80,7 +91,7 @@ sub check_token {
my ($self, %args) = @_;
my $secret = $args{token};
my $db = $self->app()->db();
my $db = $self->stash('db');
my $token = AccessCheck::Data::Token->new(
db => $db,
......@@ -115,6 +126,7 @@ sub get_sp {
my ($self, %args) = @_;
my $entityid = $args{entityid};
my $db = $self->stash('db');
return $self->abort(
log_message => "Missing parameter: entityid",
......@@ -126,7 +138,6 @@ sub get_sp {
user_message => "invalid_entityid"
) if $entityid !~ $AccessCheck::Regexp::entityid;
my $db = $self->app()->db();
my $sp = AccessCheck::Data::Entity->new(
db => $db,
......@@ -147,8 +158,8 @@ sub abort {
my $status = $args{status} || 200;
my $format = $args{format} || 'html';
my $db = $self->stash('db');
my $db = $self->app()->db();
$db->rollback() if $db && $db->in_transaction();
$self->app()->log()->error($args{log_message}) if $args{log_message};
......
......@@ -13,16 +13,17 @@ sub run {
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_l10n();
$self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) {
return if !$self->check_authentication();
}
my $user = $self->stash('user');
my $db = $self->stash('db');
my $sps = AccessCheck::Data::Entity->get_entities(
db => $db,
......
......@@ -14,6 +14,7 @@ sub run {
$self->init_l10n();
$self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) {
return if !$self->check_authentication();
......
......@@ -19,10 +19,10 @@ sub run {
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_l10n();
$self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) {
return if !$self->check_authentication();
......@@ -32,6 +32,7 @@ sub run {
my $email = $self->param('email');
my $l10n = $self->stash('l10n');
my $user = $self->stash('user');
my $db = $self->stash('db');
my $sp = $self->get_sp(entityid => $entityid);
return if !$sp;
......
......@@ -19,10 +19,10 @@ sub run {
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_l10n();
$self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) {
return if !$self->check_authentication();
......@@ -34,6 +34,7 @@ sub run {
my $validity = $self->param('validity');
my $profiles = $self->every_param('profiles');
my $l10n = $self->stash('l10n');
my $db = $self->stash('db');
my $sp = $self->get_sp(entityid => $entityid);
return if !$sp;
......
......@@ -18,10 +18,10 @@ sub run {
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_l10n();
$self->init_user();
$self->init_db();
if ($config->{app}->{login_url}) {
return if !$self->check_authentication();
......@@ -30,6 +30,7 @@ sub run {
my $entityid = $self->param('entityid');
my $token = $self->param('token');
my $key = $self->param('key');
my $db = $self->stash('db');
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