Created
June 19, 2012 07:19
-
-
Save wilr/2952759 to your computer and use it in GitHub Desktop.
ENHANCEMENT: update module to support 3.0 api. FEATURE: module now fires an 'authchanged' javascript event to allow site UI updates. ENHANCEMENT: store uses access token to allow offline publishing. Twitter access tokens never expire. BUGFIX: fix throwing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -urN --exclude=.piston.yml --exclude=.git --exclude=.svn /Users/Will/Sites/justsold/.twitter.tmp/Authenticator/LoginForm.php /Users/Will/Sites/justsold/twitter/Authenticator/LoginForm.php | |
--- /Users/Will/Sites/justsold/.twitter.tmp/Authenticator/LoginForm.php 2012-06-19 19:13:19.000000000 +1200 | |
+++ /Users/Will/Sites/justsold/twitter/Authenticator/LoginForm.php 2012-06-12 20:39:03.000000000 +1200 | |
@@ -1,6 +1,7 @@ | |
<?php | |
class TwitterLoginForm extends LoginForm { | |
+ | |
protected $authenticator_class = 'TwitterAuthenticator'; | |
public function __construct($controller, $method, $fields = null, $actions = null, $checkCurrentUser = true) { | |
@@ -10,15 +11,15 @@ | |
$backURL = Session::get('BackURL'); | |
} | |
if($checkCurrentUser && Member::currentUser() && Member::logged_in_session_exists()) { | |
- $fields = new FieldSet( | |
+ $fields = new FieldList( | |
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this) | |
); | |
- $actions = new FieldSet( | |
+ $actions = new FieldList( | |
new FormAction("logout", _t('Member.BUTTONLOGINOTHER', "Log in as someone else")) | |
); | |
} else { | |
if(!$fields) { | |
- $fields = new FieldSet( | |
+ $fields = new FieldList( | |
new HiddenField("AuthenticationMethod", null, $this->authenticator_class, $this) | |
); | |
if(Security::$autologin_enabled) { | |
@@ -31,14 +32,15 @@ | |
} | |
} | |
if(!$actions) { | |
- $actions = new FieldSet( | |
- new ImageFormAction('dologin', 'Sign in with Twitter', 'twitter/Images/signin.png') | |
+ $actions = new FieldList( | |
+ FormAction::create('dologin', 'Sign in with Twitter')->setAttribute('src', 'twitter/Images/signin.png') | |
); | |
} | |
} | |
if(!empty($backURL)) { | |
$fields->push(new HiddenField('BackURL', 'BackURL', $backURL)); | |
} | |
+ | |
parent::__construct( | |
$controller, | |
$method, | |
diff -urN --exclude=.piston.yml --exclude=.git --exclude=.svn /Users/Will/Sites/justsold/.twitter.tmp/Controllers/Callback.php /Users/Will/Sites/justsold/twitter/Controllers/Callback.php | |
--- /Users/Will/Sites/justsold/.twitter.tmp/Controllers/Callback.php 2012-06-19 19:13:19.000000000 +1200 | |
+++ /Users/Will/Sites/justsold/twitter/Controllers/Callback.php 2012-04-22 17:33:51.000000000 +1200 | |
@@ -20,6 +20,14 @@ | |
self::$consumer_key = $key; | |
} | |
+ public static function get_consumer_key() { | |
+ return self::$consumer_key; | |
+ } | |
+ | |
+ public static function get_consumer_secret() { | |
+ return self::$consumer_secret; | |
+ } | |
+ | |
public static $allowed_actions = array( | |
'Connect', | |
'Login', | |
@@ -52,7 +60,7 @@ | |
$token = SecurityToken::inst(); | |
if(!$token->checkRequest($request)) return $this->httpError(400); | |
$m = $this->CurrentMember(); | |
- $m->TwitterID = $m->TwitterHandle = null; | |
+ $m->TwitterID = $m->TwitterHandle = $m->TwitterAccessToken = null; | |
$m->write(); | |
} | |
@@ -71,6 +79,7 @@ | |
} | |
$callback = $this->AbsoluteLink('Connect?ret=' . $returnTo); | |
$callback = $token->addToUrl($callback); | |
+ | |
$config = array( | |
'callbackUrl' => $callback, | |
'consumerKey' => self::$consumer_key, | |
@@ -80,6 +89,7 @@ | |
); | |
$consumer = new Zend_Oauth_Consumer($config); | |
$token = $consumer->getRequestToken(); | |
+ | |
Session::set('Twitter.Request.Token', serialize($token)); | |
$url = $consumer->getRedirectUrl(array( | |
'force_login' => 'true' | |
@@ -130,6 +140,7 @@ | |
} | |
try{ | |
$access = $consumer->getAccessToken($req->getVars(), $token); | |
+ | |
$client = $access->getHttpClient($config); | |
$client->setUri('https://api.twitter.com/1/account/verify_credentials.json'); | |
$client->setMethod(Zend_Http_Client::GET); | |
@@ -142,6 +153,7 @@ | |
} catch(Exception $e) { | |
Session::set('FormInfo.TwitterLoginForm_LoginForm.formError.message', $e->getMessage()); | |
Session::set('FormInfo.TwitterLoginForm_LoginForm.formError.type', 'error'); | |
+ | |
return $this->redirect('Security/login#TwitterLoginForm_LoginForm_tab'); | |
} | |
if(!is_numeric($id)) { | |
@@ -149,7 +161,9 @@ | |
Session::set('FormInfo.TwitterLoginForm_LoginForm.formError.type', 'error'); | |
return $this->redirect('Security/login#TwitterLoginForm_LoginForm_tab'); | |
} | |
+ | |
$u = DataObject::get_one('Member', '"TwitterID" = \'' . Convert::raw2sql($id) . '\''); | |
+ | |
if(!$u || !$u->exists()) { | |
Session::set('FormInfo.TwitterLoginForm_LoginForm.formError.message', 'No one found for Twitter account ' . $data->screen_name . '.'); | |
Session::set('FormInfo.TwitterLoginForm_LoginForm.formError.type', 'error'); | |
@@ -157,9 +171,18 @@ | |
} | |
if($u->TwitterHandle != $data->screen_name) { | |
$u->TwitterHandle = $data->screen_name; | |
- $u->write(); | |
} | |
+ | |
+ if($access) { | |
+ $u->TwitterAccessToken = json_encode(array( | |
+ 'oauth_token' => $access->getParam('oauth_token'), | |
+ 'user_secret' => $access->getParam('oauth_token_secret') | |
+ )); | |
+ } | |
+ | |
+ $u->write(); | |
$u->login(Session::get('SessionForms.TwitterLoginForm.Remember')); | |
+ | |
Session::clear('SessionForms.TwitterLoginForm.Remember'); | |
$backURL = Session::get('BackURL'); | |
Session::clear('BackURL'); | |
@@ -191,8 +214,13 @@ | |
$data = $response->getBody(); | |
$data = json_decode($data); | |
+ | |
if($m = $this->CurrentMember()) { | |
$m->TwitterID = $data->id; | |
+ $m->TwitterAccessToken = json_encode(array( | |
+ 'oauth_token' => $access->getParam('oauth_token'), | |
+ 'user_secret' => $access->getParam('oauth_token_secret') | |
+ )); | |
$m->TwitterHandle = $data->screen_name; | |
$m->write(); | |
} else { | |
diff -urN --exclude=.piston.yml --exclude=.git --exclude=.svn /Users/Will/Sites/justsold/.twitter.tmp/Extensions/Identifier.php /Users/Will/Sites/justsold/twitter/Extensions/Identifier.php | |
--- /Users/Will/Sites/justsold/.twitter.tmp/Extensions/Identifier.php 2012-06-19 19:13:19.000000000 +1200 | |
+++ /Users/Will/Sites/justsold/twitter/Extensions/Identifier.php 2012-06-10 18:01:41.000000000 +1200 | |
@@ -1,14 +1,12 @@ | |
<?php | |
-class TwitterIdentifier extends DataObjectDecorator { | |
- public function extraStatics() { | |
- return array( | |
- 'db' => array( | |
- 'TwitterID' => 'Varchar', | |
- 'TwitterHandle' => 'Varchar', | |
- ) | |
- ); | |
- } | |
+class TwitterIdentifier extends DataExtension { | |
+ | |
+ public static $db = array( | |
+ 'TwitterID' => 'Varchar', | |
+ 'TwitterHandle' => 'Varchar', | |
+ 'TwitterAccessToken' => 'Text', | |
+ ); | |
public function updateMemberFormFields(FieldSet &$fields) { | |
$fields->removeByName('TwitterID'); | |
@@ -27,14 +25,20 @@ | |
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); | |
Requirements::javascript(THIRDPARTY_DIR . '/jquery-livequery/jquery.livequery.js'); | |
Requirements::javascript('twitter/javascript/twitter.js'); | |
- if($this->owner->TwitterID) { | |
+ | |
+ if($this->hasTwitter()) { | |
$token = SecurityToken::inst(); | |
$removeURL = Controller::join_links('TwitterCallback', 'RemoveTwitter'); | |
$removeURL = $token->addToUrl($removeURL); | |
- return 'Connected to Twitter account ' . $this->owner->TwitterHandle . '. <a href="' . $removeURL . '" id="RemoveTwitterButton">Disconnect</a>'; | |
+ | |
+ return 'Connected to Twitter account @' . $this->owner->TwitterHandle . '. <a href="' . $removeURL . '" class="unconnect-twitter">Disconnect</a>'; | |
} else { | |
- return '<img src="twitter/Images/connect.png" id="ConnectTwitterButton" alt="Connect to Twitter" />'; | |
+ return '<img src="twitter/Images/connect.png" class="connect-twitter" alt="Connect to Twitter" />'; | |
} | |
} | |
} | |
+ | |
+ function hasTwitter() { | |
+ return ($this->owner->TwitterAccessToken); | |
+ } | |
} | |
diff -urN --exclude=.piston.yml --exclude=.git --exclude=.svn /Users/Will/Sites/justsold/.twitter.tmp/javascript/twitter.js /Users/Will/Sites/justsold/twitter/javascript/twitter.js | |
--- /Users/Will/Sites/justsold/.twitter.tmp/javascript/twitter.js 2012-06-19 19:13:19.000000000 +1200 | |
+++ /Users/Will/Sites/justsold/twitter/javascript/twitter.js 2012-06-10 17:51:13.000000000 +1200 | |
@@ -2,17 +2,29 @@ | |
(function($){ | |
window.TwitterResponse = function(data) { | |
if(data.handle) { | |
- $('#ConnectTwitterButton').replaceWith('Connected to Twitter account ' + data.handle + '. <a href="' + data.removeLink + '" id="RemoveTwitterButton">Disconnect</a>'); | |
+ $('.connect-twitter').replaceWith('Connected to Twitter account @' + data.handle + '. <a href="' + data.removeLink + '" class="unconnect-twitter">Disconnect</a>'); | |
+ $("body").trigger("authchanged"); | |
} | |
}; | |
- $('#ConnectTwitterButton').livequery('click', function (e) { | |
- window.open('TwitterCallback/TwitterConnect').focus(); | |
+ | |
+ $('.connect-twitter').livequery('click', function (e) { | |
+ var url = $("base").get(0).href; | |
+ url += 'TwitterCallback/TwitterConnect'; | |
+ | |
+ window.open(url).focus(); | |
e.stopPropagation(); | |
return false; | |
}); | |
- $('#RemoveTwitterButton').livequery('click', function (e) { | |
+ | |
+ $('.unconnect-twitter').livequery('click', function (e) { | |
$.get($(this).attr('href')); | |
- $(this).parent().html('<img src="twitter/Images/connect.png" id="ConnectTwitterButton" alt="Connect to Twitter" />'); | |
+ | |
+ $('.unconnect-twitter').each(function(i, elem) { | |
+ $(elem).parent().html('<img src="twitter/Images/connect.png" class="connect-twitter" alt="Connect to Twitter" />'); | |
+ }); | |
+ | |
+ $("body").trigger("authchanged"); | |
+ | |
e.stopPropagation(); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment