From: tla Date: Thu, 5 Feb 2015 21:46:11 +0000 (+0100) Subject: Directory changes needed for G+ signin; see tla/stemmaweb@7c6c2a1a X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=11a9ecda9676a5bc2304a152dfd264928adbb596;p=scpubgit%2Fstemmatology.git Directory changes needed for G+ signin; see tla/stemmaweb@7c6c2a1a --- diff --git a/persistence/lib/Text/Tradition/Directory.pm b/persistence/lib/Text/Tradition/Directory.pm index ec8462e..385390b 100644 --- a/persistence/lib/Text/Tradition/Directory.pm +++ b/persistence/lib/Text/Tradition/Directory.pm @@ -440,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, @@ -513,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}; @@ -532,6 +540,49 @@ sub find_user { return $user; } +sub _find_gplus { + my ($self, $userinfo) = @_; + + my $sub = $userinfo->{sub}; + my $openid = $userinfo->{openid_id}; + + # Do we have a user with the google id already? + + my $user = $self->find_user({ + username => $sub + }); + + if ($user) { + return $user; + } + + # Do we have a user with the openid? + + $user = $self->find_user({ + url => $openid + }); + + 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, + }); + + foreach my $t (@{ $user->traditions }) { + $new_user->add_tradition($t); + } + + #$self->delete_user({ username => $user->id }); + return $new_user; +} + =head2 modify_user( $userinfo ) Takes a hashref of C and C (same as add_user).