The Blog

Zend_Auth, Zend_Ldap and Zend_Acl

This took a while to figure out, so I’m going to write an entry on it.

Over the last few days, I’ve been working with the Zend_Auth, Zend_Ldap and Zend_Acl classes in the Zend Framework while developing a login framework. I’ve created custom login frameworks before, using php, coldfusion and java. But this time I wanted to make the most use of these classes, without writing a lot of extra, custom code. I figured that the Zend Framework classes would take care of most everything if I just learned how to use all the features they provide.


First thing I needed was to create a basic login form. I used the Zend_Form for this, and created it inside of a LoginController class. The LoginController class is a basic controller class that extends Zend_Controller_Action. It uses a login action, which calls a function that handles all of the Ldap connections. In the end, I came up with the following class that sits in my /application/controllers/LoginController.php directory.

class LoginController extends Zend_Controller_Action
  private $authenticationErrors = array();

  public function init()

    //only for extending the class constructor and initializing resources
  } //init

  public function indexAction()
    $request = $this->getRequest();
  } //indexAction

  private function authenticate($credentials)
    return $this->authenticateLdap($credentials);
  } //authenticate
  public function loginAction()
    $request = $this->getRequest();
    $username = new Zend_Form_Element_Text('username');
    $usernameValidator = new Zend_Validate_Alnum();

    $password = new Zend_Form_Element_Password('password');
    $submit = new Zend_Form_Element_Submit('submit');
    $loginForm = new Zend_Form();
      $formData = $request->getPost();
        $credentials = new stdclass();
        $credentials->username = $formData['username'];
        $credentials->password = $formData['password'];
          $this->view->authenticationErrors = $this->authenticationErrors;
          $this->view->errorContainer = $this->view->partial('partial/partial-message-login-error.phtml', $this->view);

    $this->view->loginForm = $loginForm;
  } //loginAction

  public function logoutAction()
    $request = $this->getRequest();
    $auth = Zend_Auth::getInstance();
  } //logoutAction

  private function authenticateLdap($credentials)
    $auth = Zend_Auth::getInstance();
    $auth->clearIdentity(); //clear out any existing login information
    $adapter = new Zend_Auth_Adapter_Ldap(
    $result = $auth->authenticate($adapter);
    $ldap = $adapter->getLdap();
    $messages = $result->getMessages();
    array_push($this->authenticationErrors,array('messageType'=>'Error', 'messageText'=>$messages[0]));

      $ldap = $adapter->getLdap();
      $entry = $ldap->getEntry($ldap->getCanonicalAccountName($credentials->username, Zend_Ldap::ACCTNAME_FORM_DN));
      $roles = $entry['memberof'];

      $identity = new stdclass();
      $identity->username = $credentials->username;
      $identity->roles = $roles;
    return $auth->hasIdentity();		
  } //authenticateLdap
} //LoginController

A lot of the LDAP configuration is coming from the config.ini file (/application/config/config.ini). If you want to see more details on how to configure the config.ini file to use it with Active Directory, click here to see the post.


Here’s what the login form looks like when rendered. It uses a view; login.phtml; to render itself to the browser.

Login Form

If I click “Login” without entering any values, these are the errors that will appear:

If I try to submit credentials that don’t exist in Active Directory, the form will display an error message above it that only appears if there are some error message to display:

This is the view code that displays the form (located in the /application/views/scripts/login/login.phtml folder. It doesn’t have much to it, and leaves most of the processing for the action controller.

<?= $this->errorContainer; ?>

<div id="logincontainer">
<?= $this->loginForm; ?> 

Again, the $this->errorContainer only displays when it has data in it. If there aren’t any errors, the loginAction() function will leave this value as null.

Tags: , , ,

No comments yet.

Leave a Comment

Remember to play nicely folks, nobody likes a troll.

You must be logged in to post a comment.