Created
July 11, 2012 21:22
-
-
Save LouisLandry/3093630 to your computer and use it in GitHub Desktop.
Example identity usage for web application.
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
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Identity | |
* | |
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die; | |
/** | |
* Identity value object for the Joomla Platform. | |
* | |
* @package Joomla.Platform | |
* @subpackage Identity | |
* @see JObject | |
* @since 12.3 | |
*/ | |
class JIdentity extends JObject | |
{ | |
public $accountId; | |
public $name; | |
public $email; | |
public $password; | |
public $block; | |
public $sendEmail; | |
public $registerDate; | |
public $lastVisitDate; | |
public $activation; | |
public $params; | |
public $links = array(); | |
} |
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
<?php | |
/** | |
* @package Example.Web | |
* @subpackage Application | |
* | |
* @license GNU General Public License version 2 or later. | |
*/ | |
/** | |
* Example Web Application Class | |
* | |
* @package Example.Web | |
* @subpackage Application | |
* @since 1.0 | |
*/ | |
class EApplicationWeb extends JApplicationWeb | |
{ | |
// ... | |
/** | |
* Get the application identity. | |
* | |
* @return mixed A JIdentity object or null. | |
* | |
* @since 12.1 | |
*/ | |
public function getIdentity() | |
{ | |
return $this->identity; | |
} | |
/** | |
* Allows the application to load a custom or default identity. | |
* | |
* @param JIdentity $identity An optional identity object. If omitted, the factory user is created. | |
* | |
* @return EApplicationWeb This method is chainable. | |
* | |
* @since 1.0 | |
*/ | |
public function loadIdentity(JIdentity $identity = null) | |
{ | |
if ($identity) | |
{ | |
$this->identity = $identity; | |
} | |
elseif ($this->session instanceof JSession) | |
{ | |
$this->identity = $this->session->get('user', new JIdentity); | |
} | |
else | |
{ | |
$this->identity = new JIdentity; | |
} | |
return $this; | |
} | |
/** | |
* Allows the application to load a custom or default session. | |
* | |
* @param JSession $session An optional session object. If omitted, the session is created. | |
* | |
* @return EApplicationWeb This method is chainable. | |
* | |
* @since 1.0 | |
*/ | |
public function loadSession(JSession $session = null) | |
{ | |
if ($session !== null) | |
{ | |
$this->session = $session; | |
return $this; | |
} | |
// Get the session handler from the configuration. | |
$handler = $this->get('session.handler', 'none'); | |
// Initialize the options for JSession. | |
$options = array( | |
'name' => $this->get('session.name', md5(get_class($this) . $this->get('uri.base.host'))), | |
'expire' => (($this->get('session.lifetime')) ? $this->get('session.lifetime') * 60 : 900), | |
'force_ssl' => $this->get('force_ssl') | |
); | |
// Instantiate the session object. | |
$session = JSession::getInstance($handler, $options); | |
if ($session->getState() == 'expired') | |
{ | |
$session->restart(); | |
} | |
// If the session is new, load the user and registry objects. | |
if ($session->isNew()) | |
{ | |
$session->set('registry', new JRegistry); | |
$session->set('user', new JIdentity); | |
} | |
// Initialise the input for the session. | |
$session->initialise($this->input); | |
$session->start(); | |
// Set the session object. | |
$this->session = $session; | |
return $this; | |
} | |
/** | |
* Allows the application to set and persist an identity. | |
* | |
* @param JIdentity $identity An optional identity object. If omitted, the factory user is created. | |
* | |
* @return EApplicationWeb This method is chainable. | |
* | |
* @since 1.0 | |
*/ | |
public function setIdentity(JIdentity $identity) | |
{ | |
$this->identity = $identity; | |
// Persist the identity into the session. | |
$this->session->set('user', $this->identity); | |
return $this; | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment