My goal is to require login for certain pages. I am using Zend Framework MVC, and I'm trying to find examples regarding best practices.
Some notes on what I'm looking for:
- I want non-logged in users to get a login box, and then return to logged in version of the page, once authenticated
- I want to use dependency injection, and avoid singletons
- Small code footprint - tie into Zend mvc structure
- Should login box be a separate controller and do header redirect? How to return to landing page after auth success? An idea to simply call the login controller action to display the login box in the landing page, or is this a disadvantage regarding search engine indexing?
- Be able to use external library for handling cookies
Or something completely different. I'm fairly new to the Zend framework, and I want to do it 'the right way'.
Use a FrontController plugin and redirect or forward them to your loginAction.
Zend Framework, doesn't currently ship any DI system, however, the Zend_Application_Resource_* actually replace it. What kind of dependency would you need here?
That's up to you.
I mostly use a special
AuthController
withLoginAction
&LogoutAction
. To redirect the user to the page is was trying to view, I always add areturnUrl
element in my forms, and I inject the value of the requested URL to be able to redirect the user, and if none, I redirect him to the index/dashboard, depends.Zend_Auth allows you to set your own storage mechanism, so just implement the interface.
But never store authentication result in a cookie, it's so easy to modify it and access your website.
You may also take a look to one of my previous answer.
You could use the combination of Zend_Auth and Zend_Acl. To extend the other answers I give a short example of how you can manage authentication using zend framework:
First you need to setup a plugin to predispatch all requests and check if the client is allowed to access certain data. This plugin might look like this one:
So every user type has certain permissions that you define in your acl library. On every request you check if the user is allowed to access a resource. If not you redirect to login page, else the preDispatch passes the user to the resource.
In Zend_Acl you define roles, resources and permission, that allow or deny access, e.g.:
Then you have to setup acl and auth in your bootstrap file:
Finally in your authentication controller you have to use a custom auth adapter and setup actions for login and logout:
In your login action you need to pass login data to the auth adapter which performs the authentication.
And that's all. I recommend you this HOW TO on youtube on zend auth and zend acl.
Not sure if this is best practice, but if I were to implement what you are after then I'ld do the following:
LoginForm
with a uniquely identifiable submit button (explained later)AuthService
with methodsgetLoginForm
,login
,logout
and one or all of:getIdentity
,hasIdentity
, and similar methods.hasIdentity
is, either render the LoginForm, or render the identity of logged in user, in the views that need this functionality.preDispatch
plugin hook. Give it some config of which actions to probe. IfAuthService->hasIdentity()
returns true, do nothing. If request is a post request look for the uniquely identifiable submit button name, i.e.:$request->getPost( 'loginSubmitButton', null );
defaulting tonull
if not available. If notnull
do alogin( $request->getPost() )
on the service. If successful, redirect to the same action, if unsuccessful fall through and render the form in the viewhelper again (automatically displaying the errors).The service could be fetched by both the viewhelper and the front controller plugin with some service locator doing something like
ServiceAbstract::getService( 'Auth' )
by registering the service toServiceAbstract
in the bootstrap.