[Bps-public-commit] jifty-plugin-authentication-twitter branch, master, updated. 732d584974018ebe93b5e9b9148a649622c1a8d3

Shawn Moore sartak at bestpractical.com
Thu Jun 3 03:38:29 EDT 2010


The branch, master has been updated
       via  732d584974018ebe93b5e9b9148a649622c1a8d3 (commit)
      from  4650d08e3b5a267624b3b9293999570498d70dc3 (commit)

Summary of changes:
 .../Twitter/Action/LoginTwitterUser.pm             |   86 ++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)
 create mode 100644 lib/Jifty/Plugin/Authentication/Twitter/Action/LoginTwitterUser.pm

- Log -----------------------------------------------------------------
commit 732d584974018ebe93b5e9b9148a649622c1a8d3
Author: Shawn M Moore <sartak at gmail.com>
Date:   Thu Jun 3 03:39:13 2010 -0400

    Skeleton LoginTwitterUser action

diff --git a/lib/Jifty/Plugin/Authentication/Twitter/Action/LoginTwitterUser.pm b/lib/Jifty/Plugin/Authentication/Twitter/Action/LoginTwitterUser.pm
new file mode 100644
index 0000000..3caaf92
--- /dev/null
+++ b/lib/Jifty/Plugin/Authentication/Twitter/Action/LoginTwitterUser.pm
@@ -0,0 +1,86 @@
+use warnings;
+use strict;
+
+=head1 NAME
+
+Jifty::Plugin::Authentication::Twitter::Action::LoginTwitterUser - login Twitter user
+
+=cut
+
+package Jifty::Plugin::Authentication::Twitter::Action::LoginTwitterUser;
+use base qw/Jifty::Action/;
+
+=head1 ARGUMENTS
+
+=cut
+
+use Jifty::Param::Schema;
+use Jifty::Action schema {
+};
+
+=head1 METHODS
+
+=head2 take_action
+
+Get the session key using the Twitter API.  Check for existing user.
+If none, autocreate.  Login user.
+
+=cut
+
+sub take_action {
+    my $self = shift;
+
+    my $name = '...';
+
+    # Load up the user
+    my $current_user = Jifty->app_class('CurrentUser');
+    my $user = $current_user->new( twitter_name => $name );
+
+    # Autocreate the user if necessary
+    if ( not $user->id ) {
+        my $action = Jifty->web->new_action(
+            class           => 'CreateUser',
+            current_user    => $current_user->superuser,
+            arguments       => {
+                twitter_name => $name,
+            }
+        );
+        $action->run;
+
+        if ( not $action->result->success ) {
+            # Should this be less "friendly"?
+            $self->result->error(_("Sorry, something weird happened (we couldn't create a user for you).  Try again later."));
+            return;
+        }
+
+        $user = $current_user->new( twitter_name => $name );
+    }
+
+    my $u = $user->user_object;
+
+    # Always check name
+    $u->__set( column => 'twitter_name', value => $name )
+        if not defined $u->twitter_name or $u->twitter_name ne $name;
+
+    # Login!
+    Jifty->web->current_user( $user );
+    Jifty->web->session->expires('+1y');
+    Jifty->web->session->set_cookie;
+
+    # Success!
+    $self->report_success;
+
+    return 1;
+}
+
+=head2 report_success
+
+=cut
+
+sub report_success {
+    my $self = shift;
+    $self->result->message(_("Hi %1!", Jifty->web->current_user->user_object->twitter_name ));
+}
+
+1;
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list