Skip to content

Commit

Permalink
Login action, profileLink and indexAction in IndexController refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin committed Feb 23, 2011
1 parent 8120d8a commit 15c8760
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 50 deletions.
21 changes: 13 additions & 8 deletions application/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

class IndexController extends Zend_Controller_Action
{
class IndexController extends Zend_Controller_Action {

public function init()
{
public function init() {
/* Initialize action controller here */
}

public function indexAction()
{
// action body
}
public function indexAction() {

$auth = Zend_Auth::getInstance();

if ($auth->hasIdentity()) {
$this->view->identity = $auth->getIdentity();
} else {
$this->view->identity = null;
}


}

}

87 changes: 64 additions & 23 deletions application/controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,20 @@ public function loginAction() {
} else if ('https://www.facebook.com' == $openid_identifier) {
$adapter = $this->_getFacebookAdapter();
} else {
// for openid fetch only email
$toFetch = array("email" => true,'firstname'=> true);
$adapter = $this->_getOpenIdAdapter($openid_identifier, $toFetch );
// for openid
$adapter = $this->_getOpenIdAdapter($openid_identifier);

// specify what to grab from the provider and what extension to use
// for this purpose
$toFetch = array('email' => true);
// for google and yahoo use AtributeExchange Extension
if ('https://www.google.com/accounts/o8/id' == $openid_identifier || 'http://me.yahoo.com/' == $openid_identifier) {
$ext = $this->_getOpenIdExt('ax', $toFetch);
} else {
$ext = $this->_getOpenIdExt('sreg', $toFetch);
}

$adapter->setExtensions($ext);
}

// here a user is redirect to the provider for loging
Expand All @@ -71,22 +82,45 @@ public function loginAction() {
// for twitter
$adapter = $this->_getTwitterAdapter()->setQueryData($_GET);
} else {
// for openid
$adapter = $this->_getOpenIdAdapter();
// for openid
$adapter = $this->_getOpenIdAdapter(null);

// specify what to grab from the provider and what extension to use
// for this purpose
$ext = null;
$toFetch = array('email' => true);
// for google and yahoo use AtributeExchange Extension
if (isset($_GET['openid_ns_ext1']) || isset($_GET['openid_ns_ax'])) {
$ext = $this->_getOpenIdExt('ax', $toFetch);
} else if (isset($_GET['openid_ns_sreg'])) {
$ext = $this->_getOpenIdExt('sreg', $toFetch);
}
if ($ext) {
$ext->parseResponse($_GET);
$adapter->setExtensions($ext);
}
}

$result = $auth->authenticate($adapter);

var_dump($result->getMessages());
var_dump($result->getIdentity());
var_dump($_GET);
// var_dump($ext->getProperties());
return;
// var_dump($result->getMessages());
// var_dump($ext->getProperties());
//
// var_dump($result->getIdentity());
// var_dump($_GET);
//// // var_dump($ext->getProperties());
// return;


if ($result->isValid()) {
$toStore = array('identity' => $auth->getIdentity());

if ($ext) {
$toStore['properties'] = $ext->getProperties();
}
$auth->getStorage()->write($toStore);

$this->_helper->FlashMessenger('Successful OpenID authentication');
$auth->getStorage()->write($this->getRequest()->getParams());
return $this->_redirect('/index/index');
} else {
$this->_helper->FlashMessenger('Failed authentication');
Expand All @@ -99,6 +133,8 @@ public function loginAction() {
public function logoutAction() {
$auth = Zend_Auth::getInstance();
$auth->clearIdentity();
$this->_helper->FlashMessenger('You were logged out');
return $this->_redirect('/index/index');
}

/**
Expand All @@ -125,25 +161,30 @@ protected function _getTwitterAdapter() {
* Get Zend_Auth_Adapter_OpenId adapter
*
* @param string $openid_identifier
* @param array $propertiesToRequest
* @return Zend_Auth_Adapter_OpenId
*/
protected function _getOpenIdAdapter($openid_identifier = null, array $propertiesToRequest = array()) {

$adapter = new Zend_Auth_Adapter_OpenId($openid_identifier);
protected function _getOpenIdAdapter($openid_identifier = null) {
return new Zend_Auth_Adapter_OpenId($openid_identifier);
}

if (!empty($propertiesToRequest)) {
/**
* Get Zend_OpenId_Extension. Sreg or Ax.
*
* @param string $extType Possible values: 'sreg' or 'ax'
* @param array $propertiesToRequest
* @return Zend_OpenId_Extension|null
*/
protected function _getOpenIdExt($extType, array $propertiesToRequest) {

if ('https://www.google.com/accounts/o8/id' == $openid_identifier || 'http://me.yahoo.com/' == $openid_identifier) {
$ext = new My_OpenId_Extension_AttributeExchange($propertiesToRequest);
} else {
$ext = new Zend_OpenId_Extension_Sreg($propertiesToRequest);
}
$ext = null;

$adapter->setExtensions($ext);
if ('ax' == $extType) {
$ext = new My_OpenId_Extension_AttributeExchange($propertiesToRequest);
} elseif ('sreg' == $extType) {
$ext = new Zend_OpenId_Extension_Sreg($propertiesToRequest);
}

return $adapter;
return $ext;
}

}
Expand Down
2 changes: 1 addition & 1 deletion application/layouts/scripts/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<body>
<div>
<h1>OpenID with Zend Framework</h1>
<a href="<?php echo $this->baseUrl();?>">Home</a>
<a href="<?php echo $this->baseUrl();?>">Home</a> | <?php echo $this->profileLink(); ?>
<h2 style="color:green"><?php echo $this->flashMessenger(); ?></h2>


Expand Down
7 changes: 6 additions & 1 deletion application/openid/extension/AttributeExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function getProperties()

private function splitParams($params)
{
$final = array();
$final = array();

// Loop the parameters
foreach ($params as $identifier => $value)
Expand Down Expand Up @@ -255,6 +255,7 @@ public function prepareResponse(&$params)
public function parseResponse($params)
{
$params = $this->splitParams($params);

$ax = null;

// Get the data name space
Expand All @@ -274,6 +275,8 @@ public function parseResponse($params)
}
}



// Check if the data was found
if ($ax == null)
return false;
Expand All @@ -282,6 +285,8 @@ public function parseResponse($params)
if (isset($ax['mode']) && $ax['mode'] != 'fetch_response')
return false;



// Get the attributes
foreach ($ax['value'] as $attr => $value)
{
Expand Down
15 changes: 3 additions & 12 deletions application/views/helpers/ProfileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ public function profileLink() {

$html = '<a href="'.$baseUrl.'/user/login"> Login </a>';

if ($auth->hasIdentity()) {

$identity = $auth->getIdentity();

$html = "You identity:" . $identity['openid_identity'] . '<br />' ;
var_dump($identity);
// $html .= 'Returned from the openID provider:' . var_export($identity) . '<br />';
$html .= '<a href="'.$baseUrl.'/user/logout"> Logout </a>';
return $html;
}


if ($auth->hasIdentity()) {
$html = '<a href="'.$baseUrl.'/user/logout"> Logout </a>';
}

return $html;
}
Expand Down
19 changes: 18 additions & 1 deletion application/views/scripts/index/index.phtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
<div>

<?php echo $this->profileLink();?>
<?php if ($this->identity): ?>

<h2>You are logged</h2>
You identity:
<pre>
<?php echo $this->identity['identity']; ?>
</pre>
<?php if (isset($this->identity['properties'])): ?>
Data fetched:
<pre>
<?php echo var_export($this->identity['properties'], true); ?>
</pre>
<?php endif; ?>

<?php else: ?>
<h2>You are not logged</h2>
<?php endif; ?>


</div>
6 changes: 3 additions & 3 deletions library/Zend/OpenId/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,16 @@ protected function _checkId($immediate, $id, $returnTo=null, $root=null, $extens
if ($server == 'https://www.google.com/accounts/o8/ud') {
$params['openid.identity'] = 'http://specs.openid.net/auth/2.0/identifier_select';
$params['openid.claimed_id'] = 'http://specs.openid.net/auth/2.0/identifier_select';
$params['openid.ns.ax'] = 'http://openid.net/srv/ax/1.0';
$params['openid.ax.mode'] = 'fetch_request';
$params['openid.ns.pape'] = 'http://specs.openid.net/extensions/pape/1.0';
$params['openid.pape.preferred_auth_policies'] = '';
}

if ($server == 'http://www.myopenid.com/server') {
$params['openid.ns.sreg'] = 'http://openid.net/extensions/sreg/1.1';
$params['openid.ns.pape'] = 'http://specs.openid.net/extensions/pape/1.0';
$params['openid.pape.preferred_auth_policies'] = '';
}

}
}

if (isset($handle)) {
Expand Down
3 changes: 2 additions & 1 deletion library/Zend/OpenId/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ abstract class Zend_OpenId_Extension
*/
static public function forAll($extensions, $func, &$params)
{

if ($extensions !== null) {
if (is_array($extensions)) {
foreach ($extensions as $ext) {
if ($ext instanceof Zend_OpenId_Extension) {
if (!$ext->$func($params)) {
if (!$ext->$func($params)) {
return false;
}
} else {
Expand Down

0 comments on commit 15c8760

Please sign in to comment.