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
No related branches found
No related tags found
No related merge requests found
......@@ -6,13 +6,26 @@ use English qw(-no_match_vars);
use Template::Constants qw(:chomp);
use Syntax::Keyword::Try;
use AccessCheck::Data::DB;
use constant {
ACCESSCHECK_VERSION => '1.2.0'
};
has 'db';
sub startup {
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(
......@@ -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 $base_templates_dir = $self->home()->child('templates');
......@@ -72,14 +101,6 @@ sub startup {
$routes->get('/step4')->to(controller => 'step4', action => 'run')->name('step4');
$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);
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;
......@@ -42,31 +41,6 @@ sub init_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 {
my $self = shift;
......@@ -106,7 +80,7 @@ sub check_token {
my ($self, %args) = @_;
my $secret = $args{token};
my $db = $self->stash('db');
my $db = $self->app()->db();
my $token = AccessCheck::Data::Token->new(
db => $db,
......@@ -152,7 +126,7 @@ sub get_sp {
user_message => "invalid_entityid"
) if $entityid !~ $AccessCheck::Regexp::entityid;
my $db = $self->stash('db');
my $db = $self->app()->db();
my $sp = AccessCheck::Data::Entity->new(
db => $db,
......@@ -174,7 +148,7 @@ 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};
......
......@@ -31,7 +31,7 @@ Return the health status of the frontend.
sub run {
my $self = shift;
my $config = $self->app()->config();
my $config = $self->app()->config();
if (!$config->{status}) {
$self->render(
......
......@@ -10,10 +10,11 @@ use AccessCheck::Data::Entity;
sub run {
my $self = shift;
my $config = $self->app()->config();
my $log = $self->app()->log();
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n();
$self->init_user();
......@@ -21,7 +22,6 @@ sub run {
return if !$self->check_authentication();
}
my $db = $self->stash('db');
my $user = $self->stash('user');
my $sps = AccessCheck::Data::Entity->get_entities(
......
......@@ -8,10 +8,10 @@ use Syntax::Keyword::Try;
sub run {
my $self = shift;
my $config = $self->app()->config();
my $log = $self->app()->log();
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
$self->init_db();
$self->init_l10n();
$self->init_user();
......
......@@ -16,10 +16,11 @@ use AccessCheck::Tools;
sub run {
my $self = shift;
my $config = $self->app()->config();
my $log = $self->app()->log();
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n();
$self->init_user();
......@@ -29,7 +30,6 @@ sub run {
my $entityid = $self->param('entityid');
my $email = $self->param('email');
my $db = $self->stash('db');
my $l10n = $self->stash('l10n');
my $user = $self->stash('user');
......
......@@ -16,10 +16,11 @@ use AccessCheck::Tools;
sub run {
my $self = shift;
my $config = $self->app()->config();
my $log = $self->app()->log();
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n();
$self->init_user();
......@@ -32,7 +33,6 @@ sub run {
my $token = $self->param('token');
my $validity = $self->param('validity');
my $profiles = $self->every_param('profiles');
my $db = $self->stash('db');
my $l10n = $self->stash('l10n');
my $sp = $self->get_sp(entityid => $entityid);
......
......@@ -18,8 +18,8 @@ sub run {
my $app = $self->app();
my $config = $app->config();
my $log = $app->log();
my $db = $app->db();
$self->init_db();
$self->init_l10n();
$self->init_user();
......@@ -30,7 +30,6 @@ 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.
Finish editing this message first!
Please register or to comment