blib/lib/Yukki.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Yukki/Web.pm ↑ Summary blib/lib/Yukki/Web/View.pm »
1 package Yukki;
2 {
3 $Yukki::VERSION = '0.140290';
4 }
5 4 ▶use 5.12.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Yukki::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Yukki::Types qw( AccessLevel );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 use Crypt::SaltedHash;
15 use MooseX::Params::Validate;
16 use MooseX::Types::Path::Class;
17 use Path::Class;
18 use YAML qw( LoadFile );
19  
20 # ABSTRACT: Yet Uh-nother wiki
21  
22  
23 has config_file => (
24 is => 'ro',
25 isa => 'Path::Class::File',
26 required => 1,
27 coerce => 1,
28 lazy_build => 1,
29 );
30  
31 sub _build_config_file {
32 my $self = shift;
33  
34 my $cwd_conf = file(dir(), 'etc', 'yukki.conf');
35 if (not $ENV{YUKKI_CONFIG} and -f "$cwd_conf") {
36 return $cwd_conf;
37 }
38  
39 die("Please make YUKKI_CONFIG point to your configuration file.\n")
40 unless defined $ENV{YUKKI_CONFIG};
41  
42 die("No configuration found at $ENV{YUKKI_CONFIG}. Please set YUKKI_CONFIG to the correct location.\n")
43 unless -f $ENV{YUKKI_CONFIG};
44  
45 return $ENV{YUKKI_CONFIG};
46 }
47  
48  
49 has settings => (
50 is => 'ro',
51 isa => 'Yukki::Settings',
52 required => 1,
53 coerce => 1,
54 lazy_build => 1,
55 );
56  
57 sub _build_settings {
58 my $self = shift;
59 LoadFile(''.$self->config_file);
60 }
61  
62  
63 sub view { ... }
64  
65  
66 sub controller { ... }
67  
68  
69 sub model {
70 my ($self, $name, $params) = @_;
71 my $class_name = join '::', 'Yukki::Model', $name;
72 Class::Load::load_class($class_name);
73 return $class_name->new(app => $self, %{ $params // {} });
74 }
75  
76  
77 sub _locate {
78 my ($self, $type, $base, @extra_path) = @_;
79  
80 my $path_class = $type eq 'file' ? 'Path::Class::File'
81 : $type eq 'dir' ? 'Path::Class::Dir'
82 : http_throw("unkonwn location type $type");
83  
84 my $base_path = $self->settings->$base;
85 if ($base_path !~ m{^/}) {
86 return $path_class->new($self->settings->root, $base_path, @extra_path);
87 }
88 else {
89 return $path_class->new($base_path, @extra_path);
90 }
91 }
92  
93 sub locate {
94 my ($self, $base, @extra_path) = @_;
95 $self->_locate(file => $base, @extra_path);
96 }
97  
98  
99 sub locate_dir {
100 my ($self, $base, @extra_path) = @_;
101 $self->_locate(dir => $base, @extra_path);
102 }
103  
104  
105 sub check_access {
106 my ($self, $user, $repository, $needs) = validated_list(\@_,
107 user => { isa => 'Undef|HashRef', optional => 1 },
108 repository => { isa => 'Str' },
109 needs => { isa => AccessLevel },
110 );
111  
112 # Always grant none
113 return 1 if $needs eq 'none';
114  
115 my $config = $self->settings->repositories->{$repository};
116  
117 return '' unless $config;
118  
119 my $read_groups = $config->read_groups;
120 my $write_groups = $config->write_groups;
121  
122 my %access_level = (none => 0, read => 1, write => 2);
123 my $has_access = sub {
124 $access_level{$_[0] // 'none'} >= $access_level{$needs}
125 };
126  
127 # Deal with anonymous users first.
128 return 1 if $has_access->($config->anonymous_access_level);
129 return '' unless $user;
130  
131 # Only logged users considered here forward.
132 my @user_groups = @{ $user->{groups} // [] };
133  
134 for my $level (qw( read write )) {
135 if ($has_access->($level)) {
136  
137 my $groups = "${level}_groups";
138  
139 return 1 if $config->$groups ~~ 'ANY';
140  
141 if (ref $config->$groups eq 'ARRAY') {
142 my @level_groups = @{ $config->$groups };
143  
144 for my $level_group (@level_groups) {
145 return 1 if $level_group ~~ @user_groups;
146 }
147 }
148 elsif ($config->$groups ne 'NONE') {
149 warn "weird value ", $config->$groups,
150 " in $groups config for $repository settings";
151 }
152 }
153 }
154  
155 return '';
156 }
157  
158  
159 sub hasher {
160 my $self = shift;
161  
162 return Crypt::SaltedHash->new(algorithm => $self->settings->digest);
163 }
164  
165 with qw( Yukki::Role::App );
166  
167  
168 1;
169  
170 __END__
« blib/lib/Yukki/Web.pm ↑ Summary blib/lib/Yukki/Web/View.pm »