blib/lib/Yukki.pm

filter: Statementstmt 99.1% 112 / 113 Branchbran 89.2% 25 / 28 Conditioncond 62.5% 10 / 16 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 100% 28 / 28 Podpod 100% 7 / 7 totaltotal 92.0% 185 / 201 SCAR 31.8
CC
24
Cov
94%
check_access
17.0
_build_config_file
5.0
_locate
3.0
CRAP 24.1
« blib/lib/Yukki/Web/View/Admin/User.pm ↑ Summary blib/lib/Yukki/TemplateUtil.pm »
1 package Yukki;
2 13 $Yukki::VERSION = '0.991_002'; # TRIAL
3  
4 13 ▶$Yukki::VERSION = '0.991002';use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Yukki::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Yukki::TextUtil qw( load_file );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Yukki::Types qw( AccessLevel YukkiSettings );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 ▶use Crypt::SaltedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Type::Params qw( validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use Types::Standard qw( Dict HashRef Str Maybe slurpy Optional );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use Types::Path::Tiny qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # ABSTRACT: Yet Uh-nother wiki
26  
27  
28 13 has config_file => (
29 is => 'ro',
30 isa => Path,
31 required => 1,
32 coerce => 1,
33 lazy => 1,
34 builder => '_build_config_file',
35 );
36  
37 sub _build_config_file {
38 11 ▶ my $self = shift;
Subroutine _build_config_file: called CC=5 SCAR=16.1
39  
40 11 my $cwd_conf = path('.', 'etc', 'yukki.conf');
41 11 ▶ if (not $ENV{YUKKI_CONFIG} and -f "$cwd_conf") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{"YUKKI_CONFIG"} and -f "$cwd_conf") 1 10 11
Condition100%3/3
not $ENV{"YUKKI_CONFIG"} and -f "$cwd_conf"
!ll&&!rl&&r
911
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
42 1 return $cwd_conf;
43 }
44  
45 die("Please make YUKKI_CONFIG point to your configuration file.\n")
46 10 ▶ unless defined $ENV{YUKKI_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"YUKKI_CONFIG"} 1 9 10
47  
48 die("No configuration found at $ENV{YUKKI_CONFIG}. Please set YUKKI_CONFIG to the correct location.\n")
49 9 ▶ unless -f $ENV{YUKKI_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $ENV{"YUKKI_CONFIG"} 1 8 9
50  
51 8 return $ENV{YUKKI_CONFIG};
52 }
53  
54  
55 13 has settings => (
56 is => 'ro',
57 isa => YukkiSettings,
58 required => 1,
59 coerce => 1,
60 lazy => 1,
61 builder => '_build_settings',
62 );
63  
64 sub _build_settings {
65 8 ▶ my $self = shift;
Subroutine _build_settings: called CC=1 SCAR=0.0
66 8 load_file($self->config_file)
67 }
68  
69  
70 1 ▶sub view { ... }
Subroutine view: called CC=1 SCAR=0.0
Pod documented
71  
72  
73 1 ▶sub controller { ... }
Subroutine controller: called CC=1 SCAR=0.0
Pod documented
74  
75  
76 sub model {
77 14 ▶ my ($self, $name, $params) = @_;
Subroutine model: called CC=2 SCAR=6.9
Pod documented
78 14 my $class_name = join '::', 'Yukki::Model', $name;
79 14 Class::Load::load_class($class_name);
80 14 ▶ return $class_name->new(app => $self, %{ $params // {} });
Condition100%2/2
$params // {}
l!l
311
MC/DC100%1/1
$params // {}
$params
Truth table100%2/2
A
A= $params
Aresult
11
00
81 }
82  
83  
84 sub _locate {
85 9 ▶ my ($self, $type, $base, @extra_path) = @_;
Subroutine _locate: called CC=3 SCAR=11.0
86  
87 9 my $base_path = $self->settings->$base;
88 9 my $root_path;
89  
90 9 ▶ if ($base_path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $base_path =~ m[^/]u) { } 7 2 9
91 7 $root_path = path($self->settings->root, $base_path);
92 }
93 else {
94 2 $root_path = path($base_path);
95 }
96  
97 9 my $located_path = $root_path->child(@extra_path);
98  
99 # Small safety mechanism
100 9 ▶ die "attempted to lookup an illegal $base path: ", join('/', @extra_path)
Branch: 1 branch
BranchTrueFalseTotal
unless $root_path->subsumes($located_path) 0 9 9
101 unless $root_path->subsumes($located_path);
102  
103 9 return $located_path;
104 }
105  
106 sub locate {
107 5 ▶ my ($self, $base, @extra_path) = @_;
Subroutine locate: called CC=1 SCAR=0.0
Pod documented
108 5 $self->_locate(file => $base, @extra_path);
109 }
110  
111  
112 sub locate_dir {
113 4 ▶ my ($self, $base, @extra_path) = @_;
Subroutine locate_dir: called CC=1 SCAR=0.0
Pod documented
114 4 $self->_locate(dir => $base, @extra_path);
115 }
116  
117  
118 sub check_access {
119 42 ▶ my ($self, $opt)
Subroutine check_access: called CC=16 SCAR=28.3
Pod documented
120 = validate(\@_, class_type(__PACKAGE__),
121 slurpy Dict[
122 user => Maybe[class_type('Yukki::User')],
123 special => Optional[Str],
124 repository => Optional[Str],
125 needs => AccessLevel,
126 ]
127 );
128 my ($user, $repository, $special, $needs)
129 42 = @{$opt}{qw( user repository special needs )};
130  
131 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
132 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
133  
134 # Always grant none
135 42 ▶ return 1 if $needs eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $needs eq "none" 14 28 42
136  
137 my $config = $self->settings->repositories->{$repository}
138 28 ▶ // $self->settings->special_privileges->{$special};
Condition33%1/3
$self->settings->repositories->{$repository} // $self->settings->special_privileges->{$special}
l!l&&r!l&&!r
2800
MC/DC0%0/2
$self->settings->repositories->{$repository} // $self->settings->special_privileges->{$special}
$self->settings->repositories->{$repository} $self->settings->special_privileges->{$special}
Truth table33%1/3
A // B
A= $self->settings->repositories->{$repository}B= $self->settings->special_privileges->{$special}
ABresult
1X1
011
000
139  
140 28 ▶ return '' unless $config;
Branch: 1 branch
BranchTrueFalseTotal
unless $config 0 28 28
141  
142 28 my $read_groups = $config->read_groups;
143 28 my $write_groups = $config->write_groups;
144  
145 28 my %access_level = (none => 0, read => 1, write => 2);
146 my $has_access = sub {
147 48 ▶ $access_level{$_[0] // 'none'} >= $access_level{$needs}
Subroutine __ANON__: called CC=2 SCAR=7.6
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
148 28 };
149  
150 # Deal with anonymous users first.
151 28 ▶ return 1 if $has_access->($config->anonymous_access_level);
Branch: 1 branch
BranchTrueFalseTotal
if &$has_access($config->anonymous_access_level) 6 22 28
152 22 ▶ return '' unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 11 11 22
153  
154 # Only logged users considered here forward.
155 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
156  
157 11 for my $level (qw( read write )) {
158 20 ▶ if ($has_access->($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$has_access($level)) 14 6 20
159  
160 14 my $groups = "${level}_groups";
161  
162 14 ▶ return 1 if $config->$groups eq 'ANY';
Branch: 1 branch
BranchTrueFalseTotal
if $config->$groups eq "ANY" 3 11 14
163  
164 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
165 5 my @level_groups = @{ $config->$groups };
166  
167 5 for my $level_group (@level_groups) {
168 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
169 }
170 }
171 elsif ($config->$groups ne 'NONE') {
172 0 warn "weird value ", $config->$groups,
173 " in $groups config for $repository settings";
174 }
175 }
176 }
177  
178 5 return '';
179 }
180  
181  
182 sub hasher {
183 6 ▶ my $self = shift;
Subroutine hasher: called CC=1 SCAR=0.0
Pod documented
184  
185 6 return Crypt::SaltedHash->new(algorithm => $self->settings->digest);
186 }
187  
188 13 with qw( Yukki::Role::App );
189  
190  
191 13 1;
192  
193 __END__
194  
195 =pod
196  
197 =encoding UTF-8
198  
199 =head1 NAME
200  
201 Yukki - Yet Uh-nother wiki
202  
203 =head1 VERSION
204  
205 version 0.991_002
206  
207 =head1 DESCRIPTION
208  
209 This is intended to be the simplest, stupidest wiki on the planet. It uses git for versioning and it is perfectly safe to clone this repository and push and pull and all that jazz to maintain this wiki in multiple places.
210  
211 For information on getting started see L<Yukki::Manual::Installation>.
212  
213 =head1 WITH ROLES
214  
215 =over
216  
217 =item *
218  
219 L<Yukki::Role::App>
220  
221 =back
222  
223 =head1 ATTRIBUTES
224  
225 =head2 config_file
226  
227 This is the name of the configuraiton file. The application will try to find it in F<etc> within the current working directory first. If not there, it will check the C<YUKKI_CONFIG> environment variable.
228  
229 =head2 settings
230  
231 This is the configuration loaded from the L</config_file>.
232  
233 =head1 METHODS
234  
235 =head2 view
236  
237 my $view = $app->view('Page');
238  
239 Not implemented in this class. See L<Yukki::Web>.
240  
241 =head2 controller
242  
243 my $controller = $app->controller('Page');
244  
245 Not implemented in this class. See L<Yukki::Web>.
246  
247 =head2 model
248  
249 my $model = $app->model('Repository', { repository => 'main' });
250  
251 Returns an instance of the requested model class. The parameters are passed to
252 the instance constructor.
253  
254 =head2 locate
255  
256 my $file = $app->locate('user_path', 'test_user');
257  
258 The first argument is the name of the configuration directive naming the path.
259 It may be followed by one or more path components to be tacked on to the end.
260  
261 Returns a L<Path::Tiny> for the file.
262  
263 =head2 locate_dir
264  
265 my $dir = $app->locate_dir('repository_path', 'main.git');
266  
267 The arguments are identical to L</locate>, but returns a L<Path::Tiny> for
268 the given file.
269  
270 =head2 check_access
271  
272 my $access_is_ok = $app->check_access({
273 user => $user,
274 repository => 'main',
275 needs => 'read',
276 });
277  
278 The C<user> is optional. It should be an object returned from
279 L<Yukki::Model::User>. The C<repository> is required and should be the name of
280 the repository the user is trying to gain access to. The C<needs> is the access
281 level the user needs. It must be an L<Yukki::Types/AccessLevel>.
282  
283 The method returns a true value if access should be granted or false otherwise.
284  
285 =head2 hasher
286  
287 Returns a message digest object that can be used to create a cryptographic hash.
288  
289 =head1 WHY?
290  
291 I wanted a Perl-based, MultiMarkdown-supporting wiki that I could take sermon notes and personal study notes for church and Bible study and such. However, I'm offline at church, so I want to do this from my laptop and sync it up to the master wiki when I get home. That's it.
292  
293 Does it suit your needs? I don't really care, but if I've shared this on the CPAN or the GitHub, then I'm offering it to you in case you might find it useful WITHOUT WARRANTY. If you want it to suit your needs, bug me by email at C<< hanenkamp@cpan.org >> and send me patches.
294  
295 =head1 AUTHOR
296  
297 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
298  
299 =head1 COPYRIGHT AND LICENSE
300  
301 This software is copyright (c) 2017 by Qubling Software LLC.
302  
303 This is free software; you can redistribute it and/or modify it under
304 the same terms as the Perl 5 programming language system itself.
305  
306 =cut
« blib/lib/Yukki/Web/View/Admin/User.pm ↑ Summary blib/lib/Yukki/TemplateUtil.pm »