[Solar-talk] Verifying Login Status
Paul M Jones
pmjones at solarphp.com
Thu Feb 7 09:43:01 CST 2008
On Feb 7, 2008, at 09:30 , Sean Montague wrote:
> Just as a follow up, I modified my Vendor_App_Base to look like this:
>
> function _setup() {
> // Register a Solar_Sql object if not already
> if (! Solar_Registry::exists('sql')) {
> Solar_Registry::set('sql', Solar::factory('Solar_Sql'));
> }
> // Register a Solar_User object if not already.
> // This will trigger the authentication process.
> if (! Solar_Registry::exists('user')) {
> Solar_Registry::set('user',
> Solar::factory('Solar_User'));
> }
> $auth = Solar::factory('Solar_Auth');
> $auth->start();
> Solar_Registry::set('auth', $auth);
> if (Solar_Registry::get('auth')->isValid()) {
> // user is logged in
> } else {
> // user is *not* logged in
> $uri = Solar::factory('Solar_Uri');
> if($uri->path[0] != 'Login') {
> ob_start();
> header("Location: http://solartutorial/Login");
> ob_end_flush();
> exit;
> }
> }
> }
>
> and it works great. Thanks!
I think we can do better even than that. Instead of ...
$auth = Solar::factory('Solar_Auth');
$auth->start();
Solar_Registry::set('auth', $auth);
if (Solar_Registry::get('auth')->isValid()) {
// user is logged in
} else {
// user is *not* logged in
... you should be able to do this:
$user = Solar_Registry::get('user');
if ($user->auth->isValid()) {
// user is logged in
} else {
// user is not logged in
First, the Solar_User class keeps a Solar_Auth object as a public
property. Second, when Solar_User is instantiated (e.g., via
Solar::factory()), it calls $this->auth->start() for you
automatically. Take a look at the Solar_User::__construct() method
for more information.
Welcome aboard! :-)
-- pmj
More information about the Solar-talk
mailing list