Directory.pm

filter: Statementstmt 82.2% 209 / 254 Branchbran 56.3% 62 / 110 Conditioncond 61.3% 27 / 44 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 95.1% 39 / 41 Podpod 92.3% 12 / 13 totaltotal 73.8% 359 / 486 CC 76 SCAR 22.8
CC
76
Cov
73%
_find_gplus
42.0
__ANON__
33.0
tradition
13.0
CRAP 9.8
↑ Summary User.pm »
1 package Text::Tradition::Directory;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Encode qw/ encode decode_utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use KiokuDB::GC::Naive;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use KiokuDB::TypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use KiokuDB::TypeMap::Entry::Naive;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Text::Tradition::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ## users
15 4 ▶use KiokuX::User::Util qw(crypt_password);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Text::Tradition::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Text::Tradition::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Text::Tradition::TypeMap::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 extends 'KiokuX::Model';
21  
22 4 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 $VERSION = "1.3";
24  
25 =head1 NAME
26  
27 Text::Tradition::Directory - a KiokuDB interface for storing and retrieving
28 traditions and their owners
29  
30 =head1 SYNOPSIS
31  
32 use Text::Tradition::Directory;
33 my $d = Text::Tradition::Directory->new(
34 'dsn' => 'dbi:SQLite:mytraditions.db',
35 'extra_args' => { 'create' => 1 },
36 );
37
38 my $tradition = Text::Tradition->new( @args );
39 my $stemma = $tradition->add_stemma( dotfile => $dotfile ); # if Analysis module installed
40 $d->save_tradition( $tradition );
41
42 foreach my $id ( $d->traditions ) {
43 print $d->tradition( $id )->name;
44 }
45  
46 ## Users:
47 my $userstore = Text::Tradition::UserStore->new(dsn => 'dbi:SQLite:foo.db');
48 my $newuser = $userstore->add_user({ username => 'fred',
49 password => 'somepassword' });
50  
51 my $fetchuser = $userstore->find_user({ username => 'fred' });
52 if($fetchuser->check_password('somepassword')) {
53 ## login user or .. whatever
54 }
55  
56 my $user = $userstore->deactivate_user({ username => 'fred' });
57 if(!$user->active) {
58 ## shouldnt be able to login etc
59 }
60
61 =head1 DESCRIPTION
62  
63 Text::Tradition::Directory is an interface for storing and retrieving text
64 traditions and all their data, including an associated stemma hypothesis
65 and a user who has ownership rights to the tradition data. It is an
66 instantiation of a KiokuDB::Model, storing traditions and associated
67 stemmas by UUID.
68  
69 The Text::Tradition::Directory package also includes the
70 L<Text::Tradition::User> class for user objects, and the
71 L<Text::Tradition::Ownership> role which extends the Text::Tradition class
72 to handle user ownership.
73  
74 =head1 ATTRIBUTES
75  
76 =head2 MIN_PASS_LEN
77  
78 Constant for the minimum password length when validating passwords,
79 defaults to "8".
80  
81 =cut
82  
83 2 ▶has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 =head1 METHODS
86  
87 =head2 new
88  
89 Returns a Directory object.
90  
91 =head2 traditionlist
92  
93 Returns a hashref mapping of ID => name for all traditions in the directory.
94  
95 =head2 tradition( $id )
96  
97 Returns the Text::Tradition object of the given ID.
98  
99 =head2 save( $tradition )
100  
101 Writes the given tradition to the database, returning its ID.
102  
103 =head2 delete( $tradition )
104  
105 Deletes the given tradition object from the database.
106 WARNING!! Garbage collection does not yet work. Use this sparingly.
107  
108 =begin testing
109  
110 use TryCatch;
111 use File::Temp;
112 use Safe::Isa;
113 use Text::Tradition;
114 use_ok 'Text::Tradition::Directory';
115  
116 my $fh = File::Temp->new();
117 my $file = $fh->filename;
118 $fh->close;
119 my $dsn = "dbi:SQLite:dbname=$file";
120 my $uuid;
121 my $user = 'user@example.org';
122 my $t = Text::Tradition->new(
123 'name' => 'inline',
124 'input' => 'Tabular',
125 'file' => 't/data/simple.txt',
126 );
127 my $stemma_enabled = $t->can( 'add_stemma' );
128  
129 {
130 my $d = Text::Tradition::Directory->new( 'dsn' => $dsn,
131 'extra_args' => { 'create' => 1 } );
132 ok( $d->$_isa('Text::Tradition::Directory'), "Got directory object" );
133
134 my $scope = $d->new_scope;
135 $uuid = $d->save( $t );
136 ok( $uuid, "Saved test tradition" );
137
138 # Add a test user
139 my $user = $d->add_user({ username => $user, password => 'UserPass' });
140 $user->add_tradition( $t );
141 $d->store( $user );
142 is( $t->user, $user, "Assigned tradition to test user" );
143
144 SKIP: {
145 skip "Analysis package not installed", 5 unless $stemma_enabled;
146 my $s = $t->add_stemma( dotfile => 't/data/simple.dot' );
147 ok( $d->save( $t ), "Updated tradition with stemma" );
148 is( $d->tradition( $uuid ), $t, "Correct tradition returned for id" );
149 is( $d->tradition( $uuid )->stemma(0), $s, "...and it has the correct stemma" );
150 try {
151 $d->save( $s );
152 } catch( Text::Tradition::Error $e ) {
153 is( $e->ident, 'database error', "Got exception trying to save stemma directly" );
154 like( $e->message, qr/Cannot directly save non-Tradition object/,
155 "Exception has correct message" );
156 }
157 }
158 }
159 my $nt = Text::Tradition->new(
160 'name' => 'CX',
161 'input' => 'CollateX',
162 'file' => 't/data/Collatex-16.xml',
163 );
164 ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
165  
166 {
167 my $f = Text::Tradition::Directory->new( 'dsn' => $dsn );
168 my $scope = $f->new_scope;
169 is( scalar $f->traditionlist, 1, "Directory index has our tradition" );
170 my $nuuid = $f->save( $nt );
171 ok( $nuuid, "Stored second tradition" );
172 my @tlist = $f->traditionlist;
173 is( scalar @tlist, 2, "Directory index has both traditions" );
174 my $tf = $f->tradition( $uuid );
175 my( $tlobj ) = grep { $_->{'id'} eq $uuid } @tlist;
176 is( $tlobj->{'name'}, $tf->name, "Directory index has correct tradition name" );
177 is( $tf->name, $t->name, "Retrieved the tradition from a new directory" );
178 my $sid;
179 SKIP: {
180 skip "Analysis package not installed", 4 unless $stemma_enabled;
181 $sid = $f->object_to_id( $tf->stemma(0) );
182 try {
183 $f->tradition( $sid );
184 } catch( Text::Tradition::Error $e ) {
185 is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
186 like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
187 }
188 if( $ENV{TEST_DELETION} ) {
189 try {
190 $f->delete( $sid );
191 } catch( Text::Tradition::Error $e ) {
192 is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
193 like( $e->message, qr/Cannot directly delete non-Tradition object/,
194 "Exception has correct message" );
195 }
196 }
197 }
198
199 SKIP: {
200 skip "Set TEST_DELETION in env to test DB deletion functionality", 3
201 unless $ENV{TEST_DELETION};
202 $f->delete( $uuid );
203 ok( !$f->exists( $uuid ), "Object is deleted from DB" );
204 ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
205 is( scalar $f->traditionlist, 1, "Object is deleted from index" );
206 }
207 }
208  
209 {
210 my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
211 my $scope = $g->new_scope;
212 SKIP: {
213 skip "Set TEST_DELETION in env to test DB deletion functionality", 1
214 unless $ENV{TEST_DELETION};
215 is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
216 }
217 my $ntobj = $g->tradition( 'CX' );
218 my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
219 my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
220 is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" );
221 }
222  
223 =end testing
224  
225 =cut
226 4 ▶use Text::Tradition::TypeMap::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 has +typemap => (
229 is => 'rw',
230 isa => 'KiokuDB::TypeMap',
231 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
232 2 KiokuDB::TypeMap->new(
233 isa_entries => {
234 # now that we fall back to YAML deflation, all attributes of
235 # Text::Tradition will be serialized to YAML as individual objects
236 # Except if we declare a specific entry type here
237 "Text::Tradition" =>
238 KiokuDB::TypeMap::Entry::MOP->new(),
239 # We need users to be naive entries so that they hold
240 # references to the original tradition objects, not clones
241 "Text::Tradition::User" =>
242 KiokuDB::TypeMap::Entry::MOP->new(),
243 "Text::Tradition::Collation" =>
244 KiokuDB::TypeMap::Entry::MOP->new(),
245 "Text::Tradition::Witness" =>
246 KiokuDB::TypeMap::Entry::MOP->new(),
247 "Graph" => Text::Tradition::TypeMap::Entry->new(),
248 "Set::Scalar" => Text::Tradition::TypeMap::Entry->new(),
249 }
250 );
251 },
252 4 );
253  
254 4 has '_mysql_utf8_hack' => (
255 is => 'ro',
256 isa => 'Bool',
257 default => undef,
258 );
259  
260 # Push some columns into the extra_args
261 2 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=8 SCAR=23.4
262 2 my $orig = shift;
263 2 my $class = shift;
264 2 my $args;
265 2 ▶ if( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 1 2
266 1 $args = $_[0];
267 } else {
268 1 $args = { @_ };
269 }
270 2 my @column_args;
271 2 ▶ if( $args->{'dsn'} =~ /^dbi:(\w+):/ ) { # We're using Backend::DBI
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"dsn"} =~ /^dbi:(\w+):/) 2 0 2
272 2 my $dbtype = $1;
273 2 @column_args = ( 'columns',
274 [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 },
275 'public' => { 'data_type' => 'bool', 'is_nullable' => 1 } ] );
276 2 ▶ if( $dbtype eq 'mysql' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($dbtype eq "mysql" and exists $args->{"extra_args"}{"dbi_attrs"} and $args->{"extra_args"}{"dbi_attrs"}{"mysql_enable_utf8"}) 0 2 2
Condition33%2/6
$dbtype eq "mysql" and exists $args->{"extra_args"}{"dbi_attrs"} and $args->{"extra_args"}{"dbi_attrs"}{"mysql_enable_utf8"}
!ll&&!rl&&r
200
$dbtype eq "mysql" and exists $args->{"extra_args"}{"dbi_attrs"}
!ll&&!rl&&r
200
MC/DC0%0/3
$dbtype eq "mysql" and exists $args->{"extra_args"}{"dbi_attrs"} and $args->{"extra_args"}{"dbi_attrs"}{"mysql_enable_utf8"}
$dbtype eq "mysql" exists $args->{"extra_args"}{"dbi_attrs"} $args->{"extra_args"}{"dbi_attrs"}{"mysql_enable_utf8"}
Truth table25%1/4
A and B and C
A= $dbtype eq "mysql"B= exists $args->{"extra_args"}{"dbi_attrs"}C= $args->{"extra_args"}{"dbi_attrs"}{"mysql_enable_utf8"}
ABCresult
0XX0
10X0
1100
1111
277 exists $args->{extra_args}->{dbi_attrs} &&
278 $args->{extra_args}->{dbi_attrs}->{mysql_enable_utf8} ) {
279 # There is a bad interaction with MySQL in utf-8 mode.
280 # Work around it here.
281 # TODO fix the underlying storage problem
282 0 $args->{extra_args}->{dbi_attrs}->{mysql_enable_utf8} = undef;
283 0 $args->{_mysql_utf8_hack} = 1;
284 }
285 }
286 2 my $ea = $args->{'extra_args'};
287 2 ▶ if( ref( $ea ) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ea eq 'ARRAY') { } 0 2 2
elsif (ref $ea eq 'HASH') { } 2 0 2
288 0 push( @$ea, @column_args );
289 } elsif( ref( $ea ) eq 'HASH' ) {
290 2 $ea = { %$ea, @column_args };
291 } else {
292 0 $ea = { @column_args };
293 }
294 2 $args->{'extra_args'} = $ea;
295  
296 2 return $class->$orig( $args );
297 4 };
298  
299 2 ▶override _build_directory => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
300 2 my($self) = @_;
301 2 Text::Tradition::Store->connect(@{ $self->_connect_args },
302 2 ▶ resolver_constructor => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
303 2 my($class) = @_;
304 2 $class->new({ typemap => $self->directory->merged_typemap,
305 fallback_entry => Text::Tradition::TypeMap::Entry->new() });
306 2 });
307 4 };
308  
309 ## These checks don't cover store($id, $obj)
310 # before [ qw/ store update insert delete / ] => sub {
311 2 ▶before [ qw/ delete / ] => sub {
Subroutine __ANON__: called CC=9 SCAR=35.0
312 2 my $self = shift;
313 2 my @nontrad;
314 2 foreach my $obj ( @_ ) {
315 1 ▶ if( ref( $obj ) && !$obj->$_isa( 'Text::Tradition' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition") and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition::User")) 0 1 1
Condition33%2/6
ref $obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition") and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition::User")
!ll&&!rl&&r
010
ref $obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")
!ll&&!rl&&r
001
MC/DC0%0/3
ref $obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition") and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition::User")
ref $obj not $obj->$Text::Tradition::Directory::_isa("Text::Tradition") not $obj->$Text::Tradition::Directory::_isa("Text::Tradition::User")
Truth table25%1/4
A and not(B) and not(C)
A= ref $objB= not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")C= not $obj->$Text::Tradition::Directory::_isa("Text::Tradition::User")
ABCresult
0XX0
10X0
1100
1111
316 && !$obj->$_isa('Text::Tradition::User') ) {
317 # Is it an id => Tradition hash?
318 0 ▶ if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH" and keys %$obj == 1) 0 0 0
Condition0%0/3
ref $obj eq "HASH" and keys %$obj == 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $obj eq "HASH" and keys %$obj == 1
ref $obj eq "HASH" keys %$obj == 1
Truth table0%0/3
A and B
A= ref $obj eq "HASH"B= keys %$obj == 1
ABresult
0X0
100
111
319 0 my( $k ) = keys %$obj;
320 0 ▶ next if $obj->{$k}->$_isa('Text::Tradition');
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{$k}->$Text::Tradition::Directory::_isa("Text::Tradition") 0 0 0
321 }
322 0 push( @nontrad, $obj );
323 }
324 }
325 2 ▶ if( @nontrad ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nontrad) 0 2 2
326 0 throw( "Cannot directly save non-Tradition object of type "
327 . ref( $nontrad[0] ) );
328 }
329 4 };
330  
331 # TODO Garbage collection doesn't work. Suck it up and live with the
332 # inflated DB.
333 2 ▶after delete => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
334 2 my $self = shift;
335 2 my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
336 2 $self->directory->backend->delete( $gc->garbage->members );
337 4 };
338  
339 6 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
340 6 my $self = shift;
341 6 return $self->store( @_ );
342 }
343  
344 4 ▶sub tradition {
Subroutine tradition: called CC=6 SCAR=25.6
Pod documented
345 4 my( $self, $id ) = @_;
346 4 my $obj = $self->lookup( $id );
347 4 ▶ unless( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 4 4
348 # Try looking up by name.
349 0 foreach my $item ( $self->traditionlist ) {
350 0 ▶ if( $item->{'name'} eq $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"name"} eq $id) 0 0 0
351 0 $obj = $self->lookup( $item->{'id'} );
352 0 last;
353 }
354 }
355 }
356 4 ▶ if( $obj && !$obj->$_isa('Text::Tradition') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")) 0 4 4
Condition33%1/3
$obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")
!ll&&!rl&&r
040
MC/DC0%0/2
$obj and not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")
$obj not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")
Truth table33%1/3
A and not(B)
A= $objB= not $obj->$Text::Tradition::Directory::_isa("Text::Tradition")
ABresult
0X0
100
111
357 0 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
358 }
359 4 return $obj;
360 }
361  
362 8 ▶sub traditionlist {
Subroutine traditionlist: called CC=2 SCAR=6.9
Pod documented
363 8 my $self = shift;
364 8 my ($user) = @_;
365  
366 8 ▶ return $self->user_traditionlist($user) if($user);
Branch: 1 branch
BranchTrueFalseTotal
if $user 5 3 8
367 3 return $self->_get_object_idlist( 'Text::Tradition' );
368 }
369  
370 3 ▶sub _get_object_idlist {
Subroutine _get_object_idlist: called CC=7 SCAR=21.9
371 3 my( $self, $objclass ) = @_;
372 3 my @tlist;
373 # If we are using DBI, we can do it the easy way; if not, the hard way.
374 # Easy way still involves making a separate DBI connection. Ew.
375 3 ▶ if( $self->dsn =~ /^dbi:(\w+):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dsn =~ /^dbi:(\w+):/) { } 3 0 3
376 3 my $dbtype = $1;
377 3 my @connection = @{$self->directory->backend->connect_info};
378 # Get rid of KiokuDB-specific arg
379 3 ▶ pop @connection if scalar @connection > 4;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @connection > 4 3 0 3
380 3 ▶ $connection[3]->{'sqlite_unicode'} = 1 if $dbtype eq 'SQLite';
Branch: 1 branch
BranchTrueFalseTotal
if $dbtype eq "SQLite" 3 0 3
381 3 ▶ $connection[3]->{'pg_enable_utf8'} = 1 if $dbtype eq 'Pg';
Branch: 1 branch
BranchTrueFalseTotal
if $dbtype eq "Pg" 0 3 3
382 3 my $dbh = DBI->connect( @connection );
383 3 my $q = $dbh->prepare( 'SELECT id, name, public from entries WHERE class = "'
384 . $objclass . '"' );
385 3 $q->execute();
386 3 ▶ while( my @row = $q->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@row) = $q->fetchrow_array) 16 3 19
387 # Horrible horrible hack. Re-convert the name to UTF-8.
388 16 ▶ if( $self->_mysql_utf8_hack ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mysql_utf8_hack) 0 16 16
389 # Convert the chars into a raw bytestring.
390 0 my $octets = encode( 'ISO-8859-1', $row[1] );
391 0 $row[1] = decode_utf8( $octets );
392 }
393 16 push( @tlist, { 'id' => $row[0], 'name' => $row[1], 'public' => $row[2] } );
394 }
395 } else {
396 0 ▶ $self->scan( sub { my $o = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
397 0 ▶ push( @tlist, { 'id' => $self->object_to_id( $o ),
Branch: 1 branch
BranchTrueFalseTotal
if ref $o eq $objclass 0 0 0
398 'name' => $o->name,
399 'public' => $o->public } )
400 0 if( ref $o eq $objclass ) } );
401 }
402 3 return @tlist;
403 }
404  
405 1 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
Pod undocumented
406 1 Text::Tradition::Error->throw(
407 'ident' => 'database error',
408 'message' => $_[0],
409 );
410 }
411  
412  
413 # has 'directory' => (
414 # is => 'rw',
415 # isa => 'KiokuX::Model',
416 # handles => []
417 # );
418  
419 ## TODO: Some of these methods should probably optionally take $user objects
420 ## instead of hashrefs.
421  
422 ## It also occurs to me that all these methods don't need to be named
423 ## XX_user, but leaving that way for now incase we merge this code
424 ## into ::Directory for one-store.
425  
426 =head1 USER DIRECTORY METHODS
427  
428 =head2 add_user( $userinfo )
429  
430 Takes a hashref of C<username>, C<password>.
431  
432 Create a new user object, store in the KiokuDB backend, and return it.
433  
434 =cut
435  
436 12 ▶sub add_user {
Subroutine add_user: called CC=10 SCAR=23.4
Pod documented
437 12 my ($self, $userinfo) = @_;
438  
439 12 my $username = $userinfo->{username};
440 12 my $password = $userinfo->{password};
441 12 ▶ my $role = $userinfo->{role} || 'user';
Condition100%2/2
$userinfo->{'role'} || 'user'
l!l
111
MC/DC100%1/1
$userinfo->{'role'} || 'user'
$userinfo->{'role'}
Truth table100%2/2
A
A= $userinfo->{'role'}
Aresult
11
00
442  
443 12 ▶ if ($userinfo->{sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo->{"sub"}) 0 12 12
444 0 $username = $userinfo->{sub};
445 }
446  
447 12 ▶ throw( "No username given" ) unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 12 12
448 throw( "Invalid password - must be at least " . $self->MIN_PASS_LEN
449 . " characters long" )
450 12 ▶ unless ( $self->validate_password($password) || $username =~ /^https?:/ || exists ($userinfo->{openid_id}) || exists ($userinfo->{sub}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_password($password) or $username =~ /^https?:/ or exists $userinfo->{"openid_id"} or exists $userinfo->{"sub"} 1 11 12
Condition77%7/9
$self->validate_password($password) or $username =~ /^https?:/ or exists $userinfo->{"openid_id"} or exists $userinfo->{"sub"}
l!l&&r!l&&!r
1101
$self->validate_password($password) or $username =~ /^https?:/ or exists $userinfo->{"openid_id"}
l!l&&r!l&&!r
1101
$self->validate_password($password) or $username =~ /^https?:/
l!l&&r!l&&!r
831
MC/DC50%2/4
$self->validate_password($password) or $username =~ /^https?:/ or exists $userinfo->{"openid_id"} or exists $userinfo->{"sub"}
$self->validate_password($password) $username =~ /^https?:/ exists $userinfo->{"openid_id"} exists $userinfo->{"sub"}
Truth table60%3/5
A or B or C or D
A= $self->validate_password($password)B= $username =~ /^https?:/C= exists $userinfo->{"openid_id"}D= exists $userinfo->{"sub"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
451  
452 my $user = Text::Tradition::User->new(
453 id => $username,
454 password => ($password ? crypt_password($password) : ''),
455 11 ▶ email => ($userinfo->{email} ? $userinfo->{email} : $username),
Branch: 2 branches
BranchTrueFalseTotal
$password ? : 8 3 11
$userinfo->{'email'} ? : 2 9 11
456 role => $role,
457 );
458  
459 11 $self->store($user->kiokudb_object_id, $user);
460  
461 11 return $user;
462 }
463  
464 =head2 create_user( $userinfo )
465  
466 Takes a hashref that can either be suitable for add_user (see above) or be
467 a hash of OpenID user information from Credential::OpenID.
468  
469 =cut
470  
471 3 ▶sub create_user {
Subroutine create_user: called CC=2 SCAR=7.0
Pod documented
472 3 my ($self, $userinfo) = @_;
473  
474 ## No username means probably an OpenID based user
475 3 ▶ if(!exists $userinfo->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $userinfo->{"username"}) 3 0 3
476 3 _extract_openid_data($userinfo);
477 }
478  
479 3 return $self->add_user($userinfo);
480 }
481  
482 ## Not quite sure where this method should be.. Auth /
483 ## Credential::OpenID just pass us back the chunk of extension data
484 5 ▶sub _extract_openid_data {
Subroutine _extract_openid_data: called CC=4 SCAR=14.0
485 5 my ($userinfo) = @_;
486  
487 ## Spec says SHOULD use url as identifier
488 5 $userinfo->{username} = $userinfo->{url};
489  
490 ## Use email addy as display if available
491 5 ▶ if(exists $userinfo->{extensions} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $userinfo->{"extensions"} and exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"} and defined $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}{"value.email"}) 2 3 5
Condition66%4/6
exists $userinfo->{"extensions"} and exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"} and defined $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}{"value.email"}
!ll&&!rl&&r
302
exists $userinfo->{"extensions"} and exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}
!ll&&!rl&&r
302
MC/DC33%1/3
exists $userinfo->{"extensions"} and exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"} and defined $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}{"value.email"}
exists $userinfo->{"extensions"} exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"} defined $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}{"value.email"}
Truth table50%2/4
A and B and C
A= exists $userinfo->{"extensions"}B= exists $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}C= defined $userinfo->{"extensions"}{"http://openid.net/srv/ax/1.0"}{"value.email"}
ABCresult
0XX0
10X0
1100
1111
492 exists $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'} &&
493 defined $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'}) {
494 ## Somewhat ugly attribute extension reponse, contains
495 ## google-email string which we can use as the id
496  
497 2 $userinfo->{email} = $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'};
498 }
499  
500 5 return;
501 }
502  
503 =head2 find_user( $userinfo )
504  
505 Takes a hashref of C<username> or C<email>, and possibly openIDish results from
506 L<Net::OpenID::Consumer>.
507  
508 Fetches the user object for the given username and returns it.
509  
510 =cut
511  
512 13 ▶sub find_user {
Subroutine find_user: called CC=8 SCAR=21.1
Pod documented
513 13 my ($self, $userinfo) = @_;
514  
515 ## A URL field means probably an OpenID based user
516 13 ▶ if( exists $userinfo->{url} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $userinfo->{"url"}) 2 11 13
517 2 _extract_openid_data($userinfo);
518 }
519  
520 13 ▶ if (exists $userinfo->{sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $userinfo->{"sub"}) 0 13 13
521 0 return $self->_find_gplus($userinfo);
522 }
523  
524 13 my $user;
525 13 ▶ if( exists $userinfo->{username} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $userinfo->{'username'}) { } 12 1 13
elsif (exists $userinfo->{'email'}) { } 1 0 1
526 12 my $username = $userinfo->{username};
527 ## No logins if user is deactivated (use lookup to fetch to re-activate)
528 12 $user = $self->lookup(Text::Tradition::User->id_for_user($username));
529 ## If there is an inactive user, skip it
530 12 ▶ return if( $user && !$user->active );
Branch: 1 branch
BranchTrueFalseTotal
if $user and not $user->active 1 11 12
Condition100%3/3
$user and not $user->active
!ll&&!rl&&r
1101
MC/DC100%2/2
$user and not $user->active
$user not $user->active
Truth table100%3/3
A and not(B)
A= $userB= not $user->active
ABresult
0X0
100
111
531 } elsif( exists $userinfo->{email} ) {
532 ## Scan the users looking for a matching email
533 1 my @matches;
534 15 ▶ $self->scan( sub { push( @matches, @_ )
Subroutine __ANON__: called CC=3 SCAR=11.0
535 if $_[0]->isa('Text::Tradition::User')
536 1 ▶ && $_[0]->email eq $userinfo->{email} } );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->isa("Text::Tradition::User") and $_[0]->email eq $userinfo->{"email"} 1 14 15
Condition100%3/3
$_[0]->isa("Text::Tradition::User") and $_[0]->email eq $userinfo->{"email"}
!ll&&!rl&&r
681
MC/DC100%2/2
$_[0]->isa("Text::Tradition::User") and $_[0]->email eq $userinfo->{"email"}
$_[0]->isa("Text::Tradition::User") $_[0]->email eq $userinfo->{"email"}
Truth table100%3/3
A and B
A= $_[0]->isa("Text::Tradition::User")B= $_[0]->email eq $userinfo->{"email"}
ABresult
0X0
100
111
537 1 ▶ if( $userinfo->{google_user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo->{"google_user"}) 0 1 1
538 0 @matches = grep { $_->id =~ m!https://www.google.com/accounts/o8/id! } @matches;
539 }
540 1 $user = shift @matches;
541 }
542 # print STDERR "Found user, $username, email is :", $user->email, ":\n";
543 12 return $user;
544 }
545  
546 0 ▶sub _find_gplus {
Subroutine _find_gplus: not called CC=6 SCAR=37.4
547 0 my ($self, $userinfo) = @_;
548  
549 0 my $sub = $userinfo->{sub};
550 0 my $email = $userinfo->{email};
551  
552 # Do we have a user with the google id already?
553  
554 0 my $user = $self->find_user({
555 username => $sub
556 });
557 0 ▶ warn "Found by google+id" if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
558  
559 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) 0 0 0
560 0 return $user;
561 }
562  
563 # Do we have a user with the email and an old G+ OpenID?
564 0 $user = $self->find_user({ email => $userinfo->{email}, google_user => 1 });
565 0 ▶ warn "Found by email" if $user;
Branch: 1 branch
BranchTrueFalseTotal
if $user 0 0 0
566  
567 0 ▶ if (!$user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
568 0 return undef;
569 }
570  
571 0 my $new_user = $self->add_user({
572 username => $sub,
573 password => $user->password,
574 role => $user->role,
575 active => $user->active,
576 sub => $sub,
577 email => $email,
578 });
579  
580 0 foreach my $t (@{ $user->traditions }) {
581 0 $user->remove_tradition($t);
582 0 $new_user->add_tradition($t);
583 0 $self->update($t);
584 }
585 0 $self->update($user);
586 0 $self->update($new_user);
587  
588 # $self->delete_user({ username => $user->id });
589 0 return $new_user;
590 }
591  
592 =head2 modify_user( $userinfo )
593  
594 Takes a hashref of C<username> and C<password> (same as add_user).
595  
596 Retrieves the user, and updates it with the new information. Username
597 changing is not currently supported.
598  
599 Returns the updated user object, or undef if not found.
600  
601 =cut
602  
603 1 ▶sub modify_user {
Subroutine modify_user: called CC=6 SCAR=18.8
Pod documented
604 1 my ($self, $userinfo) = @_;
605 1 my $username = $userinfo->{username};
606 1 my $password = $userinfo->{password};
607 1 my $role = $userinfo->{role};
608  
609 1 ▶ throw( "Missing username" ) unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 1 1
610  
611 1 my $user = $self->find_user({ username => $username });
612 1 ▶ throw( "Could not find user $username" ) unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 1 1
613  
614 1 ▶ if($password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($password) 1 0 1
615 1 ▶ throw( "Bad password" ) unless $self->validate_password($password);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validate_password($password) 0 1 1
616 1 $user->password(crypt_password($password));
617 }
618 1 ▶ if($role) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role) 0 1 1
619 0 $user->role($role);
620 }
621  
622 1 $self->update($user);
623  
624 1 return $user;
625 }
626  
627 =head2 deactivate_user( $userinfo )
628  
629 Takes a hashref of C<username>.
630  
631 Sets the users C<active> flag to false (0), and sets all traditions
632 assigned to them to non-public, updates the storage and returns the
633 deactivated user.
634  
635 Returns undef if user not found.
636  
637 =cut
638  
639 2 ▶sub deactivate_user {
Subroutine deactivate_user: called CC=4 SCAR=13.9
Pod documented
640 2 my ($self, $userinfo) = @_;
641 2 my $username = $userinfo->{username};
642  
643 2 ▶ throw( "Need to specify a username for deactivation" ) unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 2 2
644  
645 2 my $user = $self->find_user({ username => $username });
646 2 ▶ throw( "User $username not found" ) unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 2 2
647  
648 2 $user->active(0);
649 2 foreach my $tradition (@{ $user->traditions }) {
650 ## Not implemented yet
651 # $tradition->public(0);
652 }
653  
654 ## Should we be using Text::Tradition::Directory also?
655 2 $self->update(@{ $user->traditions });
656  
657 2 $self->update($user);
658  
659 2 return $user;
660 }
661  
662 =head2 reactivate_user( $userinfo )
663  
664 Takes a hashref of C<username>.
665  
666 Returns the user object if already activated. Activates (sets the
667 active flag to true (1)), updates the storage and returns the user.
668  
669 Returns undef if the user is not found.
670  
671 =cut
672  
673 1 ▶sub reactivate_user {
Subroutine reactivate_user: called CC=4 SCAR=14.2
Pod documented
674 1 my ($self, $userinfo) = @_;
675 1 my $username = $userinfo->{username};
676  
677 1 ▶ throw( "Need to specify a username for reactivation" ) unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 1 1
678  
679 1 my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
680 1 ▶ throw( "User $username not found" ) unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 1 1
681  
682 1 ▶ return $user if $user->active;
Branch: 1 branch
BranchTrueFalseTotal
if $user->active 0 1 1
683  
684 1 $user->active(1);
685 1 $self->update($user);
686  
687 1 return $user;
688 }
689  
690 =head2 delete_user( $userinfo )
691  
692 CAUTION: Deletes actual data!
693  
694 Takes a hashref of C<username>.
695  
696 Returns undef if the user doesn't exist.
697  
698 Removes the user from the store and returns 1.
699  
700 =cut
701  
702 1 ▶sub delete_user {
Subroutine delete_user: called CC=3 SCAR=11.1
Pod documented
703 1 my ($self, $userinfo) = @_;
704 1 my $username = $userinfo->{username};
705  
706 1 ▶ throw( "Need to specify a username for deletion" ) unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 1 1
707  
708 1 my $user = $self->find_user({ username => $username });
709 1 ▶ throw( "User $username not found" ) unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 1 1
710  
711 ## Should we be using Text::Tradition::Directory for this bit?
712 1 $self->delete( @{ $user->traditions });
713  
714 ## Poof, gone.
715 1 $self->delete($user);
716  
717 1 return 1;
718 }
719  
720 =head2 validate_password( $password )
721  
722 Takes a password string. Returns true if it is longer than
723 L</MIN_PASS_LEN>, false otherwise.
724  
725 Used internally by L</add_user>.
726  
727 =cut
728  
729 14 ▶sub validate_password {
Subroutine validate_password: called CC=3 SCAR=11.0
Pod documented
730 14 my ($self, $password) = @_;
731  
732 14 ▶ return if !$password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 3 11 14
733 11 ▶ return if length($password) < $self->MIN_PASS_LEN;
Branch: 1 branch
BranchTrueFalseTotal
if length $password < $self->MIN_PASS_LEN 2 9 11
734  
735 9 return 1;
736 }
737  
738 =head2 user_traditionlist( $user )
739  
740 Returns a tradition list (see specification above) but containing only
741 those traditions visible to the specified user. If $user is the string
742 'public', returns only publicly-viewable traditions.
743  
744 =cut
745  
746 5 ▶sub user_traditionlist {
Subroutine user_traditionlist: called CC=6 SCAR=18.0
Pod documented
747 5 my ($self, $user) = @_;
748
749 5 my @tlist;
750 5 ▶ if(ref $user && $user->is_admin) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $user and $user->is_admin) { } 1 4 5
elsif (ref $user) { } 3 1 4
elsif ($user ne 'public') { } 0 1 1
Condition100%3/3
ref $user and $user->is_admin
!ll&&!rl&&r
131
MC/DC100%2/2
ref $user and $user->is_admin
ref $user $user->is_admin
Truth table100%3/3
A and B
A= ref $userB= $user->is_admin
ABresult
0X0
100
111
751 ## Admin sees all
752 1 return $self->traditionlist();
753 } elsif(ref $user) {
754 ## We have a user object already, so just fetch its traditions and use tose
755 3 foreach my $t (@{ $user->traditions }) {
756 2 push( @tlist, { 'id' => $self->object_to_id( $t ),
757 'name' => $t->name } );
758 }
759 3 return @tlist;
760 } elsif($user ne 'public') {
761 0 die "Passed neither a user object nor 'public' to user_traditionlist";
762 }
763
764 ## Search for all traditions which allow public viewing
765 1 my @list = grep { $_->{public} } $self->traditionlist();
766 1 return @list;
767 }
768  
769 4 1;
770
771 =head1 LICENSE
772  
773 This package is free software and is provided "as is" without express
774 or implied warranty. You can redistribute it and/or modify it under
775 the same terms as Perl itself.
776  
777 =head1 AUTHORS
778  
779 Tara L Andrews E<lt>aurum@cpan.orgE<gt> (initial release)
780  
781 Shadowcat Systems L<http://www.scsys.co.uk/> (user functionality; making it all work)
782  
↑ Summary User.pm »