Rubric/User.pm

filter: Statementstmt 100% 107 / 107 Branchbran 91.6% 33 / 36 Conditioncond 100% 21 / 21 MC/DCmcdc 100% 13 / 13 Subroutinesub 100% 17 / 17 Podpod 100% 10 / 10 totaltotal 98.5% 201 / 204 CC 27 SCAR 15.2
CC
27
Cov
98%
related_tags
7.0
quick_entry
7.0
related_tags_counted
7.0
CRAP 4.6
« Rubric/WebApp/URI.pm ↑ Summary Rubric/WebApp/Login.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Rubric::User 0.157;
4 # ABSTRACT: a Rubric user
5  
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod This class provides an interface to Rubric users. It inherits from
9 #pod Rubric::DBI, which is a Class::DBI class.
10 #pod
11 #pod =cut
12  
13 12 ▶use base qw(Rubric::DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 __PACKAGE__->table('users');
18  
19 #pod =head1 COLUMNS
20 #pod
21 #pod username - the user's login name
22 #pod password - the hex md5sum of the user's password
23 #pod email - the user's email address
24 #pod created - the user's date of registration
25 #pod
26 #pod verification_code - the code sent to the user for verification
27 #pod NULL if verified
28 #pod
29 #pod =cut
30  
31 12 __PACKAGE__->columns(
32 All => qw(username password email created verification_code reset_code)
33 );
34  
35 #pod =head1 RELATIONSHIPS
36 #pod
37 #pod =head2 entries
38 #pod
39 #pod Every user has_many entries, which are Rubric::Entry objects. They can be
40 #pod retrieved with the C<entries> accessor, as usual.
41 #pod
42 #pod =cut
43  
44 12 __PACKAGE__->has_many(entries => 'Rubric::Entry' );
45  
46 #pod =head2 tags
47 #pod
48 #pod A user has as "his" tags all the tags that occur on his entries. There exist a
49 #pod number of accessors for his tag list.
50 #pod
51 #pod =head3 tags
52 #pod
53 #pod This returns an arrayref of all the user's (non-system) tags in their database
54 #pod colation order.
55 #pod
56 #pod =cut
57  
58 12 __PACKAGE__->set_sql(tags => <<'' );
59 SELECT DISTINCT tag
60 FROM entrytags
61 WHERE entry IN (SELECT id FROM entries WHERE username = ?)
62 AND tag NOT LIKE '@%%'
63 ORDER BY tag
64  
65 1 ▶sub tags {
Subroutine tags: called CC=1 SCAR=0.0
Pod documented
66 1 my ($self) = @_;
67 1 my $sth = $self->sql_tags;
68 1 $sth->execute($self->username);
69 1 my $tags = $sth->fetchall_arrayref;
70 1 [ map { @$_ } @$tags ];
71 }
72  
73 #pod =head3 tags_counted
74 #pod
75 #pod This returns an arrayref of arrayrefs, each containing a tag name and the
76 #pod number of entries tagged with that tag. The pairs are sorted in colation order
77 #pod by tag name.
78 #pod
79 #pod =cut
80  
81 12 __PACKAGE__->set_sql(tags_counted => <<'' );
82 SELECT DISTINCT tag, COUNT(*) AS count
83 FROM entrytags
84 WHERE entry IN (SELECT id FROM entries WHERE username = ?)
85 AND tag NOT LIKE '@%%'
86 GROUP BY tag
87 ORDER BY tag
88  
89 13 ▶sub tags_counted {
Subroutine tags_counted: called CC=1 SCAR=0.0
Pod documented
90 13 my ($self) = @_;
91 13 my $sth = $self->sql_tags_counted;
92 13 $sth->execute($self->username);
93 13 my $tags = $sth->fetchall_arrayref;
94 13 return $tags;
95 }
96  
97 #pod =head3 related_tags(\@tags, \%context)
98 #pod
99 #pod This method returns a reference to an array of tags related to all the given
100 #pod tags. Tags are related if they occur together on entries.
101 #pod
102 #pod =cut
103  
104 6 ▶sub related_tags {
Subroutine related_tags: called CC=7 SCAR=19.5
Pod documented
105 6 my ($self, $tags, $context) = @_;
106 6 ▶ $tags = [ keys %$tags ] if ref $tags eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tags eq "HASH" 0 6 6
107 6 ▶ return unless $tags and my @tags = @$tags;
Branch: 1 branch
BranchTrueFalseTotal
unless $tags and my(@tags) = @$tags 2 4 6
Condition100%3/3
$tags and my(@tags) = @$tags
!ll&&!rl&&r
114
MC/DC100%2/2
$tags and my(@tags) = @$tags
$tags my(@tags) = @$tags
Truth table100%3/3
A and B
A= $tagsB= my(@tags) = @$tags
ABresult
0X0
100
111
108  
109 # or an exception?
110 4 ▶ return [] if (grep { $_ eq '@private' } @$tags)
Condition100%2/2
$context->{'user'} || ''
l!l
21
MC/DC100%1/1
$context->{'user'} || ''
$context->{'user'}
Truth table100%2/2
A
A= $context->{'user'}
Aresult
11
00
111 4 ▶ and (($context->{user}||'') ne $self->username);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username 2 2 4
Condition100%3/3
grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username
!ll&&!rl&&r
112
MC/DC100%2/2
grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username
grep {$_ eq "\@private";} @$tags ($context->{"user"} || "") ne $self->username
Truth table100%3/3
A and B
A= grep {$_ eq "\@private";} @$tagsB= ($context->{"user"} || "") ne $self->username
ABresult
0X0
100
111
112  
113 my $query = q|
114 SELECT DISTINCT tag FROM entrytags
115 WHERE entry IN (SELECT id FROM entries WHERE username = ?)
116 2 AND tag NOT IN (| . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
117 AND tag NOT LIKE '@%'
118 AND | .
119 join ' AND ',
120 2 map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
121 2 map { $self->db_Main->quote($_) }
122 @tags;
123  
124 2 $self->db_Main->selectcol_arrayref($query, undef, $self->username);
125 }
126  
127 #pod =head3 related_tags_counted(\@tags, \%context)
128 #pod
129 #pod This is the obvious conjunction of C<related_tags> and C<tags_counted>. It
130 #pod returns an arrayref of arrayrefs, each a pair of tag/occurance values.
131 #pod
132 #pod =cut
133  
134 15 ▶sub related_tags_counted {
Subroutine related_tags_counted: called CC=7 SCAR=19.5
Pod documented
135 15 my ($self, $tags, $context) = @_;
136 15 ▶ return unless $tags;
Branch: 1 branch
BranchTrueFalseTotal
unless $tags 8 7 15
137 7 ▶ $tags = [ keys %$tags ] if ref $tags eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $tags eq "HASH" 1 6 7
138 7 ▶ return unless my @tags = @$tags;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@tags) = @$tags 1 6 7
139  
140 # or an exception?
141 6 ▶ return [] if (grep { $_ eq '@private' } @$tags)
Condition100%2/2
$context->{'user'} || ''
l!l
21
MC/DC100%1/1
$context->{'user'} || ''
$context->{'user'}
Truth table100%2/2
A
A= $context->{'user'}
Aresult
11
00
142 6 ▶ and (($context->{user}||'') ne $self->username);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username 2 4 6
Condition100%3/3
grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username
!ll&&!rl&&r
312
MC/DC100%2/2
grep {$_ eq "\@private";} @$tags and ($context->{"user"} || "") ne $self->username
grep {$_ eq "\@private";} @$tags ($context->{"user"} || "") ne $self->username
Truth table100%3/3
A and B
A= grep {$_ eq "\@private";} @$tagsB= ($context->{"user"} || "") ne $self->username
ABresult
0X0
100
111
143  
144 my $query = q|
145 SELECT DISTINCT tag, COUNT(*) AS count
146 FROM entrytags
147 WHERE entry IN (SELECT id FROM entries WHERE username = ?)
148 4 AND tag NOT IN (| . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
149 AND tag NOT LIKE '@%'
150 AND | .
151 join ' AND ',
152 4 map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
153 4 map { $self->db_Main->quote($_) }
154 @tags;
155  
156 4 $query .= " GROUP BY tag";
157  
158 4 $self->db_Main->selectall_arrayref($query, undef, $self->username);
159 }
160  
161 #pod =head1 INFLATIONS
162 #pod
163 #pod =head2 created
164 #pod
165 #pod The created column is stored as seconds since epoch, but inflated to
166 #pod Time::Piece objects.
167 #pod
168 #pod =cut
169  
170 12 __PACKAGE__->has_a(created => 'Time::Piece', deflate => 'epoch');
171  
172 12 __PACKAGE__->add_trigger(before_create => \&_create_times);
173  
174 5 ▶sub _create_times {
Subroutine _create_times: called CC=2 SCAR=6.9
175 5 my $self = shift;
176 5 ▶ $self->created(scalar gmtime) unless defined $self->{created};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"created"} 4 1 5
177 }
178  
179 #pod =head1 METHODS
180 #pod
181 #pod =head2 quick_entry(\%entry)
182 #pod
183 #pod This method creates or updates an entry for the user. The passed entry should
184 #pod include the following data:
185 #pod
186 #pod uri - the URI for the entry
187 #pod tags - the tags for the entry, as a space delimited string
188 #pod title - the title for the entry
189 #pod description - the description for the entry
190 #pod body - the body for the entry
191 #pod
192 #pod If an entry for the link exists, it is updated. Existing tags are replaced
193 #pod with the new tags. If no entry exists, the Rubric::Link is created if needed,
194 #pod and a new entry is then created.
195 #pod
196 #pod The Rubric::Entry object is returned.
197 #pod
198 #pod =cut
199  
200 13 ▶sub quick_entry {
Subroutine quick_entry: called CC=7 SCAR=19.5
Pod documented
201 13 my ($self, $entry) = @_;
202  
203 13 ▶ return unless $entry->{title};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"title"} 1 12 13
204 12 $entry->{tags} = Rubric::Entry->tags_from_string($entry->{tags});
205  
206 12 my $link;
207 12 ▶ if ($entry->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"uri"}) 8 4 12
208 8 $link = eval { Rubric::Link->find_or_create({ uri => $entry->{uri} }) };
209 8 ▶ return unless $link;
Branch: 1 branch
BranchTrueFalseTotal
unless $link 0 8 8
210 }
211  
212 my $new_entry = $entry->{entryid}
213 ? Rubric::Entry->retrieve($entry->{entryid})
214 12 ▶ : $link
Branch: 2 branches
BranchTrueFalseTotal
$entry->{'entryid'} ? : 1 11 12
$link ? : 8 3 11
215 ? Rubric::Entry->find_or_create({ link => $link, username => $self })
216 : Rubric::Entry->create({ username => $self });
217  
218 12 $new_entry->link($link);
219 12 $new_entry->title($entry->{title});
220 12 $new_entry->description($entry->{description});
221 12 ▶ $new_entry->body($entry->{body} || undef);
Condition100%2/2
$entry->{'body'} || undef
l!l
57
MC/DC100%1/1
$entry->{'body'} || undef
$entry->{'body'}
Truth table100%2/2
A
A= $entry->{'body'}
Aresult
11
00
222 12 $new_entry->update;
223 12 $new_entry->set_new_tags($entry->{tags});
224  
225 12 return $new_entry;
226 }
227  
228 #pod =head2 verify($code)
229 #pod
230 #pod If the given code matches this user's C<verification_code>, the user will be
231 #pod verified; that is, his C<verification_code> will be undefined.
232 #pod
233 #pod =cut
234  
235 4 ▶sub verify {
Subroutine verify: called CC=4 SCAR=13.9
Pod documented
236 4 my ($self, $code) = @_;
237  
238 4 ▶ return unless $self->verification_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verification_code 1 3 4
239  
240 3 ▶ if ($code and $code eq $self->verification_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $code eq $self->verification_code) 1 2 3
Condition100%3/3
$code and $code eq $self->verification_code
!ll&&!rl&&r
111
MC/DC100%2/2
$code and $code eq $self->verification_code
$code $code eq $self->verification_code
Truth table100%3/3
A and B
A= $codeB= $code eq $self->verification_code
ABresult
0X0
100
111
241 1 $self->verification_code(undef);
242 1 $self->update;
243 1 return 1;
244 }
245 2 return;
246 }
247  
248 #pod =head2 reset_password($code)
249 #pod
250 #pod If the given code matches this user's C<reset_code>, the user's password will be
251 #pod reset via C<randomize_password> and his reset code will be undefined. If
252 #pod successful, the new password is returned. Otherwise, the routine returns
253 #pod false.
254 #pod
255 #pod =cut
256  
257 3 ▶sub reset_password {
Subroutine reset_password: called CC=4 SCAR=13.9
Pod documented
258 3 my ($self, $code) = @_;
259  
260 3 ▶ return unless $self->reset_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->reset_code 0 3 3
261  
262 3 ▶ if ($code and $code eq $self->reset_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $code eq $self->reset_code) 1 2 3
Condition100%3/3
$code and $code eq $self->reset_code
!ll&&!rl&&r
111
MC/DC100%2/2
$code and $code eq $self->reset_code
$code $code eq $self->reset_code
Truth table100%3/3
A and B
A= $codeB= $code eq $self->reset_code
ABresult
0X0
100
111
263 1 my $password = $self->randomize_password;
264 1 $self->reset_code(undef);
265 1 $self->update;
266 1 return $password;
267 }
268 2 return;
269 }
270  
271 #pod =head2 randomize_password
272 #pod
273 #pod This method resets the user's password to a pseudo-random string and returns
274 #pod the new password.
275 #pod
276 #pod =cut
277  
278 3 ▶sub __random_string {
Subroutine __random_string: called CC=2 SCAR=6.9
279 3 my $length = 15;
280 3 my @legal = ('a'..'z', 'A'..'Z', 0..9);
281 3 my $string = join '', map { @legal[rand @legal] } 1 .. $length;
282  
283 3 ▶ return wantarray ? (md5_hex($string), $string) : md5_hex($string);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
284 }
285  
286 1 ▶sub randomize_password {
Subroutine randomize_password: called CC=1 SCAR=0.0
Pod documented
287 1 my ($self) = @_;
288 1 my ($pass_md5, $password) = $self->__random_string;
289
290 1 $self->password($pass_md5);
291 1 $self->update;
292  
293 1 return $password;
294 }
295  
296 #pod =head2 randomize_reset_code
297 #pod
298 #pod This method resets the user's reset code to the md5sum of a pseudo-random
299 #pod string.
300 #pod
301 #pod =cut
302  
303 1 ▶sub randomize_reset_code {
Subroutine randomize_reset_code: called CC=1 SCAR=0.0
Pod documented
304 1 my ($self) = @_;
305 1 my $reset_code = $self->__random_string;
306 1 $self->reset_code($reset_code);
307 1 $self->update;
308 }
309  
310 #pod =head2 randomize_verification_code
311 #pod
312 #pod This method resets the user's verification code to the md5sum of a
313 #pod pseudo-random string.
314 #pod
315 #pod =cut
316  
317 1 ▶sub randomize_verification_code {
Subroutine randomize_verification_code: called CC=1 SCAR=0.0
Pod documented
318 1 my ($self) = @_;
319 1 my $verification_code = $self->__random_string;
320 1 $self->verification_code($verification_code);
321 1 $self->update;
322 }
323  
324 12 1;
325  
326 __END__
« Rubric/WebApp/URI.pm ↑ Summary Rubric/WebApp/Login.pm »