| 1 | package Galileo; | ||||||||||||||||||||
| 2 | 3 | ▶ | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.043'; | |||||||||||||||||||
| 5 | 3 | $VERSION = eval $VERSION; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | ▶ | use File::Spec; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 3 | ▶ | use File::Spec::Functions qw'rel2abs catdir'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 3 | ▶ | use File::ShareDir 'dist_dir'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 3 | ▶ | use Cwd; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 3 | ▶ | use Mojo::Home; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | has db => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 16 | 0 | my $self = shift; | |||||||||||||||||||
| 17 | 0 | ▶ | my $schema_class = $self->config->{db_schema} or die "Unknown DB Schema Class"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 0 | ▶ | eval "require $schema_class" or die "Could not load Schema Class ($schema_class). $@\n"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | my $schema = $schema_class->connect( | ||||||||||||||||||||
| 21 | 0 | @{ $self->config }{ qw/db_dsn db_username db_password db_options/ } | |||||||||||||||||||
| 22 | 0 | ▶ | ) or die "Could not connect to $schema_class using DSN " . $self->config->{db_dsn}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | return $schema; | |||||||||||||||||||
| 25 | 3 | }; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 3 | ▶ | has home => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 28 | 3 | ▶ | my $path = $ENV{GALILEO_HOME} || getcwd; | ||||||||||||||||||
|
Condition66%2/3
$ENV{'GALILEO_HOME'} || getcwd()
MC/DC0%0/2
$ENV{'GALILEO_HOME'} || getcwd()
$ENV{'GALILEO_HOME'}
getcwd()
Truth table66%2/3
A || B
A= $ENV{'GALILEO_HOME'}B= getcwd()
| |||||||||||||||||||||
| 29 | 3 | return Mojo::Home->new(File::Spec->rel2abs($path)); | |||||||||||||||||||
| 30 | 3 | }; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | ▶ | has config_file => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 33 | 3 | my $self = shift; | |||||||||||||||||||
| 34 | 3 | ▶ | return $ENV{GALILEO_CONFIG} if $ENV{GALILEO_CONFIG}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 3 | return $self->home->rel_file('galileo.conf'); | |||||||||||||||||||
| 36 | 3 | }; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 3 | ▶ | sub load_config { | ||||||||||||||||||
|
Subroutine
load_config: called CC=13 SCAR=33.3
Pod
undocumented
| |||||||||||||||||||||
| 39 | 3 | my $app = shift; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 3 | $app->plugin( Config => { | |||||||||||||||||||
| 42 | file => $app->config_file, | ||||||||||||||||||||
| 43 | default => { | ||||||||||||||||||||
| 44 | db_schema => 'Galileo::DB::Schema', | ||||||||||||||||||||
| 45 | db_dsn => 'dbi:SQLite:dbname=' . $app->home->rel_file('galileo.db'), | ||||||||||||||||||||
| 46 | db_username => undef, | ||||||||||||||||||||
| 47 | db_password => undef, | ||||||||||||||||||||
| 48 | db_options => { sqlite_unicode => 1 }, | ||||||||||||||||||||
| 49 | extra_css => [ '/themes/standard.css' ], | ||||||||||||||||||||
| 50 | extra_js => [], | ||||||||||||||||||||
| 51 | extra_static_paths => ['static'], | ||||||||||||||||||||
| 52 | sanitize => 1, | ||||||||||||||||||||
| 53 | pagedown_extra_options => { | ||||||||||||||||||||
| 54 | highlighter => 'highlight', | ||||||||||||||||||||
| 55 | table_class => 'table table-striped', | ||||||||||||||||||||
| 56 | extensions => [qw/ | ||||||||||||||||||||
| 57 | tables | ||||||||||||||||||||
| 58 | fenced_code_gfm | ||||||||||||||||||||
| 59 | def_list | ||||||||||||||||||||
| 60 | attr_list | ||||||||||||||||||||
| 61 | footnotes | ||||||||||||||||||||
| 62 | smartypants | ||||||||||||||||||||
| 63 | strikethrough | ||||||||||||||||||||
| 64 | newlines | ||||||||||||||||||||
| 65 | /], | ||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||
| 67 | secrets => [], | ||||||||||||||||||||
| 68 | upload_path => 'uploads', | ||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # handle deprecated db_connect | ||||||||||||||||||||
| 73 | 3 | ▶ | if ( my $db_connect = delete $app->config->{db_connect} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | warn "### Configuration key db_connect is deprecated ###\n"; | |||||||||||||||||||
| 75 | 0 | ▶ | if ( ref $db_connect ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | @{ $app->config }{ qw/db_dsn db_username db_password db_options/ } = @$db_connect; | |||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||
| 78 | 0 | $app->config->{db_dsn} = $db_connect; | |||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # handle deprecated files key | ||||||||||||||||||||
| 83 | 3 | ▶ | if (my $files = $app->config->{files}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | warn "### 'files' config key is deprecated, use extra_static_paths ###\n"; | |||||||||||||||||||
| 85 | 0 | ▶ | push @{ $app->config->{extra_static_paths} }, (ref $files ? @$files : $files); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # add the files directories to array of static content folders | ||||||||||||||||||||
| 89 | # TODO don't repeat | ||||||||||||||||||||
| 90 | 3 | foreach my $dir ( @{$app->config->{extra_static_paths}} ) { | |||||||||||||||||||
| 91 | # convert relative paths to relative one (to home dir) | ||||||||||||||||||||
| 92 | 3 | $dir = $app->_to_abs($dir); | |||||||||||||||||||
| 93 | 3 | ▶ | push @{ $app->static->paths }, $dir if -d $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # normalize and make helper for upload directory | ||||||||||||||||||||
| 97 | 3 | my $upload; | |||||||||||||||||||
| 98 | 3 | ▶ | if ( my $dir = $app->config->{upload_path} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 3 | $upload = $app->_to_abs($dir); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 3 | ▶ | if ( -d $upload ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 1 | push @{ $app->static->paths }, $upload; | |||||||||||||||||||
| 103 | 1 | $app->config->{upload_path} = $upload; | |||||||||||||||||||
| 104 | 1 | ▶ | $app->helper( upload_path => sub { $upload } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||
| 106 | 0 | ▶ | $app->helper( upload_path => sub { 0 } ); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 3 | ▶ | if ( my $secrets = $app->config->{secrets} ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 111 | 3 | ▶ | $app->secrets($secrets) if @$secrets; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | } elsif ( my $secret = $app->config->{secret} ) { | ||||||||||||||||||||
| 113 | 0 | warn "### 'secret' config key is deprecated, use 'secrets' instead ###\n"; | |||||||||||||||||||
| 114 | 0 | ▶ | $app->secrets([$secret]) if $secret; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 6 | ▶ | sub _to_abs { | ||||||||||||||||||
|
Subroutine
_to_abs: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 119 | 6 | my ($self, $dir) = @_; | |||||||||||||||||||
| 120 | 6 | ▶ | unless ( File::Spec->file_name_is_absolute( $dir ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 6 | $dir = $self->home->rel_file( $dir ); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | 6 | return $dir; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 3 | ▶ | sub startup { | ||||||||||||||||||
|
Subroutine
startup: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 127 | 3 | my $app = shift; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 3 | $app->load_config; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | { | ||||||||||||||||||||
| 132 | # use content from directories under lib/Galileo/files or using File::ShareDir | ||||||||||||||||||||
| 133 | 3 | my $lib_base = catdir(dirname(rel2abs(__FILE__)), 'Galileo', 'files'); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 3 | my $public = catdir($lib_base, 'public'); | |||||||||||||||||||
| 136 | 3 | ▶ | $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('Galileo'), 'public'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 3 | my $templates = catdir($lib_base, 'templates'); | |||||||||||||||||||
| 139 | 3 | ▶ | $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('Galileo'), 'templates'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # use commands from Galileo::Command namespace | ||||||||||||||||||||
| 143 | 3 | push @{$app->commands->namespaces}, 'Galileo::Command'; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | ## Helpers ## | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 3 | ▶ | $app->helper( schema => sub { shift->app->db } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 3 | ▶ | $app->helper( 'home_page' => sub{ '/page/home' } ); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 3 | ▶ | $app->helper( 'auth_fail' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 152 | 3 | my $self = shift; | |||||||||||||||||||
| 153 | 3 | ▶ | my $message = shift || "Not Authorized"; | ||||||||||||||||||
|
Condition50%1/2
shift() || 'Not Authorized'
MC/DC0%0/1
shift() || 'Not Authorized'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||
| 154 | 3 | $self->humane_flash( $message ); | |||||||||||||||||||
| 155 | 3 | $self->redirect_to( $self->home_page ); | |||||||||||||||||||
| 156 | 3 | return 0; | |||||||||||||||||||
| 157 | 3 | }); | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 75 | ▶ | $app->helper( 'get_user' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 160 | 75 | my ($self, $name) = @_; | |||||||||||||||||||
| 161 | 75 | ▶ | unless ($name) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 34 | $name = $self->session->{username}; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | 75 | ▶ | return undef unless $name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | 70 | return $self->schema->resultset('User')->single({name => $name}); | |||||||||||||||||||
| 166 | 3 | }); | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 38 | ▶ | $app->helper( 'is_author' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 169 | 38 | my $self = shift; | |||||||||||||||||||
| 170 | 38 | my $user = $self->get_user(@_); | |||||||||||||||||||
| 171 | 38 | ▶ | return undef unless $user; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 34 | return $user->is_author; | |||||||||||||||||||
| 173 | 3 | }); | |||||||||||||||||||
| 174 | 32 | ▶ | $app->helper( 'is_admin' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 175 | 32 | my $self = shift; | |||||||||||||||||||
| 176 | 32 | my $user = $self->get_user(@_); | |||||||||||||||||||
| 177 | 32 | ▶ | return undef unless $user; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 31 | return $user->is_admin; | |||||||||||||||||||
| 179 | 3 | }); | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 3 | $app->plugin('Memorize'); | |||||||||||||||||||
| 182 | 3 | $app->plugin('Galileo::Plugin::Modal'); | |||||||||||||||||||
| 183 | 3 | $app->plugin('Humane', auto => 0); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | ## Routing ## | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 3 | my $r = $app->routes; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 3 | ▶ | $r->any( '/' => sub { my $self = shift; $self->redirect_to( $self->home_page ) }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 190 | 3 | $r->any( '/page/:name' )->to('page#show')->name('show_page'); | |||||||||||||||||||
| 191 | 3 | $r->post( '/login' )->to('user#login'); | |||||||||||||||||||
| 192 | 3 | $r->any( '/logout' )->to('user#logout'); | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 16 | ▶ | my $if_author = $r->under( sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 195 | 16 | my $self = shift; | |||||||||||||||||||
| 196 | 16 | ▶ | return $self->is_author || $self->auth_fail; | ||||||||||||||||||
|
Condition66%2/3
$self->is_author || $self->auth_fail
MC/DC50%1/2
$self->is_author || $self->auth_fail
$self->is_author
$self->auth_fail
Truth table66%2/3
A || B
A= $self->is_authorB= $self->auth_fail
| |||||||||||||||||||||
| 197 | 3 | }); | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 3 | $if_author->any( '/admin/menu' )->to('menu#edit'); | |||||||||||||||||||
| 200 | 3 | $if_author->any( '/edit/:name' )->to('page#edit')->name('edit_page'); | |||||||||||||||||||
| 201 | 3 | $if_author->websocket( '/store/page' )->to('page#store'); | |||||||||||||||||||
| 202 | 3 | $if_author->websocket( '/store/menu' )->to('menu#store'); | |||||||||||||||||||
| 203 | 3 | $if_author->websocket( '/files/list' )->to('file#list'); | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 14 | ▶ | my $if_admin = $r->under( sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 206 | 14 | my $self = shift; | |||||||||||||||||||
| 207 | 14 | ▶ | return $self->is_admin || $self->auth_fail; | ||||||||||||||||||
|
Condition66%2/3
$self->is_admin || $self->auth_fail
MC/DC50%1/2
$self->is_admin || $self->auth_fail
$self->is_admin
$self->auth_fail
Truth table66%2/3
A || B
A= $self->is_adminB= $self->auth_fail
| |||||||||||||||||||||
| 208 | 3 | }); | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 3 | $if_admin->any( '/admin/users' )->to('admin#users'); | |||||||||||||||||||
| 211 | 3 | $if_admin->any( '/admin/pages' )->to('admin#pages'); | |||||||||||||||||||
| 212 | 3 | $if_admin->any( '/admin/user/:name' )->to('admin#user'); | |||||||||||||||||||
| 213 | 3 | $if_admin->websocket( '/store/user' )->to('admin#store_user'); | |||||||||||||||||||
| 214 | 3 | $if_admin->websocket( '/remove/page' )->to('admin#remove_page'); | |||||||||||||||||||
| 215 | |||||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 3 | 1; | |||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | __END__ | ||||||||||||||||||||