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

initialize DB access in app, not in controller

parent 82171eab
Branches
Tags
No related merge requests found
...@@ -6,13 +6,26 @@ use English qw(-no_match_vars); ...@@ -6,13 +6,26 @@ use English qw(-no_match_vars);
use Template::Constants qw(:chomp); use Template::Constants qw(:chomp);
use Syntax::Keyword::Try; use Syntax::Keyword::Try;
use AccessCheck::Data::DB;
use constant { use constant {
ACCESSCHECK_VERSION => '1.2.0' ACCESSCHECK_VERSION => '1.2.0'
}; };
has 'db';
sub startup { sub startup {
my $self = shift; my $self = shift;
$self->helper(
string_to_list => sub {
my $self = shift;
my $value = shift;
return defined $value ? split(/, */, $value) : ();
}
);
$self->plugin('INIConfig', { file => $ENV{ACCESS_CHECK_CONFIG} || 'conf/manager.conf' }); $self->plugin('INIConfig', { file => $ENV{ACCESS_CHECK_CONFIG} || 'conf/manager.conf' });
$self->plugin( $self->plugin(
...@@ -39,6 +52,22 @@ sub startup { ...@@ -39,6 +52,22 @@ sub startup {
) )
); );
AccessCheck::Data::DB->register_db(
driver => $config->{database}->{type},
database => $config->{database}->{name},
host => $config->{database}->{host},
password => $config->{database}->{password},
username => $config->{database}->{username},
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');
...@@ -72,14 +101,6 @@ sub startup { ...@@ -72,14 +101,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');
$self->helper(
string_to_list => sub {
my $self = shift;
my $value = shift;
return defined $value ? split(/, */, $value) : ();
}
);
} }
......
...@@ -5,7 +5,6 @@ use Mojo::Base qw(Mojolicious::Controller); ...@@ -5,7 +5,6 @@ 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;
...@@ -42,31 +41,6 @@ sub init_l10n { ...@@ -42,31 +41,6 @@ sub init_l10n {
return $l10n; return $l10n;
} }
sub init_db {
my $self = shift;
my $config = $self->app()->config();
AccessCheck::Data::DB->register_db(
driver => $config->{database}->{type},
database => $config->{database}->{name},
host => $config->{database}->{host},
password => $config->{database}->{password},
username => $config->{database}->{username},
options => [ $self->string_to_list($config->{database}->{options}) ]
);
my $db;
try {
$db = AccessCheck::Data::DB->new();
} catch {
}
$self->stash(db => $db);
return $db;
}
sub init_user { sub init_user {
my $self = shift; my $self = shift;
...@@ -106,7 +80,7 @@ sub check_token { ...@@ -106,7 +80,7 @@ sub check_token {
my ($self, %args) = @_; my ($self, %args) = @_;
my $secret = $args{token}; my $secret = $args{token};
my $db = $self->stash('db'); my $db = $self->app()->db();
my $token = AccessCheck::Data::Token->new( my $token = AccessCheck::Data::Token->new(
db => $db, db => $db,
...@@ -152,7 +126,7 @@ sub get_sp { ...@@ -152,7 +126,7 @@ sub get_sp {
user_message => "invalid_entityid" user_message => "invalid_entityid"
) if $entityid !~ $AccessCheck::Regexp::entityid; ) if $entityid !~ $AccessCheck::Regexp::entityid;
my $db = $self->stash('db'); my $db = $self->app()->db();
my $sp = AccessCheck::Data::Entity->new( my $sp = AccessCheck::Data::Entity->new(
db => $db, db => $db,
...@@ -174,7 +148,7 @@ sub abort { ...@@ -174,7 +148,7 @@ 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};
......
...@@ -31,7 +31,7 @@ Return the health status of the frontend. ...@@ -31,7 +31,7 @@ Return the health status of the frontend.
sub run { sub run {
my $self = shift; my $self = shift;
my $config = $self->app()->config(); my $config = $self->app()->config();
if (!$config->{status}) { if (!$config->{status}) {
$self->render( $self->render(
......
...@@ -10,10 +10,11 @@ use AccessCheck::Data::Entity; ...@@ -10,10 +10,11 @@ use AccessCheck::Data::Entity;
sub run { sub run {
my $self = shift; my $self = shift;
my $config = $self->app()->config(); my $app = $self->app();
my $log = $self->app()->log(); my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
...@@ -21,7 +22,6 @@ sub run { ...@@ -21,7 +22,6 @@ sub run {
return if !$self->check_authentication(); return if !$self->check_authentication();
} }
my $db = $self->stash('db');
my $user = $self->stash('user'); my $user = $self->stash('user');
my $sps = AccessCheck::Data::Entity->get_entities( my $sps = AccessCheck::Data::Entity->get_entities(
......
...@@ -8,10 +8,10 @@ use Syntax::Keyword::Try; ...@@ -8,10 +8,10 @@ use Syntax::Keyword::Try;
sub run { sub run {
my $self = shift; my $self = shift;
my $config = $self->app()->config(); my $app = $self->app();
my $log = $self->app()->log(); my $config = $app->config();
my $log = $app->log();
$self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
......
...@@ -16,10 +16,11 @@ use AccessCheck::Tools; ...@@ -16,10 +16,11 @@ use AccessCheck::Tools;
sub run { sub run {
my $self = shift; my $self = shift;
my $config = $self->app()->config(); my $app = $self->app();
my $log = $self->app()->log(); my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
...@@ -29,7 +30,6 @@ sub run { ...@@ -29,7 +30,6 @@ sub run {
my $entityid = $self->param('entityid'); my $entityid = $self->param('entityid');
my $email = $self->param('email'); my $email = $self->param('email');
my $db = $self->stash('db');
my $l10n = $self->stash('l10n'); my $l10n = $self->stash('l10n');
my $user = $self->stash('user'); my $user = $self->stash('user');
......
...@@ -16,10 +16,11 @@ use AccessCheck::Tools; ...@@ -16,10 +16,11 @@ use AccessCheck::Tools;
sub run { sub run {
my $self = shift; my $self = shift;
my $config = $self->app()->config(); my $app = $self->app();
my $log = $self->app()->log(); my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
...@@ -32,7 +33,6 @@ sub run { ...@@ -32,7 +33,6 @@ sub run {
my $token = $self->param('token'); my $token = $self->param('token');
my $validity = $self->param('validity'); my $validity = $self->param('validity');
my $profiles = $self->every_param('profiles'); my $profiles = $self->every_param('profiles');
my $db = $self->stash('db');
my $l10n = $self->stash('l10n'); my $l10n = $self->stash('l10n');
my $sp = $self->get_sp(entityid => $entityid); my $sp = $self->get_sp(entityid => $entityid);
......
...@@ -18,8 +18,8 @@ sub run { ...@@ -18,8 +18,8 @@ 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_db();
$self->init_l10n(); $self->init_l10n();
$self->init_user(); $self->init_user();
...@@ -30,7 +30,6 @@ sub run { ...@@ -30,7 +30,6 @@ 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