| 1 | package ETLp::Audit::Browser::Controller::Base; | ||
| 2 | use Moose; | ||
| 3 | BEGIN { extends qw(CGI::Application Moose::Object); } | ||
| 4 | with qw(ETLp::Role::Config ETLp::Role::Schema ETLp::Role::Audit); | ||
| 5 | |||
| 6 | use CGI::Application::Plugin::TT; | ||
| 7 | use CGI::Application::Plugin::Config::General; | ||
| 8 | use CGI::Application::Plugin::ParsePath; | ||
| 9 | use CGI::Application::Plugin::HTMLPrototype; | ||
| 10 | use CGI::Application::Plugin::Session; | ||
| 11 | use CGI::Application::Plugin::Redirect; | ||
| 12 | use CGI::Application::Plugin::Forward; | ||
| 13 | use URI::Escape; | ||
| 14 | use UNIVERSAL::require; | ||
| 15 | use Log::Log4perl qw(get_logger); | ||
| 16 | use FindBin qw($Bin); | ||
| 17 | use Data::Dumper; | ||
| 18 | use ETLp::Config; | ||
| 19 | use ETLp::Schema; | ||
| 20 | use DBI; | ||
| 21 | use DBI::Const::GetInfoType; | ||
| 22 | |||
| 23 | sub cgiapp_prerun { | ||
| 24 | my $self = shift; | ||
| 25 | $self->_create_logger unless $self->logger; | ||
| 26 | $self->logger->debug($self->dump); | ||
| 27 | $self->logger->debug('DSN: ' . $self->conf->param('dsn')); | ||
| 28 | |||
| 29 | my $config = ETLp::Config->instance; | ||
| 30 | |||
| 31 | unless ($config->schema) { | ||
| 32 | my $dbh = DBI->connect( | ||
| 33 | $self->conf->param('dsn'), | ||
| 34 | $self->conf->param('user'), | ||
| 35 | $self->conf->param('password') | ||
| 36 | ); | ||
| 37 | |||
| 38 | if (lc $dbh->get_info($GetInfoType{SQL_DBMS_NAME}) eq 'oracle') { | ||
| 39 | $dbh->{LongReadLen} = 1000000; | ||
| 40 | $dbh->{LongTruncOk} = 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | $config->schema( | ||
| 44 | ETLp::Schema->connect( | ||
| 45 | sub {$dbh}, | ||
| 46 | {on_connect_call => 'datetime_setup'} | ||
| 47 | ) | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | unless ($config->dbh) { | ||
| 52 | $config->dbh( | ||
| 53 | DBI->connect_cached( | ||
| 54 | $self->conf->param('dsn'), | ||
| 55 | $self->conf->param('user'), | ||
| 56 | $self->conf->param('password'), | ||
| 57 | {RaiseError => 1, session => 1, AutoCommit => 1} | ||
| 58 | ) | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | |||
| 62 | my $db_type = lc $self->get_driver; | ||
| 63 | |||
| 64 | $self->session_config( | ||
| 65 | CGI_SESSION_OPTIONS => | ||
| 66 | ["driver:$db_type", $self->query, {Handle => $config->dbh}], | ||
| 67 | COOKIE_PARAMS => {-path => '/',}, | ||
| 68 | SEND_COOKIE => 1, | ||
| 69 | ); | ||
| 70 | |||
| 71 | $self->error_mode('error'); | ||
| 72 | my @include_path = @{$self->tt_include_path}; | ||
| 73 | push @include_path, $self->get_include_dir; | ||
| 74 | $self->tt_include_path(["$Bin/../view", "$Bin/../view/includes"]); | ||
| 75 | |||
| 76 | unless ($self->session->param('user_id') | ||
| 77 | || $self->get_current_runmode =~ /(?:login|validate_login|error)/) | ||
| 78 | { | ||
| 79 | my $redir = | ||
| 80 | $self->conf->param('root_url') | ||
| 81 | . '/user/login?next=' | ||
| 82 | . uri_escape($ENV{'REQUEST_URI'}); | ||
| 83 | return $self->redirect($redir); | ||
| 84 | } | ||
| 85 | |||
| 86 | unless ($self->check_runmode_permissions) { | ||
| 87 | $self->param( | ||
| 88 | 'message' => "You do not have permission to call this function"); | ||
| 89 | $self->prerun_mode('error'); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | sub cgiapp_init { | ||
| 94 | my $self = shift; | ||
| 95 | $self->conf->init( | ||
| 96 | -ConfigFile => $self->param('config_file'), | ||
| 97 | -CacheConfigFiles => 1, | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub cgiapp_postrun { | ||
| 102 | my $self = shift; | ||
| 103 | $self->session->flush; | ||
| 104 | $self->header_add(-type => 'text/html; charset=utf-8'); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub tt_pre_process { | ||
| 108 | my ($self, $file, $vars) = @_; | ||
| 109 | $vars->{script} = $ENV{SCRIPT_NAME}; | ||
| 110 | $vars->{module} = $self->module; | ||
| 111 | $vars->{root_url} = $self->conf->param('root_url'); | ||
| 112 | $vars->{db_type} = lc $self->get_driver; | ||
| 113 | |||
| 114 | # If we have a message, send it to the template, and then remove it from the | ||
| 115 | # stack | ||
| 116 | if ($self->session->param('message')) { | ||
| 117 | $vars->{message} = $self->session->param('message'); | ||
| 118 | $self->session->clear('message'); | ||
| 119 | } | ||
| 120 | |||
| 121 | my $params = $self->query->Vars; | ||
| 122 | delete $params->{rm}; | ||
| 123 | delete $params->{page}; | ||
| 124 | |||
| 125 | # This is to add any filter criteria for the paginators | ||
| 126 | $vars->{criteria} = '&' . join('&', map "$_=$params->{$_}", keys %$params) | ||
| 127 | if %$params; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Created a log4perl logger. Should only be called if the logger doesn't exist | ||
| 131 | sub _create_logger { | ||
| 132 | my $self = shift; | ||
| 133 | my $log_dir = "$Bin/../log"; | ||
| 134 | |||
| 135 | my $log_conf = qq( | ||
| 136 | log4perl.rootLogger=DEBUG,LOGFILE | ||
| 137 | |||
| 138 | log4perl.appender.LOGFILE=Log::Dispatch::FileRotate | ||
| 139 | log4perl.appender.LOGFILE.filename = $log_dir/runtime_browser.log | ||
| 140 | log4perl.appender.LOGFILE.mode=append | ||
| 141 | log4perl.appender.LOGFILE.max = 5 | ||
| 142 | log4perl.appender.LOGFILE.size = 10000000 | ||
| 143 | log4perl.appender.LOGFILE.TZ = NZT | ||
| 144 | |||
| 145 | log4perl.appender.LOGFILE.layout = PatternLayout | ||
| 146 | log4perl.appender.LOGFILE.layout.ConversionPattern = %d %l %p> %m%n | ||
| 147 | ); | ||
| 148 | |||
| 149 | Log::Log4perl::init(\$log_conf); | ||
| 150 | my $logger = Log::Log4perl::get_logger("runtime_browser") || die $!; | ||
| 151 | ETLp::Config->logger($logger); | ||
| 152 | } | ||
| 153 | |||
| 154 | # returns the model partner of the current controller | ||
| 155 | sub model { | ||
| 156 | my $self = shift; | ||
| 157 | unless ($self->param('model')) { | ||
| 158 | my $model_name = (caller(0))[0]; | ||
| 159 | $model_name =~ s/::Controller::/::Model::/; | ||
| 160 | $model_name->require || die $@; | ||
| 161 | my $pagesize = $self->conf->param('pagesize'); | ||
| 162 | $self->logger->debug("Pagesize: $pagesize"); | ||
| 163 | $self->param('model', $model_name->new({pagesize => $pagesize})); | ||
| 164 | } | ||
| 165 | return $self->param('model'); | ||
| 166 | } | ||
| 167 | |||
| 168 | # Determne the directory where the views should be found | ||
| 169 | sub get_include_dir { | ||
| 170 | my $self = shift; | ||
| 171 | my $uplevel = shift || 0; | ||
| 172 | |||
| 173 | # the directory is based on the object's package name | ||
| 174 | my $dir = File::Spec->catdir(split(/::/, ref($self))); | ||
| 175 | $dir =~ s!\\!\/!g; | ||
| 176 | return "$Bin/../lib/MEL/View/$dir"; | ||
| 177 | } | ||
| 178 | |||
| 179 | # This gets called in the event of an error when a runmode is executing. | ||
| 180 | # It will not be called if an error occurs before a runmode is executed or | ||
| 181 | # after it finishes | ||
| 182 | sub error { | ||
| 183 | my $self = shift; | ||
| 184 | my $error = shift || $self->session->param('error'); | ||
| 185 | $self->session->clear('error'); | ||
| 186 | $self->logger->error($error); | ||
| 187 | return $self->tt_process('error.tmpl', {error => $error}); | ||
| 188 | } | ||
| 189 | |||
| 190 | # This is required when sub-classing a non-Moose class like CGI::Application | ||
| 191 | sub new { | ||
| 192 | my $class = shift; | ||
| 193 | my $obj = $class->SUPER::new(@_); | ||
| 194 | |||
| 195 | # $obj->BUILD; | ||
| 196 | |||
| 197 | return $class->meta->new_object( | ||
| 198 | __INSTANCE__ => $obj, | ||
| 199 | @_, | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | sub check_runmode_permissions { | ||
| 203 | my $self = shift; | ||
| 204 | my $req_mode = $self->get_current_runmode() || $self->start_mode(); | ||
| 205 | $self->logger->debug("Requested runmode: $req_mode"); | ||
| 206 | my $rm = $self->param('run_modes') || return 1; | ||
| 207 | my %rm = %$rm; | ||
| 208 | |||
| 209 | # Exit: we don't want to interfere with errors | ||
| 210 | return 1 if ($req_mode eq 'error'); | ||
| 211 | |||
| 212 | my @valid_permissions = @{$rm{$req_mode}->{authorization}}; | ||
| 213 | |||
| 214 | # if no permissions are defined then continue to runmode | ||
| 215 | return 1 unless (@valid_permissions > 0); | ||
| 216 | |||
| 217 | # This is for ETL-Pipeline only | ||
| 218 | foreach my $allowed_role (@valid_permissions) { | ||
| 219 | return 1 | ||
| 220 | if (($allowed_role eq 'admin') && $self->session->param('admin')); | ||
| 221 | } | ||
| 222 | |||
| 223 | return 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub module { | ||
| 227 | die 'Error: cannot call abstract method "module"'; | ||
| 228 | } | ||
| 229 | |||
| 230 | # __PACKAGE__->meta->make_immutable( inline_constructor => 0 ); | ||
| 231 | |||
| 232 | 1; | ||
| 233 |