lib/MojoMojo/Schema/Result/Person.pm

filter: Statementstmt 70.0% 42 / 60 Branchbran 14.2% 2 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 56.2% 9 / 16 Podpod 100% 11 / 11 totaltotal 61.2% 68 / 111 CC 10 SCAR 20.9
CC
10
Cov
59%
can_edit
30.0
pass_matches
6.0
age
6.0
CRAP 8.1
« lib/MojoMojo/Formatter/Dir.pm ↑ Summary lib/MojoMojo/Controller/Attachment.pm »
1 package MojoMojo::Schema::Result::Person;
2  
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 40 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use parent qw/MojoMojo::Schema::Base::Result/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Text::Textile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 my $textile = Text::Textile->new( flavor => "xhtml1", charset => 'utf-8' );
12  
13 40 __PACKAGE__->load_components(
14 qw/DateTime::Epoch TimeStamp EncodedColumn Core/);
15 40 __PACKAGE__->table("person");
16 40 __PACKAGE__->add_columns(
17 "id",
18 {
19 data_type => "INTEGER",
20 is_nullable => 0,
21 size => undef,
22 is_auto_increment => 1
23 },
24
25 "active",
26 # -1 = user registered but hasn't confirmed e-mail address yet;
27 # 0 = manually set to inactive from Site Settings -> Users;
28 # 1 = active user
29 {
30 data_type => "INTEGER",
31 is_nullable => 0,
32 default_value => -1,
33 size => undef
34 },
35
36 "registered",
37  
38 # { data_type => "BIGINT", is_nullable => 0, size => undef, epoch => 'ctime' },
39 {
40 data_type => "BIGINT",
41 is_nullable => 0,
42 size => undef,
43 inflate_datetime => 'epoch',
44 set_on_create => 1,
45 },
46 "views",
47 { data_type => "INTEGER", is_nullable => 1, size => undef },
48 "photo",
49 { data_type => "INTEGER", is_nullable => 1, size => undef },
50 "login",
51 { data_type => "VARCHAR", is_nullable => 0, size => 100 },
52 "name",
53 { data_type => "VARCHAR", is_nullable => 0, size => 100 },
54 "email",
55 { data_type => "VARCHAR", is_nullable => 0, size => 100 },
56 "pass",
57 {
58 data_type => "CHAR",
59 is_nullable => 0,
60 size => 40,
61 encode_column => 1,
62 encode_class => 'Digest',
63 encode_args => { algorithm => 'SHA-1', format => 'hex' },
64 encode_check_method => 'check_password',
65 },
66 "timezone",
67 { data_type => "VARCHAR", is_nullable => 1, size => 100 },
68 "born",
69 {
70 data_type => "BIGINT",
71 is_nullable => 1,
72 size => undef,
73 default_value => undef,
74 # epoch => 1,
75 inflate_datetime => 'epoch',
76 datetime_undef_if_invalid => 1,
77 },
78 "gender",
79 { data_type => "CHAR", is_nullable => 1, size => 1 },
80 "occupation",
81 { data_type => "VARCHAR", is_nullable => 1, size => 100 },
82 "industry",
83 { data_type => "VARCHAR", is_nullable => 1, size => 100 },
84 "interests",
85 { data_type => "TEXT", is_nullable => 1, size => undef },
86 "movies",
87 { data_type => "TEXT", is_nullable => 1, size => undef },
88 "music",
89 { data_type => "TEXT", is_nullable => 1, size => undef },
90 );
91 40 __PACKAGE__->set_primary_key("id");
92 40 __PACKAGE__->add_unique_constraint( login => [qw/login/], );
93 40 __PACKAGE__->add_unique_constraint( email => [qw/email/], );
94 40 __PACKAGE__->has_many(
95 "entries",
96 "MojoMojo::Schema::Result::Entry",
97 { "foreign.author" => "self.id" }
98 );
99 40 __PACKAGE__->has_many(
100 "tags",
101 "MojoMojo::Schema::Result::Tag",
102 { "foreign.person" => "self.id" }
103 );
104 40 __PACKAGE__->has_many(
105 "comments",
106 "MojoMojo::Schema::Result::Comment",
107 { "foreign.poster" => "self.id" }
108 );
109 40 __PACKAGE__->has_many(
110 "role_members",
111 "MojoMojo::Schema::Result::RoleMember",
112 { "foreign.person" => "self.id" },
113 );
114 40 __PACKAGE__->has_many(
115 "page_versions",
116 "MojoMojo::Schema::Result::PageVersion",
117 { "foreign.creator" => "self.id" },
118 );
119 40 __PACKAGE__->many_to_many( roles => 'role_members', 'role' );
120 40 __PACKAGE__->has_many(
121 "contents",
122 "MojoMojo::Schema::Result::Content",
123 { "foreign.creator" => "self.id" }
124 );
125  
126 =head1 NAME
127  
128 MojoMojo::Schema::Result::Person - store user info
129  
130 =head1 METHODS
131  
132 =head2 is_admin
133  
134 Checks if user belongs to list of admins.
135  
136 =cut
137  
138 120 ▶sub is_admin {
Subroutine is_admin: called CC=3 SCAR=11.0
Pod documented
139 120 my $self = shift;
140 120 my $admins = MojoMojo->pref('admins');
141 120 my $login = $self->login;
142 120 ▶ return 1 if $login && $admins =~ m/\b$login\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $login and $admins =~ /\b$login\b/ 105 15 120
Condition66%2/3
$login and $admins =~ /\b$login\b/
!ll&&!rl&&r
015105
MC/DC50%1/2
$login and $admins =~ /\b$login\b/
$login $admins =~ /\b$login\b/
Truth table66%2/3
A and B
A= $loginB= $admins =~ /\b$login\b/
ABresult
0X0
100
111
143 15 return 0;
144 }
145  
146 =head2 link
147  
148 Returns a relative link to the user's home node.
149  
150 =cut
151  
152 90 ▶sub link {
Subroutine link: called CC=2 SCAR=8.1
Pod documented
153 90 my ($self) = @_;
154 90 ▶ return lc "/" . ( $self->login || MojoMojo->pref('anonymous_user') );
Condition33%1/3
$self->login || 'MojoMojo'->pref('anonymous_user')
l!l&&r!l&&!r
9000
MC/DC0%0/2
$self->login || 'MojoMojo'->pref('anonymous_user')
$self->login 'MojoMojo'->pref('anonymous_user')
Truth table33%1/3
A || B
A= $self->loginB= 'MojoMojo'->pref('anonymous_user')
ABresult
1X1
011
000
155 }
156  
157 =head2 can_edit <path>
158  
159 Checks if a user has rights to edit a given path.
160  
161 =cut
162  
163 0 ▶sub can_edit {
Subroutine can_edit: not called CC=5 SCAR=34.0
Pod documented
164 0 my ( $self, $page ) = @_;
165 0 ▶ return 0 unless $self->active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->active 0 0 0
166  
167 # allow admins
168 0 ▶ return 1 if $self->is_admin;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_admin 0 0 0
169  
170 # allow edit unless users are restricted to home page
171 0 ▶ return 1 unless MojoMojo->pref('restricted_user');
Branch: 1 branch
BranchTrueFalseTotal
unless "MojoMojo"->pref("restricted_user") 0 0 0
172  
173 # allow users editing their pages
174 0 my $link = $self->link;
175 0 ▶ return 1 if $page =~ m|^$link\b|i;
Branch: 1 branch
BranchTrueFalseTotal
if $page =~ /^$link\b/i 0 0 0
176 0 return 0;
177 }
178  
179 =head2 pages
180  
181 Return the pages created by the user.
182  
183 =cut
184  
185 6 ▶sub pages {
Subroutine pages: called CC=1 SCAR=0.0
Pod documented
186 6 my ($self) = @_;
187 6 my @pages =
188 $self->result_source->related_source('page_versions')
189 ->related_source('page')->resultset->search(
190 { 'versions.creator' => $self->id, },
191 {
192 join => [qw/versions/],
193 order_by => ['me.name'],
194 distinct => 1,
195 }
196 )->all;
197 6 return $self->result_source->related_source('page_versions')
198 ->related_source('page')->resultset->set_paths(@pages);
199 }
200  
201 =head2 pass_matches <pass1> <pass2>
202  
203 Returns true if pass1 eq pass2.
204  
205 =cut
206  
207 0 ▶sub pass_matches {
Subroutine pass_matches: not called CC=2 SCAR=17.9
Pod documented
208 0 ▶ return 1 if ( $_[0] eq $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_[1] 0 0 0
209 0 return 0;
210 }
211  
212 =head2 valid_pass <password>
213  
214 Check password against database.
215  
216 =cut
217  
218 0 ▶sub valid_pass {
Subroutine valid_pass: not called CC=1 SCAR=6.9
Pod documented
219 0 my ( $self, $pass ) = @_;
220 0 return $self->check_password($pass);
221 }
222  
223 =head2 hashed
224  
225 Apply a SHA1 hash to the input string.
226  
227 =cut
228  
229 1 ▶sub hashed {
Subroutine hashed: called CC=1 SCAR=0.0
Pod documented
230 1 my ( $self, $secret ) = @_;
231 1 return Digest::SHA::sha1_hex( $self->id . $secret );
232 }
233  
234 # FIXME: the formatter is arbitrarily taken to be Textile; it could be MultiMarkdown
235 # http://github.com/marcusramberg/mojomojo/issues/#issue/29
236  
237 =head2 interests_formatted
238  
239 Format a person's interests.
240  
241 =cut
242  
243 0 ▶sub interests_formatted { $textile->process( shift->interests ); }
Subroutine interests_formatted: not called CC=1 SCAR=6.9
Pod documented
244  
245 =head2 music_formatted
246  
247 Format a person's music preferences.
248  
249 =cut
250 0 ▶sub music_formatted { $textile->process( shift->music ); }
Subroutine music_formatted: not called CC=1 SCAR=6.9
Pod documented
251  
252 =head2 movies_formatted
253  
254 Format a person's movie tastes.
255  
256 =cut
257  
258 0 ▶sub movies_formatted { $textile->process( shift->movies ); }
Subroutine movies_formatted: not called CC=1 SCAR=6.9
Pod documented
259  
260 =head2 age
261  
262 Returns age of the user in years.
263  
264 =cut
265  
266 0 ▶sub age {
Subroutine age: not called CC=2 SCAR=17.9
Pod documented
267 0 my ($self) = @_;
268 0 ▶ if ( my $birthdate = $self->born ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $birthdate = $self->born) 0 0 0
269 0 my $diff = DateTime->now - $birthdate;
270 0 return $diff->years;
271 }
272 }
273  
274 =head1 AUTHOR
275  
276 Marcus Ramberg <mramberg@cpan.org>
277  
278 =head1 AUTHOR
279  
280 Marcus Ramberg <mramberg@cpan.org>
281  
282 =head1 LICENSE
283  
284 This library is free software. You can redistribute it and/or modify
285 it under the same terms as Perl itself.
286  
287 =cut
288  
289 40 1;
« lib/MojoMojo/Formatter/Dir.pm ↑ Summary lib/MojoMojo/Controller/Attachment.pm »