blib/lib/Yukki.pm

filter: Statementstmt 99.1% 118 / 119 Branchbran 89.2% 25 / 28 Conditioncond 62.5% 10 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 30 / 30 Podpod 100% 7 / 7 totaltotal 92.3% 193 / 209 CC 24 SCAR 18.5
CC
24
Cov
94%
check_access
16.8
_build_config_file
5.0
_locate
3.0
CRAP 6.3
« blib/lib/Yukki/TemplateUtil.pm ↑ Summary blib/lib/Yukki/Web/View/Admin/User.pm »
1 package Yukki;
2 19 $Yukki::VERSION = '0.991_007'; # TRIAL
3 19 $Yukki::VERSION = '0.991007';
4 19 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 19 ▶use Yukki::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use Yukki::TextUtil qw( load_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use Yukki::Types qw( AccessLevel YukkiSettings );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 19 ▶use Crypt::SaltedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use Type::Params qw( validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use Types::Standard qw( Dict HashRef Str Maybe slurpy Optional );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 ▶use Types::Path::Tiny qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 19 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 19 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # ABSTRACT: Yet Uh-nother wiki - a git-based wiki with workgroups
27  
28  
29 19 has config_file => (
30 is => 'ro',
31 isa => Path,
32 required => 1,
33 coerce => 1,
34 lazy => 1,
35 builder => '_build_config_file',
36 );
37  
38 14 ▶sub _build_config_file {
Subroutine _build_config_file: called CC=5 SCAR=16.1
39 14 my $self = shift;
40  
41 14 my $cwd_conf = path('.', 'etc', 'yukki.conf');
42 14 ▶ if (not $ENV{YUKKI_CONFIG} and -f "$cwd_conf") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"YUKKI_CONFIG"} and -f "$cwd_conf") 1 13 14
Condition100%3/3
not $ENV{"YUKKI_CONFIG"} and -f "$cwd_conf"
!ll&&!rl&&r
1211
MC/DC100%2/2
not $ENV{"YUKKI_CONFIG"} and -f "$cwd_conf"
not $ENV{"YUKKI_CONFIG"} -f "$cwd_conf"
Truth table100%3/3
not(A) and B
A= not $ENV{"YUKKI_CONFIG"}B= -f "$cwd_conf"
ABresult
0X0
100
111
43 1 return $cwd_conf;
44 }
45  
46 die("Please make YUKKI_CONFIG point to your configuration file.\n")
47 13 ▶ unless defined $ENV{YUKKI_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"YUKKI_CONFIG"} 1 12 13
48  
49 die("No configuration found at $ENV{YUKKI_CONFIG}. Please set YUKKI_CONFIG to the correct location.\n")
50 12 ▶ unless -f $ENV{YUKKI_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $ENV{"YUKKI_CONFIG"} 1 11 12
51  
52 11 return $ENV{YUKKI_CONFIG};
53 }
54  
55  
56 19 has settings => (
57 is => 'ro',
58 isa => YukkiSettings,
59 required => 1,
60 coerce => 1,
61 lazy => 1,
62 builder => '_build_settings',
63 );
64  
65 11 ▶sub _build_settings {
Subroutine _build_settings: called CC=1 SCAR=0.0
66 11 my $self = shift;
67 11 load_file($self->config_file)
68 }
69  
70  
71 1 ▶sub view { ... }
Subroutine view: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 1 ▶sub controller { ... }
Subroutine controller: called CC=1 SCAR=0.0
Pod documented
75  
76  
77 76 ▶sub model {
Subroutine model: called CC=2 SCAR=6.9
Pod documented
78 76 my ($self, $name, $params) = @_;
79 76 my $class_name = join '::', 'Yukki::Model', $name;
80 76 Class::Load::load_class($class_name);
81 76 ▶ return $class_name->new(app => $self, %{ $params // {} });
Condition100%2/2
$params // {}
l!l
3343
MC/DC100%1/1
$params // {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
82 }
83  
84  
85 12 ▶sub _locate {
Subroutine _locate: called CC=3 SCAR=11.0
86 12 my ($self, $type, $base, @extra_path) = @_;
87  
88 12 my $base_path = $self->settings->$base;
89 12 my $root_path;
90  
91 12 ▶ if ($base_path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $base_path =~ m[^/]u) { } 10 2 12
92 10 $root_path = path($self->settings->root, $base_path);
93 }
94 else {
95 2 $root_path = path($base_path);
96 }
97  
98 12 my $located_path = $root_path->child(@extra_path);
99  
100 # Small safety mechanism
101 12 ▶ die "attempted to lookup an illegal $base path: ", join('/', @extra_path)
Branch: 1 branch
BranchTrueFalseTotal
unless $root_path->subsumes($located_path) 0 12 12
102 unless $root_path->subsumes($located_path);
103  
104 12 return $located_path;
105 }
106  
107 6 ▶sub locate {
Subroutine locate: called CC=1 SCAR=0.0
Pod documented
108 6 my ($self, $base, @extra_path) = @_;
109 6 $self->_locate(file => $base, @extra_path);
110 }
111  
112  
113 6 ▶sub locate_dir {
Subroutine locate_dir: called CC=1 SCAR=0.0
Pod documented
114 6 my ($self, $base, @extra_path) = @_;
115 6 $self->_locate(dir => $base, @extra_path);
116 }
117  
118  
119 42 ▶sub check_access {
Subroutine check_access: called CC=16 SCAR=28.2
Pod documented
120 42 my ($self, $opt)
121 = validate(\@_, class_type(__PACKAGE__),
122 slurpy Dict[
123 user => Maybe[class_type('Yukki::User')],
124 special => Optional[Str],
125 repository => Optional[Str],
126 needs => AccessLevel,
127 ]
128 );
129 my ($user, $repository, $special, $needs)
130 42 = @{$opt}{qw( user repository special needs )};
131  
132 42 ▶ $repository //= '-';
Condition50%1/2
$repository //= "-"
l!l
420
MC/DC0%0/1
$repository //= "-"
$repository
Truth table50%1/2
A
A= $repository
Aresult
11
00
133 42 ▶ $special //= '-';
Condition50%1/2
$special //= "-"
l!l
042
MC/DC0%0/1
$special //= "-"
$special
Truth table50%1/2
A
A= $special
Aresult
11
00
134  
135 # Always grant none
136 42 ▶ return 1 if $needs eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $needs eq "none" 14 28 42
137  
138 28 my $config;
139 28 ▶ $config = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
140 28 $self->model('Root')->repository($repository)->repository_settings;
141 28 };
142 28 ▶ $config //= $self->settings->special_privileges->{$special};
Condition33%1/3
$config //= $self->settings->special_privileges->{$special}
l!l&&r!l&&!r
2800
MC/DC0%0/2
$config //= $self->settings->special_privileges->{$special}
$config $self->settings->special_privileges->{$special}
Truth table33%1/3
A //= B
A= $configB= $self->settings->special_privileges->{$special}
ABresult
1X1
011
000
143  
144 28 ▶ return '' unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 28 28
145  
146 28 my $read_groups = $config->read_groups;
147 28 my $write_groups = $config->write_groups;
148  
149 28 my %access_level = (none => 0, read => 1, write => 2);
150 48 ▶ my $has_access = sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
151 48 ▶ $access_level{$_[0] // 'none'} >= $access_level{$needs}
Condition50%1/2
$_[0] // "none"
l!l
480
MC/DC0%0/1
$_[0] // "none"
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
152 28 };
153  
154 # Deal with anonymous users first.
155 28 ▶ return 1 if $has_access->($config->anonymous_access_level);
Branch: 1 branch
BranchTrueFalseTotal
if &$has_access($config->anonymous_access_level) 6 22 28
156 22 ▶ return '' unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 11 11 22
157  
158 # Only logged users considered here forward.
159 11 ▶ my @user_groups = @{ $user->{groups} // [] };
Condition50%1/2
$user->{"groups"} // []
l!l
110
MC/DC0%0/1
$user->{"groups"} // []
$user->{"groups"}
Truth table50%1/2
A
A= $user->{"groups"}
Aresult
11
00
160  
161 11 for my $level (qw( read write )) {
162 20 ▶ if ($has_access->($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$has_access($level)) 14 6 20
163  
164 14 my $groups = "${level}_groups";
165  
166 14 ▶ return 1 if $config->$groups eq 'ANY';
Branch: 1 branch
BranchTrueFalseTotal
if $config->$groups eq "ANY" 3 11 14
167  
168 11 ▶ if (ref $config->$groups eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $config->$groups eq 'ARRAY') { } 5 6 11
elsif ($config->$groups ne 'NONE') { } 0 6 6
169 5 my @level_groups = @{ $config->$groups };
170  
171 5 for my $level_group (@level_groups) {
172 9 ▶ return 1 if any { $_ eq $level_group } @user_groups;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $level_group; } , @user_groups) 3 6 9
173 }
174 }
175 elsif ($config->$groups ne 'NONE') {
176 0 warn "weird value ", $config->$groups,
177 " in $groups config for $repository settings";
178 }
179 }
180 }
181  
182 5 return '';
183 }
184  
185  
186 7 ▶sub hasher {
Subroutine hasher: called CC=1 SCAR=0.0
Pod documented
187 7 my $self = shift;
188  
189 7 return Crypt::SaltedHash->new(algorithm => $self->settings->digest);
190 }
191  
192 19 with qw( Yukki::Role::App );
193  
194  
195 19 1;
196  
197 __END__
« blib/lib/Yukki/TemplateUtil.pm ↑ Summary blib/lib/Yukki/Web/View/Admin/User.pm »