Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if user is logged with Zend Framework?

I'm learning Zend Framework, but I have some doubts about the usage and the concepts.

I want to check if the user is logged to allow access to all the pages. If it is, show the page, if not, display the login the page.

My main doubts are what I need to use to do this (Zend_Auth, Zend_Acl, etc) and where to check if the user is logged (in each controller or the framework automatically checks this for each requisition).

like image 690
Renato Dinhani Avatar asked May 09 '12 14:05

Renato Dinhani


2 Answers

The tool you want to use is Zend_Auth which is quite easy to use when you get the hang of it.

Checking if a user is logged in can be as simple as:-

$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) $loggedIn = true;

See Rob Allen's excellent tutorial on getting started with Zend Auth.

The method I use is to set up a user class that looks after authorisation and access control and inject it into my application as an Action Helper, so that in any of my controllers I can just do:-

$this->user->checkSomething();

The authorisation part needs to affect all the parts of your site which you don't want public and each affected controller needs to check that the user is logged in. For access control, that is done on a per role/per resource basis depending on how fine grained you need to be.See ACL and AUTH in the manual.

like image 142
vascowhite Avatar answered Oct 21 '22 04:10

vascowhite


Want to check if the user is logged in ZendFramework? Try this:

Place this in anywhere of your controller to 'debug', and put it in the top or beginning of your code:

if (Zend_Auth::getInstance()->hasIdentity()) echo "oh yeah I'm logged in lol"; die;
like image 2
Raymond Avatar answered Oct 21 '22 04:10

Raymond