| 1 | package Arepa::Web::Base; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Mojolicious::Controller'; | ||
| 7 | |||
| 8 | use Arepa::Config; | ||
| 9 | |||
| 10 | use constant DEFAULT_CONFIG_PATH => '/etc/arepa/config.yml'; | ||
| 11 | our $config = undef; | ||
| 12 | our $config_path = $ENV{AREPA_CONFIG} || DEFAULT_CONFIG_PATH; | ||
| 13 | |||
| 14 | if (-r $config_path) { | ||
| 15 | $config = Arepa::Config->new($config_path); | ||
| 16 | } | ||
| 17 | else { | ||
| 18 | die "Couldn't read configuration file $config_path.\n" . | ||
| 19 | "Use the environment variable AREPA_CONFIG to specify one.\n"; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub config { return $Arepa::Web::Base::config; } | ||
| 23 | sub config_path { return $Arepa::Web::Base::config_path; } | ||
| 24 | |||
| 25 | |||
| 26 | sub _add_error { | ||
| 27 | my ($self, $error, $output) = @_; | ||
| 28 | push @{$self->{error_list}}, {error => $error, | ||
| 29 | output => $output || ""}; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub _error_list { | ||
| 33 | my ($self) = @_; | ||
| 34 | @{$self->{error_list} || []}; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _only_if_admin { | ||
| 38 | my ($self, $f) = @_; | ||
| 39 | |||
| 40 | if ($self->stash('is_user_admin')) { | ||
| 41 | $f->(); | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | $self->vars(errors => | ||
| 45 | [{error => "You need to be an admin to do this!"}]); | ||
| 46 | $self->render('error'); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub vars { | ||
| 51 | my ($self, %args) = @_; | ||
| 52 | |||
| 53 | my $auth_key = 'web_ui:authentication:type'; | ||
| 54 | my $external_auth = ($self->config->key_exists($auth_key) && | ||
| 55 | $self->config->get_key($auth_key) eq 'external'); | ||
| 56 | $self->stash( | ||
| 57 | base_url => $self->config->get_key('web_ui:base_url'), | ||
| 58 | external_auth => $external_auth, | ||
| 59 | %args); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub show_view { | ||
| 63 | my ($self, $stash, %opts) = @_; | ||
| 64 | |||
| 65 | $self->vars(%$stash); | ||
| 66 | if ($opts{template}) { | ||
| 67 | $self->render($opts{template}, layout => 'default'); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | $self->render(layout => 'default'); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |