« Return to Thread: Zend_Acl / Zend_Auth example scenario
Simon Mundy wrote:Login (Authentication)
The act of authentication - in my app - all happens within a domain
model - MyForm_Login. Using Matt Zandstra's excellent reference on
Observers/Observable at zend.com as a starting point I have created a
form object that extends the PEAR HTML_Quickform component to allow
one or more observers to be added to the form and activated upon
validation.
The form is constructed (and auto-populated in my domain-specific
instance with form elements like 'Username', 'Password' and a
'Remember Me' checkbox), then several observers are added to it.
When a form validates, the observers are all notified and given an
instance of the form values and the Zend_Auth instance. From there,
it is simply a matter of checking the sanitised form values (we've
applied our form filters, right? :) and passing them to a domain-
specific Zend_Auth_Identity object to query the database, perform a
lookup and then either spit out an error message or start the login
session.
The example below would also create a hypothetical log observer to
record the login time, date, details, etc.
BTW, in case you're wondering why the $view->render() isn't called,
it's because I generally have a View_Plugin that's registered in the
the Front_Controller that kicks in during dispatch shutdown. It
allows me to incrementally add components/properties to the view as
the dispatcher loops through all the application actions.
LoginController.php
class LoginController extends Zend_Controller_Action
{
public function indexAction()
{
$auth = $this->getInvokeArg('auth');
$view = $this->getInvokeArg('view');
if ($auth->hasIdentity()) {
$this->_redirect('/home/index'); // Already authenticated?
Navigate away
}
$form = new MyForm_Login(); // creates all fields, adds
filters, etc...
$form->attach(new MyPlugin_Login_User($auth); // Perform
login of user identity
$form->attach(new MyPlugin_Login_Log($auth); // Perhaps log
the event?
if ($form->validate()) {
$this->_redirect('/home/index');
}
// Render page
$this->getInvokeArg('view')->title = 'Login';
$this->getInvokeArg('view')->template = 'login/index.tpl';
$this->getInvokeArg('view')->form = $form->render();
}
public function agreementAction()
{
$auth = $this->getInvokeArg('auth');
$view = $this->getInvokeArg('view');
$form = new MyForm_Agreement();
$form->attach(new MyPlugin_Agreement_User($auth));
if ($form->validate()) {
$this->_redirect('/home/index');
}
// Render page
$this->getInvokeArg('view')->title = 'Agreement';
$this->getInvokeArg('view')->template = 'login/agreement.tpl';
$this->getInvokeArg('view')->form = $form->render();
}
}
User.php
class MyPlugin_Login_User implements Observer
{
function notify($form)
{
$auth = $this->_auth;
$values = $form->exportValues();
$adapter = new MyAuth_Adapter();
$adapter->setUsername($values['username']);
$adapter->setPassword($values['password']);
try {
$auth->authenticate($adapter);
} catch (MyAuth_Adapter_Exception_Missing $e) {
// Let form know that login has failed...
} catch (MyAuth_Adapter_Exception_Locked $e) {
// Let form know that login has failed...
}
if (!$auth->isAuthenticated()) {
// Let form know that password was incorrect or your
account is not active...
}
$identity = $auth->getIdentity();
// Retrieve row of user info and store inside Identity
object (including role!)
$userTable = new MyUser_Table; // Instance of Zend_Db_Table
or similar...
$identity->setUser($userTable->find($identity->getIdentifier
()));
}
}
« Return to Thread: Zend_Acl / Zend_Auth example scenario
| Free embeddable forum powered by Nabble | Forum Help |