Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxime Picaud
committed
Aug 20, 2010
1 parent
6a4fd69
commit 40d4dcc
Showing
5 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
melody_access: | ||
url: /access/:service | ||
param: { module: sfMelody, action: access } |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |