remove tradition from old user too
[scpubgit/stemmatology.git] / persistence / lib / Text / Tradition / Directory.pm
index 3afc469..c57115b 100644 (file)
@@ -20,7 +20,7 @@ use Text::Tradition::TypeMap::Entry;
 extends 'KiokuX::Model';
 
 use vars qw/ $VERSION /;
-$VERSION = "1.1";
+$VERSION = "1.2";
 
 =head1 NAME
 
@@ -185,25 +185,35 @@ ok( $nt->$_isa('Text::Tradition'), "Made new tradition" );
                        is( $e->ident, 'database error', "Got exception trying to fetch stemma directly" );
                        like( $e->message, qr/not a Text::Tradition/, "Exception has correct message" );
                }
-               try {
-                       $f->delete( $sid );
-               } catch( Text::Tradition::Error $e ) {
-                       is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
-                       like( $e->message, qr/Cannot directly delete non-Tradition object/, 
-                               "Exception has correct message" );
+               if( $ENV{TEST_DELETION} ) {
+                       try {
+                               $f->delete( $sid );
+                       } catch( Text::Tradition::Error $e ) {
+                               is( $e->ident, 'database error', "Got exception trying to delete stemma directly" );
+                               like( $e->message, qr/Cannot directly delete non-Tradition object/, 
+                                       "Exception has correct message" );
+                       }
                }
        }
        
-       $f->delete( $uuid );
-       ok( !$f->exists( $uuid ), "Object is deleted from DB" );
-       ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
-       is( scalar $f->traditionlist, 1, "Object is deleted from index" );
+       SKIP: {
+               skip "Set TEST_DELETION in env to test DB deletion functionality", 3
+                       unless $ENV{TEST_DELETION};
+               $f->delete( $uuid );
+               ok( !$f->exists( $uuid ), "Object is deleted from DB" );
+               ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ) if $stemma_enabled;
+               is( scalar $f->traditionlist, 1, "Object is deleted from index" );
+       }
 }
 
 {
        my $g = Text::Tradition::Directory->new( 'dsn' => $dsn );
        my $scope = $g->new_scope;
-       is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
+       SKIP: {
+               skip "Set TEST_DELETION in env to test DB deletion functionality", 1
+                       unless $ENV{TEST_DELETION};
+               is( scalar $g->traditionlist, 1, "Now one object in new directory index" );
+       }
        my $ntobj = $g->tradition( 'CX' );
        my @w1 = sort { $a->sigil cmp $b->sigil } $ntobj->witnesses;
        my @w2 = sort{ $a->sigil cmp $b->sigil } $nt->witnesses;
@@ -430,10 +440,14 @@ sub add_user {
     my $password = $userinfo->{password};
     my $role = $userinfo->{role} || 'user';
 
+    if ($userinfo->{sub}) {
+        $username = $userinfo->{sub};
+    }
+
        throw( "No username given" ) unless $username;
        throw( "Invalid password - must be at least " . $self->MIN_PASS_LEN 
                . " characters long" )
-               unless ( $self->validate_password($password) || $username =~ /^https?:/ );
+               unless ( $self->validate_password($password) || $username =~ /^https?:/  || exists ($userinfo->{openid_id}) || exists ($userinfo->{sub}));
 
     my $user = Text::Tradition::User->new(
         id => $username,
@@ -503,6 +517,10 @@ sub find_user {
         _extract_openid_data($userinfo);
     }
 
+    if (exists $userinfo->{sub} && exists $userinfo->{openid_id}) {
+        return $self->_find_gplus($userinfo);
+    }
+
        my $user;
        if( exists $userinfo->{username} ) {
        my $username = $userinfo->{username};
@@ -522,6 +540,58 @@ sub find_user {
     return $user;
 }
 
+sub _find_gplus {
+    my ($self, $userinfo) = @_;
+
+    my $sub = $userinfo->{sub};
+    my $openid = $userinfo->{openid_id};
+    my $email = $userinfo->{email};
+
+    # Do we have a user with the google id already?
+
+    my $user = $self->find_user({
+        username => $sub
+    });
+    warn "Found by google+id" if $user;
+
+    if ($user) {
+        return $user;
+    }
+
+    # Do we have a user with the openid?
+
+    $user = $self->find_user({
+        url => $openid
+    });
+    warn "Found by openid" if $user;
+    $user ||= $self->find_user({ email => $userinfo->{email} });
+    warn "Found by email" if $user;
+
+    if (!$user) {
+        return undef;
+    }
+
+    my $new_user = $self->add_user({
+            username  => $sub,
+            password  => $user->password,
+            role      => $user->role,
+            active    => $user->active,
+            sub       => $sub,
+            openid_id => $openid,
+            email     => $email,
+        });
+
+    foreach my $t (@{ $user->traditions }) {
+               $user->remove_tradition($t);
+        $new_user->add_tradition($t);
+    }
+    $self->update($user);
+    $self->update($new_user);
+
+    # $self->delete_user({ username => $user->id });
+    return $new_user;
+}
+
 =head2 modify_user( $userinfo )
 
 Takes a hashref of C<username> and C<password> (same as add_user).