Support email-attribute fetching from OpenID
[scpubgit/stemmatology.git] / lib / Text / Tradition / Directory.pm
index 55b7986..ed5f9b2 100644 (file)
@@ -221,12 +221,11 @@ around BUILDARGS => sub {
        return $class->$orig( $args );
 };
 
+## These checks don't cover store($id, $obj)
 before [ qw/ store update insert delete / ] => sub {
        my $self = shift;
        my @nontrad;
        foreach my $obj ( @_ ) {
-#              if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition' ) {
-
                if( ref( $obj ) && ref( $obj ) ne 'Text::Tradition'
             && ref ($obj) ne 'Text::Tradition::User' ) {
                        # Is it an id => Tradition hash?
@@ -278,18 +277,24 @@ sub user_traditionlist {
     my ($self, $user) = @_;
 
     my @tlist;
-    if($user && $user ne 'public') {
+    if(ref $user && $user->is_admin) {
+        ## Admin sees all
+        return $self->traditionlist();
+    } elsif(ref $user) {
         ## We have a user object already, so just fetch its traditions and use tose
-        foreach my $t (@{ $self->lookup($user)->traditions }) {
+        foreach my $t (@{ $user->traditions }) {
             push( @tlist, { 'id' => $self->object_to_id( $t ), 
                             'name' => $t->name } );
         }
         return @tlist;
+    } elsif($user ne 'public') {
+        die "Passed neither a user object nor 'public' to user_traditionlist";
     }
     
     ## Search for all traditions which allow public viewing
     ## When they exist!
-    # $self->search({ public => 1 });
+## This needs to be more sophisticated, probably needs Search::GIN
+#    my $list = $self->search({ public => 1 });
     
     ## For now, just fetch all
     ## (could use all_objects or grep down there?)
@@ -362,8 +367,10 @@ Create a new user object, store in the KiokuDB backend, and return it.
 
 sub add_user {
     my ($self, $userinfo) = @_;
-    my $username = $userinfo->{url} || $userinfo->{username};
+
+    my $username = $userinfo->{username};
     my $password = $userinfo->{password};
+    my $role = $userinfo->{role} || 'user';
 
     return unless ($username =~ /^https?:/ 
                    || ($username && $self->validate_password($password))) ;
@@ -371,6 +378,8 @@ sub add_user {
     my $user = Text::Tradition::User->new(
         id => $username,
         password => ($password ? crypt_password($password) : ''),
+        display => ($userinfo->{display} ? $userinfo->{display} : $username),
+        role => $role,
     );
 
     $self->store($user->kiokudb_object_id, $user);
@@ -379,13 +388,41 @@ sub add_user {
 }
 
 sub create_user {
-    my $self = shift;
-    return $self->add_user(@_);
+    my ($self, $userinfo) = @_;
+
+    ## No username means probably an OpenID based user
+    if(!exists $userinfo->{username}) {
+        extract_openid_data($userinfo);
+    }
+
+    return $self->add_user($userinfo);
+}
+
+## Not quite sure where this method should be.. Auth /
+## Credential::OpenID just pass us back the chunk of extension data
+sub extract_openid_data {
+    my ($userinfo) = @_;
+
+    ## Spec says SHOULD use url as identifier
+    $userinfo->{username} = $userinfo->{url};
+
+    ## Use email addy as display if available
+    if(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'}) {
+        ## Somewhat ugly attribute extension reponse, contains
+        ## google-email string which we can use as the id
+
+        $userinfo->{display} = $userinfo->{extensions}{'http://openid.net/srv/ax/1.0'}{'value.email'};
+    }
+
+    return;
 }
 
 =head2 find_user
 
-Takes a hashref of C<username>, optionally C<openid_identifier>.
+Takes a hashref of C<username>, and possibly openIDish results from
+L<Net::OpenID::Consumer>.
 
 Fetches the user object for the given username and returns it.
 
@@ -393,13 +430,21 @@ Fetches the user object for the given username and returns it.
 
 sub find_user {
     my ($self, $userinfo) = @_;
-    ## url or display?
-    # 'display' => 'castaway.myopenid.com',
-    # 'url' => 'http://castaway.myopenid.com/',
-    my $username = $userinfo->{url} || $userinfo->{username};
 
-    return $self->lookup(Text::Tradition::User->id_for_user($username));
-    
+    ## No username means probably an OpenID based user
+    if(!exists $userinfo->{username}) {
+        extract_openid_data($userinfo);
+    }
+
+    my $username = $userinfo->{username};
+
+    ## No logins if user is deactivated (use lookup to fetch to re-activate)
+    my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
+    return if(!$user || !$user->active);
+
+    print STDERR "Found user, $username, display is :", $user->display, ":\n";
+
+    return $user;
 }
 
 =head2 modify_user
@@ -417,13 +462,20 @@ sub modify_user {
     my ($self, $userinfo) = @_;
     my $username = $userinfo->{username};
     my $password = $userinfo->{password};
+    my $role = $userinfo->{role};
 
-    return unless $username && $self->validate_password($password);
+    return unless $username;
+    return if($password && !$self->validate_password($password));
 
     my $user = $self->find_user({ username => $username });
     return unless $user;
 
-    $user->password(crypt_password($password));
+    if($password) {
+        $user->password(crypt_password($password));
+    }
+    if($role) {
+        $user->role($role);
+    }
 
     $self->update($user);
 
@@ -482,7 +534,7 @@ sub reactivate_user {
 
     return if !$username;
 
-    my $user = $self->find_user({ username => $username });
+    my $user = $self->lookup(Text::Tradition::User->id_for_user($username));
     return if !$user;
 
     return $user if $user->active;