From 40d4dcc74e5e438ff5ee05a6d3f87c7c77e5c2ae Mon Sep 17 00:00:00 2001 From: Maxime Picaud Date: Fri, 20 Aug 2010 12:03:29 +0200 Subject: [PATCH] module --- config/routing.yml | 3 + lib/sfFriendfeedMelody.class.php | 12 ---- lib/sfMelody.class.php | 2 +- lib/user/sfMelodyUser.class.php | 84 ++++++++++++++++++++++ modules/sfMelody/actions/actions.class.php | 38 ++++++++++ 5 files changed, 126 insertions(+), 13 deletions(-) create mode 100644 config/routing.yml create mode 100644 lib/user/sfMelodyUser.class.php create mode 100644 modules/sfMelody/actions/actions.class.php diff --git a/config/routing.yml b/config/routing.yml new file mode 100644 index 0000000..ce3350b --- /dev/null +++ b/config/routing.yml @@ -0,0 +1,3 @@ +melody_access: + url: /access/:service + param: { module: sfMelody, action: access } \ No newline at end of file diff --git a/lib/sfFriendfeedMelody.class.php b/lib/sfFriendfeedMelody.class.php index 22e58df..58fbfa8 100644 --- a/lib/sfFriendfeedMelody.class.php +++ b/lib/sfFriendfeedMelody.class.php @@ -7,16 +7,4 @@ protected function initialize($config) $this->request_auth_url = 'https://friendfeed.com/account/oauth/authorize'; $this->access_token_url = 'https://friendfeed.com/account/oauth/access_token'; } - - public function getContacts($uid) - { - $url = 'http://social.yahooapis.com/v1/user/'.$uid.'/contacts'; - $this->params = array('format' => 'json'); - $request = OAuthRequest::from_consumer_and_token($this->getConsumer(), $this->getToken(), 'GET', $url, $this->params); - $request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->getConsumer(), $this->getToken()); - - $url = $request->to_url(); - - return $this->call($url, null, 'GET'); - } } \ No newline at end of file diff --git a/lib/sfMelody.class.php b/lib/sfMelody.class.php index 27ab03c..cb0e927 100644 --- a/lib/sfMelody.class.php +++ b/lib/sfMelody.class.php @@ -48,7 +48,7 @@ public static function getTokenStatuses() public static function deleteTokens($service = null, $user = null, $status = null) { - $callable = array(self::getTokenOperationByOrm(), 'deleteRequestTokens'); + $callable = array(self::getTokenOperationByOrm(), 'deleteTokens'); call_user_func($callable, $service, $user, $status); } diff --git a/lib/user/sfMelodyUser.class.php b/lib/user/sfMelodyUser.class.php new file mode 100644 index 0000000..46ce2e4 --- /dev/null +++ b/lib/user/sfMelodyUser.class.php @@ -0,0 +1,84 @@ +isConnected($service) || $force) + { + if($token = $this->getToken($service, Token::STATUS_REQUEST)) + { + sfMelody::deleteTokens($service, $this->getGuardUser(), Token::STATUS_REQUEST); + } + + if($this->isConnected($service) && $force) + { + sfMelody::deleteTokens($service, $this->getGuardUser(), Token::STATUS_ACCESS); + } + + $this->setAttribute('callback_'.$service, $oauth->getCallback()); + $oauth->setCallback('@melody_access?service='.$service); + $oauth->connect($this->getGuardUser()); + } + else + { + $oauth->getController()->redirect($oauth->getCallback()); + } + } + + public function isConnected($service) + { + return !is_null($this->getToken($service, Token::STATUS_ACCESS)); + } + + public function getToken($service, $status = null) + { + $tokens = $this->getTokens(); + + if(!is_null($status)) + { + return isset($tokens[$status][$service])?$tokens[$status][$service]:null; + } + else + { + foreach(sfMelody::getTokenStatuses() as $status) + { + if(isset($tokens[$status][$service])) + { + return $tokens[$status][$service]; + } + } + } + + return null; + } + + public function getTokens() + { + if(is_null($this->tokens)) + { + $callable = array(sfMelody::getTokenOperationByOrm(), 'findByUserId'); + + $tokens = call_user_func($callable, $this->getGuardUser()->getId()); + + $this->tokens = array(); + foreach($tokens as $token) + { + $this->tokens[$token->getStatus()][$token->getName()] = $token; + } + } + + return $this->tokens; + } + + public function getApi($service, $config = array()) + { + $token = $this->getToken($service, Token::STATUS_ACCESS); + $config = array_merge(array('token' => $token), $config); + + return sfMelody::getInstance($service, $config); + } +} \ No newline at end of file diff --git a/modules/sfMelody/actions/actions.class.php b/modules/sfMelody/actions/actions.class.php new file mode 100644 index 0000000..bdce838 --- /dev/null +++ b/modules/sfMelody/actions/actions.class.php @@ -0,0 +1,38 @@ +getParameter('service'); + $token = $this->getUser()->getToken($service, Token::STATUS_REQUEST); + + $oauth = sfMelody::getInstance($service, array('token' => $token)); + + if($oauth->getVersion() == 1) + { + $code = $request->getParameter('oauth_verifier'); + } + else + { + $code = $request->getParameter('code'); + } + + $callback = $oauth->getCallback(); + //for oauth 2 the same redirect_uri + $oauth->setCallback('@melody_access?service='.$service); + $extra_params = $oauth->getAccessToken($code); + + if($token) + { + $token->delete(); + } + + $access_token = $oauth->getToken(); + $access_token->setUser($this->getUser()->getGuardUser()); + $access_token->save(); + + $this->getUser()->setAttribute($service.'_extra_params', $extra_params); + + $this->redirect($callback); + } +} \ No newline at end of file