Skip to content

Commit

Permalink
module
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Picaud committed Aug 20, 2010
1 parent 6a4fd69 commit 40d4dcc
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 13 deletions.
3 changes: 3 additions & 0 deletions config/routing.yml
@@ -0,0 +1,3 @@
melody_access:
url: /access/:service
param: { module: sfMelody, action: access }
12 changes: 0 additions & 12 deletions lib/sfFriendfeedMelody.class.php
Expand Up @@ -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');
}
}
2 changes: 1 addition & 1 deletion lib/sfMelody.class.php
Expand Up @@ -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);
}
Expand Down
84 changes: 84 additions & 0 deletions lib/user/sfMelodyUser.class.php
@@ -0,0 +1,84 @@
<?php
class sfMelodyUser extends sfGuardSecurityUser
{
protected $tokens;

public function connect($service, $force = false)
{
$oauth = sfMelody::getInstance($service);

if(!$this->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);
}
}
38 changes: 38 additions & 0 deletions modules/sfMelody/actions/actions.class.php
@@ -0,0 +1,38 @@
<?php
class sfMelodyActions extends sfActions
{
public function executeAccess(sfWebRequest $request)
{
$service = $request->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);
}
}

0 comments on commit 40d4dcc

Please sign in to comment.