Add autocreate user and add openid login back
[scpubgit/stemmaweb.git] / lib / Text / Tradition / Directory.pm
CommitLineData
83ed6665 1package Text::Tradition::Directory;
2
3use strict;
4use warnings;
5use Moose;
6use DBI;
7use Encode qw/ encode decode_utf8 /;
8use KiokuDB::GC::Naive;
9use KiokuDB::TypeMap;
10use KiokuDB::TypeMap::Entry::Naive;
11use Safe::Isa;
12use Text::Tradition::Error;
13
14## users
15use KiokuX::User::Util qw(crypt_password);
16use Text::Tradition::Store;
17use Text::Tradition::User;
18use Text::Tradition::TypeMap::Entry;
19
20extends 'KiokuX::Model';
21
22use vars qw/ $VERSION /;
23$VERSION = "1.2";
24
25=head1 NAME
26
27Text::Tradition::Directory - a KiokuDB interface for storing and retrieving
28traditions 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
63Text::Tradition::Directory is an interface for storing and retrieving text
64traditions and all their data, including an associated stemma hypothesis
65and a user who has ownership rights to the tradition data. It is an
66instantiation of a KiokuDB::Model, storing traditions and associated
67stemmas by UUID.
68
69The Text::Tradition::Directory package also includes the
70L<Text::Tradition::User> class for user objects, and the
71L<Text::Tradition::Ownership> role which extends the Text::Tradition class
72to handle user ownership.
73
74=head1 ATTRIBUTES
75
76=head2 MIN_PASS_LEN
77
78Constant for the minimum password length when validating passwords,
79defaults to "8".
80
81=cut
82
83has MIN_PASS_LEN => ( is => 'ro', isa => 'Num', default => sub { 8 } );
84
85=head1 METHODS
86
87=head2 new
88
89Returns a Directory object.
90
91=head2 traditionlist
92
93Returns a hashref mapping of ID => name for all traditions in the directory.
94
95=head2 tradition( $id )
96
97Returns the Text::Tradition object of the given ID.
98
99=head2 save( $tradition )
100
101Writes the given tradition to the database, returning its ID.
102
103=head2 delete( $tradition )
104
105Deletes the given tradition object from the database.
106WARNING!! Garbage collection does not yet work. Use this sparingly.
107
108=begin testing
109
110use TryCatch;
111use File::Temp;
112use Safe::Isa;
113use Text::Tradition;
114use_ok 'Text::Tradition::Directory';
115
116my $fh = File::Temp->new();
117my $file = $fh->filename;
118$fh->close;
119my $dsn = "dbi:SQLite:dbname=$file";
120my $uuid;
121my $user = 'user@example.org';
122my $t = Text::Tradition->new(
123 'name' => 'inline',
124 'input' => 'Tabular',
125 'file' => 't/data/simple.txt',
126 );
127my $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}
159my $nt = Text::Tradition->new(
160 'name' => 'CX',
161 'input' => 'CollateX',
162 'file' => 't/data/Collatex-16.xml',
163 );
164ok( $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
226use Text::Tradition::TypeMap::Entry;
227
228has +typemap => (
229 is => 'rw',
230 isa => 'KiokuDB::TypeMap',
231 default => sub {
232 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);
253
254has '_mysql_utf8_hack' => (
255 is => 'ro',
256 isa => 'Bool',
257 default => undef,
258);
259
260# Push some columns into the extra_args
261around BUILDARGS => sub {
262 my $orig = shift;
263 my $class = shift;
264 my $args;
265 if( @_ == 1 ) {
266 $args = $_[0];
267 } else {
268 $args = { @_ };
269 }
270 my @column_args;
271 if( $args->{'dsn'} =~ /^dbi:(\w+):/ ) { # We're using Backend::DBI
272 my $dbtype = $1;
273 @column_args = ( 'columns',
274 [ 'name' => { 'data_type' => 'varchar', 'is_nullable' => 1 },
275 'public' => { 'data_type' => 'bool', 'is_nullable' => 1 } ] );
276 if( $dbtype eq 'mysql' &&
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 $args->{extra_args}->{dbi_attrs}->{mysql_enable_utf8} = undef;
283 $args->{_mysql_utf8_hack} = 1;
284 }
285 }
286 my $ea = $args->{'extra_args'};
287 if( ref( $ea ) eq 'ARRAY' ) {
288 push( @$ea, @column_args );
289 } elsif( ref( $ea ) eq 'HASH' ) {
290 $ea = { %$ea, @column_args };
291 } else {
292 $ea = { @column_args };
293 }
294 $args->{'extra_args'} = $ea;
295
296 return $class->$orig( $args );
297};
298
299override _build_directory => sub {
300 my($self) = @_;
301 Text::Tradition::Store->connect(@{ $self->_connect_args },
302 resolver_constructor => sub {
303 my($class) = @_;
304 $class->new({ typemap => $self->directory->merged_typemap,
305 fallback_entry => Text::Tradition::TypeMap::Entry->new() });
306 });
307};
308
309## These checks don't cover store($id, $obj)
310# before [ qw/ store update insert delete / ] => sub {
311before [ qw/ delete / ] => sub {
312 my $self = shift;
313 my @nontrad;
314 foreach my $obj ( @_ ) {
315 if( ref( $obj ) && !$obj->$_isa( 'Text::Tradition' )
316 && !$obj->$_isa('Text::Tradition::User') ) {
317 # Is it an id => Tradition hash?
318 if( ref( $obj ) eq 'HASH' && keys( %$obj ) == 1 ) {
319 my( $k ) = keys %$obj;
320 next if $obj->{$k}->$_isa('Text::Tradition');
321 }
322 push( @nontrad, $obj );
323 }
324 }
325 if( @nontrad ) {
326 throw( "Cannot directly save non-Tradition object of type "
327 . ref( $nontrad[0] ) );
328 }
329};
330
331# TODO Garbage collection doesn't work. Suck it up and live with the
332# inflated DB.
333after delete => sub {
334 my $self = shift;
335 my $gc = KiokuDB::GC::Naive->new( backend => $self->directory->backend );
336 $self->directory->backend->delete( $gc->garbage->members );
337};
338
339sub save {
340 my $self = shift;
341 return $self->store( @_ );
342}
343
344sub tradition {
345 my( $self, $id ) = @_;
346 my $obj = $self->lookup( $id );
347 unless( $obj ) {
348 # Try looking up by name.
349 foreach my $item ( $self->traditionlist ) {
350 if( $item->{'name'} eq $id ) {
351 $obj = $self->lookup( $item->{'id'} );
352 last;
353 }
354 }
355 }
356 if( $obj && !$obj->$_isa('Text::Tradition') ) {
357 throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" );
358 }
359 return $obj;
360}
361
362sub traditionlist {
363 my $self = shift;
364 my ($user) = @_;
365
366 return $self->user_traditionlist($user) if($user);
367 return $self->_get_object_idlist( 'Text::Tradition' );
368}
369
370sub _get_object_idlist {
371 my( $self, $objclass ) = @_;
372 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 if( $self->dsn =~ /^dbi:(\w+):/ ) {
376 my $dbtype = $1;
377 my @connection = @{$self->directory->backend->connect_info};
378 # Get rid of KiokuDB-specific arg
379 pop @connection if scalar @connection > 4;
380 $connection[3]->{'sqlite_unicode'} = 1 if $dbtype eq 'SQLite';
381 $connection[3]->{'pg_enable_utf8'} = 1 if $dbtype eq 'Pg';
382 my $dbh = DBI->connect( @connection );
383 my $q = $dbh->prepare( 'SELECT id, name, public from entries WHERE class = "'
384 . $objclass . '"' );
385 $q->execute();
386 while( my @row = $q->fetchrow_array ) {
387 # Horrible horrible hack. Re-convert the name to UTF-8.
388 if( $self->_mysql_utf8_hack ) {
389 # Convert the chars into a raw bytestring.
390 my $octets = encode( 'ISO-8859-1', $row[1] );
391 $row[1] = decode_utf8( $octets );
392 }
393 push( @tlist, { 'id' => $row[0], 'name' => $row[1], 'public' => $row[2] } );
394 }
395 } else {
396 $self->scan( sub { my $o = shift;
397 push( @tlist, { 'id' => $self->object_to_id( $o ),
398 'name' => $o->name,
399 'public' => $o->public } )
400 if( ref $o eq $objclass ) } );
401 }
402 return @tlist;
403}
404
405sub throw {
406 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
430Takes a hashref of C<username>, C<password>.
431
432Create a new user object, store in the KiokuDB backend, and return it.
433
434=cut
435
436sub add_user {
437 my ($self, $userinfo) = @_;
438
439 my $username = $userinfo->{username};
440 my $password = $userinfo->{password};
441 my $role = $userinfo->{role} || 'user';
442
443 throw( "No username given" ) unless $username;
444 throw( "Invalid password - must be at least " . $self->MIN_PASS_LEN
445 . " characters long" )
446 unless ( $self->validate_password($password) || $username =~ /^https?:/ );
447
448 my $user = Text::Tradition::User->new(
449 id => $username,
450 password => ($password ? crypt_password($password) : ''),
451 email => ($userinfo->{email} ? $userinfo->{email} : $username),
452 role => $role,
453 );
454
455 $self->store($user->kiokudb_object_id, $user);
456
457 return $user;
458}
459
460=head2 create_user( $userinfo )
461
462Takes a hashref that can either be suitable for add_user (see above) or be
463a hash of OpenID user information from Credential::OpenID.
464
465=cut
466
467sub create_user {
468 my ($self, $userinfo) = @_;
469
470 ## No username means probably an OpenID based user
471 if(!exists $userinfo->{username}) {
472 _extract_openid_data($userinfo);
473 }
474
475 return $self->add_user($userinfo);
476}
477
478## Not quite sure where this method should be.. Auth /
479## Credential::OpenID just pass us back the chunk of extension data
480sub _extract_openid_data {
481 my ($userinfo) = @_;
482
483 ## Spec says SHOULD use url as identifier
484 $userinfo->{username} = $userinfo->{url};
485
486 ## Use email addy as display if available
487 if(exists $userinfo->{extensions} &&
488 exists $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'} &&
489 defined $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'}) {
490 ## Somewhat ugly attribute extension reponse, contains
491 ## google-email string which we can use as the id
492
493 $userinfo->{email} = $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'};
494 }
495
496 return;
497}
498
499=head2 find_user( $userinfo )
500
501Takes a hashref of C<username> or C<email>, and possibly openIDish results from
502L<Net::OpenID::Consumer>.
503
504Fetches the user object for the given username and returns it.
505
506=cut
507
508sub find_user {
509 my ($self, $userinfo) = @_;
510
511 ## A URL field means probably an OpenID based user
512 if( exists $userinfo->{url} ) {
513 _extract_openid_data($userinfo);
514 }
515
516 if (exists $userinfo->{sub} && exists $userinfo->{openid_id}) {
517 return $self->_find_gplus($userinfo);
518 }
519
520 my $user;
521 if( exists $userinfo->{username} ) {
522 my $username = $userinfo->{username};
523 ## No logins if user is deactivated (use lookup to fetch to re-activate)
524 $user = $self->lookup(Text::Tradition::User->id_for_user($username));
525 ## If there is an inactive user, skip it
526 return if( $user && !$user->active );
527 } elsif( exists $userinfo->{email} ) {
528 ## Scan the users looking for a matching email
529 my @matches;
530 $self->scan( sub { push( @matches, @_ )
531 if $_[0]->isa('Text::Tradition::User')
532 && $_[0]->email eq $userinfo->{email} } );
533 $user = shift @matches;
534 }
535# print STDERR "Found user, $username, email is :", $user->email, ":\n";
536 return $user;
537}
538
539sub _find_gplus {
540 my ($self, $userinfo) = @_;
541
542 my $sub = $userinfo->{sub};
543 my $openid = $userinfo->{openid_id};
544
545 # Do we have a user with the google id already?
546
547 my $user = $self->find_user({
548 username => $sub
549 });
550
551 if ($user) {
552 return $user;
553 }
554
555 # Do we have a user with the openid?
556
557 $user = $self->find_user({
558 url => $openid
559 });
560
561 if (!$user) {
562 throw ("Could not find a user with that openid or sub!");
563 }
564
565 my $new_user = $self->add_user({
566 username => $sub,
567 password => $user->password,
568 role => $user->role,
569 active => $user->active,
570 });
571
572 foreach my $t (@{ $user->traditions }) {
573 $new_user->add_tradition($t);
574 }
575
576 $self->delete_user({ username => $user->id });
577 return $new_user;
578}
579
580=head2 modify_user( $userinfo )
581
582Takes a hashref of C<username> and C<password> (same as add_user).
583
584Retrieves the user, and updates it with the new information. Username
585changing is not currently supported.
586
587Returns the updated user object, or undef if not found.
588
589=cut
590
591sub modify_user {
592 my ($self, $userinfo) = @_;
593 my $username = $userinfo->{username};
594 my $password = $userinfo->{password};
595 my $role = $userinfo->{role};
596
597 throw( "Missing username" ) unless $username;
598
599 my $user = $self->find_user({ username => $username });
600 throw( "Could not find user $username" ) unless $user;
601
602 if($password) {
603 throw( "Bad password" ) unless $self->validate_password($password);
604 $user->password(crypt_password($password));
605 }
606 if($role) {
607 $user->role($role);
608 }
609
610 $self->update($user);
611
612 return $user;
613}
614
615=head2 deactivate_user( $userinfo )
616
617Takes a hashref of C<username>.
618
619Sets the users C<active> flag to false (0), and sets all traditions
620assigned to them to non-public, updates the storage and returns the
621deactivated user.
622
623Returns undef if user not found.
624
625=cut
626
627sub deactivate_user {
628 my ($self, $userinfo) = @_;
629 my $username = $userinfo->{username};
630
631 throw( "Need to specify a username for deactivation" ) unless $username;
632
633 my $user = $self->find_user({ username => $username });
634 throw( "User $username not found" ) unless $user;
635
636 $user->active(0);
637 foreach my $tradition (@{ $user->traditions }) {
638 ## Not implemented yet
639 # $tradition->public(0);
640 }
641
642 ## Should we be using Text::Tradition::Directory also?
643 $self->update(@{ $user->traditions });
644
645 $self->update($user);
646
647 return $user;
648}
649
650=head2 reactivate_user( $userinfo )
651
652Takes a hashref of C<username>.
653
654Returns the user object if already activated. Activates (sets the
655active flag to true (1)), updates the storage and returns the user.
656
657Returns undef if the user is not found.
658
659=cut
660
661sub reactivate_user {
662 my ($self, $userinfo) = @_;
663 my $username = $userinfo->{username};
664
665 throw( "Need to specify a username for reactivation" ) unless $username;
666
667 my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
668 throw( "User $username not found" ) unless $user;
669
670 return $user if $user->active;
671
672 $user->active(1);
673 $self->update($user);
674
675 return $user;
676}
677
678=head2 delete_user( $userinfo )
679
680CAUTION: Deletes actual data!
681
682Takes a hashref of C<username>.
683
684Returns undef if the user doesn't exist.
685
686Removes the user from the store and returns 1.
687
688=cut
689
690sub delete_user {
691 my ($self, $userinfo) = @_;
692 my $username = $userinfo->{username};
693
694 throw( "Need to specify a username for deletion" ) unless $username;
695
696 my $user = $self->find_user({ username => $username });
697 throw( "User $username not found" ) unless $user;
698
699 ## Should we be using Text::Tradition::Directory for this bit?
700 $self->delete( @{ $user->traditions });
701
702 ## Poof, gone.
703 $self->delete($user);
704
705 return 1;
706}
707
708=head2 validate_password( $password )
709
710Takes a password string. Returns true if it is longer than
711L</MIN_PASS_LEN>, false otherwise.
712
713Used internally by L</add_user>.
714
715=cut
716
717sub validate_password {
718 my ($self, $password) = @_;
719
720 return if !$password;
721 return if length($password) < $self->MIN_PASS_LEN;
722
723 return 1;
724}
725
726=head2 user_traditionlist( $user )
727
728Returns a tradition list (see specification above) but containing only
729those traditions visible to the specified user. If $user is the string
730'public', returns only publicly-viewable traditions.
731
732=cut
733
734sub user_traditionlist {
735 my ($self, $user) = @_;
736
737 my @tlist;
738 if(ref $user && $user->is_admin) {
739 ## Admin sees all
740 return $self->traditionlist();
741 } elsif(ref $user) {
742 ## We have a user object already, so just fetch its traditions and use tose
743 foreach my $t (@{ $user->traditions }) {
744 push( @tlist, { 'id' => $self->object_to_id( $t ),
745 'name' => $t->name } );
746 }
747 return @tlist;
748 } elsif($user ne 'public') {
749 die "Passed neither a user object nor 'public' to user_traditionlist";
750 }
751
752 ## Search for all traditions which allow public viewing
753 my @list = grep { $_->{public} } $self->traditionlist();
754 return @list;
755}
756
7571;
758
759=head1 LICENSE
760
761This package is free software and is provided "as is" without express
762or implied warranty. You can redistribute it and/or modify it under
763the same terms as Perl itself.
764
765=head1 AUTHORS
766
767Tara L Andrews E<lt>aurum@cpan.orgE<gt> (initial release)
768
769Shadowcat Systems L<http://www.scsys.co.uk/> (user functionality; making it all work)
770