| 1 | package AproJo; | ||||||||||||||||||||
| 2 | 2 | ▶ | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.015'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use File::Spec::Functions qw'rel2abs catdir'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use File::ShareDir 'dist_dir'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use Cwd; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 2 | ▶ | use Mojo::Home; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 0 | ▶ | has db => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 17 | 0 | my $self = shift; | |||||||||||||||||||
| 18 | my $schema_class = $self->config->{db_schema} | ||||||||||||||||||||
| 19 | 0 | ▶ | or die "Unknown DB Schema Class"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 20 | 0 | ▶ | eval "require $schema_class" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | or die "Could not load Schema Class ($schema_class), $@\n"; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | my $db_connect = $self->config->{db_connect} | ||||||||||||||||||||
| 24 | 0 | ▶ | or die "No DBI connection string provided"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 0 | ▶ | my @db_connect = ref $db_connect ? @$db_connect : ($db_connect); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | ▶ | my $schema = $schema_class->connect(@db_connect) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | or die "Could not connect to $schema_class using $db_connect[0]"; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | return $schema; | |||||||||||||||||||
| 31 | 2 | }; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 2 | has app_debug => 0; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 1 | ▶ | has home => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.1
| |||||||||||||||||||||
| 36 | 1 | ▶ | my $path = $ENV{MOJO_HOME} || getcwd; | ||||||||||||||||||
|
Condition33%1/3
$ENV{'MOJO_HOME'} || getcwd()
MC/DC0%0/2
$ENV{'MOJO_HOME'} || getcwd()
$ENV{'MOJO_HOME'}
getcwd()
Truth table33%1/3
A || B
A= $ENV{'MOJO_HOME'}B= getcwd()
| |||||||||||||||||||||
| 37 | 1 | return Mojo::Home->new(File::Spec->rel2abs($path)); | |||||||||||||||||||
| 38 | 2 | }; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 1 | ▶ | has config_file => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 41 | 1 | my $self = shift; | |||||||||||||||||||
| 42 | 1 | ▶ | return $ENV{APROJO_CONFIG} if $ENV{APROJO_CONFIG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | return $self->home->rel_file('aprojo.conf'); | |||||||||||||||||||
| 45 | 2 | }; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 1 | ▶ | sub startup { | ||||||||||||||||||
|
Subroutine
startup: called CC=3 SCAR=14.7
Pod
documented
| |||||||||||||||||||||
| 48 | 1 | my $app = shift; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1 | $app->plugin( | |||||||||||||||||||
| 51 | Config => { | ||||||||||||||||||||
| 52 | file => $app->config_file, | ||||||||||||||||||||
| 53 | default => { | ||||||||||||||||||||
| 54 | 'db_connect' => [ | ||||||||||||||||||||
| 55 | 'dbi:SQLite:dbname=' . $app->home->rel_file('aprojo.db'), | ||||||||||||||||||||
| 56 | undef, | ||||||||||||||||||||
| 57 | undef, | ||||||||||||||||||||
| 58 | {'sqlite_unicode' => 1} | ||||||||||||||||||||
| 59 | ], | ||||||||||||||||||||
| 60 | 'db_schema' => 'AproJo::DB::Schema', | ||||||||||||||||||||
| 61 | 'secret' => '47110815' | ||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | { | ||||||||||||||||||||
| 67 | # use content from directories under share/files or using File::ShareDir | ||||||||||||||||||||
| 68 | 1 | my $lib_base = catdir(dirname(rel2abs(__FILE__)), '..', 'share','files'); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 1 | my $public = catdir($lib_base, 'public'); | |||||||||||||||||||
| 71 | 1 | ▶ | $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('AproJo'), 'files','public'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 1 | my $static_path = $app->static->paths->[0]; | |||||||||||||||||||
| 73 | #print STDERR '$static_path: ',$static_path,"\n"; | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | my $templates = catdir($lib_base, 'templates'); | |||||||||||||||||||
| 76 | 1 | ▶ | $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('AproJo'), 'files', 'templates'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 1 | $app->plugin('I18N'); | |||||||||||||||||||
| 80 | 1 | $app->plugin('Mojolicious::Plugin::ServerInfo'); | |||||||||||||||||||
| 81 | 1 | $app->plugin('Mojolicious::Plugin::DBInfo'); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 1 | $app->plugin('Mojolicious::Plugin::Form'); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 1 | push @{$app->commands->namespaces}, 'AproJo::Command'; | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | #DEPRECATED: $app->secret( $app->config->{secret} ); | ||||||||||||||||||||
| 88 | 1 | $app->secrets([$app->config->{secret}]); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 1 | ▶ | $app->helper(schema => sub { shift->app->db }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1 | ▶ | $app->helper('home_page' => sub {'/'}); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | $app->helper( | ||||||||||||||||||||
| 95 | 0 | ▶ | 'auth_fail' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 96 | 0 | my $self = shift; | |||||||||||||||||||
| 97 | 0 | ▶ | my $message = shift || "Not Authorized"; | ||||||||||||||||||
|
Condition0%0/2
shift() || 'Not Authorized'
MC/DC0%0/1
shift() || 'Not Authorized'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 98 | 0 | $self->flash(onload_message => $message); | |||||||||||||||||||
| 99 | 0 | $self->redirect_to($self->home_page); | |||||||||||||||||||
| 100 | 0 | return 0; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 1 | ); | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | $app->helper( | ||||||||||||||||||||
| 105 | 0 | ▶ | 'source_id' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 106 | 0 | my ($self, $source) = @_; | |||||||||||||||||||
| 107 | 0 | ▶ | return undef unless $source; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | 0 | my @columns = $self->schema->source($source)->columns; | |||||||||||||||||||
| 109 | 0 | my $table_name = $self->schema->class($source)->table; | |||||||||||||||||||
| 110 | 0 | my $source_id = $table_name . '_id'; | |||||||||||||||||||
| 111 | 0 | ▶ | return $source_id if (grep {/$source_id/} @columns); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | ▶ | return $columns[0] if (scalar @columns); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | 1 | ); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | $app->helper( | ||||||||||||||||||||
| 117 | 1 | ▶ | 'get_user' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||
| 118 | 1 | my ($self, $name) = @_; | |||||||||||||||||||
| 119 | 1 | ▶ | unless ($name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 0 | $name = $self->session->{username}; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | 1 | ▶ | return undef unless $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | #say STDERR 'get_user: ', $name if $self->app->app_debug; | ||||||||||||||||||||
| 124 | 1 | return $self->schema->resultset('User')->single({name => $name}); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | 1 | ); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | $app->helper( | ||||||||||||||||||||
| 129 | 0 | ▶ | 'has_role' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 130 | 0 | my $self = shift; | |||||||||||||||||||
| 131 | 0 | ▶ | my $user_string = shift || ''; | ||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 132 | 0 | ▶ | my $role_string = shift || ''; | ||||||||||||||||||
|
Condition0%0/2
shift() || ''
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 133 | 0 | my $user = $self->get_user($user_string); | |||||||||||||||||||
| 134 | 0 | ▶ | return undef unless $user; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | my $role = $user->roles()->single({name => $role_string}); | |||||||||||||||||||
| 136 | #say STDERR 'has_role: ', $role->name if $self->app->app_debug; | ||||||||||||||||||||
| 137 | 0 | ▶ | return ($role && $role->name eq $role_string); | ||||||||||||||||||
|
Condition0%0/3
$role && $role->name eq $role_string
MC/DC0%0/2
$role && $role->name eq $role_string
$role
$role->name eq $role_string
Truth table0%0/3
A && B
A= $roleB= $role->name eq $role_string
| |||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | 1 | ); | |||||||||||||||||||
| 140 | $app->helper( | ||||||||||||||||||||
| 141 | 0 | ▶ | 'is_admin' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 142 | 0 | my ($self,$user) = @_; | |||||||||||||||||||
| 143 | 0 | return $self->has_role($user,'admin'); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 1 | ); | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 1 | my $routes = $app->routes; | |||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 1 | $routes->get('/')->to('front#index'); | |||||||||||||||||||
| 150 | 1 | $routes->get('/front/*name')->to('front#page'); | |||||||||||||||||||
| 151 | 1 | $routes->post('/save')->to('front#save'); | |||||||||||||||||||
| 152 | 1 | $routes->post('/login')->to('user#login'); | |||||||||||||||||||
| 153 | 1 | $routes->any('/logout')->to('user#logout'); | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | my $if_admin = $routes->under( | ||||||||||||||||||||
| 156 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 157 | 0 | my $self = shift; | |||||||||||||||||||
| 158 | 0 | ▶ | return $self->auth_fail unless $self->is_admin; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | 0 | return 1; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | 1 | ); | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 1 | $if_admin->post('/admin/save/:table')->to('admin#save'); | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 1 | $if_admin->get('/admin/change/:table/:id')->to('admin#change'); | |||||||||||||||||||
| 166 | 1 | $if_admin->get('/admin/show/:table')->to('admin#show'); | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 2 | 1; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||