So, i'm trying to make a Ldap authentication, following the docs in
https://zf2.readthedocs.org/en/latest/user-guide/skeleton-application.html
I just replace the 'Album' module with a 'Auth' module and use the sample code for Ldap autentication
https://zf2.readthedocs.org/en/latest/modules/zend.authentication.adapter.ldap.html
but i'm getting this error where Zend can't find the Ldap class when i submit the form with the login data.
Fatal error: Class 'Zend\Ldap\Ldap' not found in C:\wamp\www\sade\vendor\zendframework\zend-authentication\src\Adapter\Ldap.php on line 139
I'm using composer, for the zend framework and the atuoloader for the modules, i already have errors related a class not found but my own classes, not Zend classes like Ldap.
Thanks.
Here is the structure of the files and folders
/module
/Auth
/config
module.config.php
/src
/Auth
/Controller
AuthController.php
/Form
AuthForm.php
/Model
Auth.php
/view
/auth
/auth
index.php
Module.php
And the main code of the module:
module.config.php
return array(
'controllers' => array(
'invokables' => array(
'Auth\Controller\Auth' => 'Auth\Controller\AuthController',
),
),
'router' => array(
'routes' => array(
'auth' => array(
'type' => 'segment',
'options' => array(
'route' => '/auth[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Auth\Controller\Auth',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'auth' => __DIR__ . '/../view',
),
),
);
AuthController.php
//AuthController.php
namespace Auth\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\Ldap as AuthAdapter;
use Zend\Config\Reader\Ini as ConfigReader;
use Zend\Config\Config;
use Zend\Log\Logger;
use Zend\Log\Writer\Stream as LogWriter;
use Zend\Log\Filter\Priority as LogFilter;
use Auth\Model\Auth;
use Auth\Form\AuthForm;
class AuthController extends AbstractActionController
{
public function indexAction()
{
$form = new AuthForm();
$form->get('submit')->setValue('index');
$request = $this->getRequest();
if ($request->isPost()) {
$Auth = new Auth();
$Auth->username = $this->getRequest()->getPost('username');
$Auth->password = $this->getRequest()->getPost('password');
$AuthenticationService = new AuthenticationService();
$configReader = new ConfigReader();
$configData = $configReader->fromFile('config/ldap-config.ini');
$config = new Config($configData, true);
$log_path = $config->production->ldap->log_path;
//die($log_path)."--";
$options = $config->production->ldap->toArray();
unset($options['log_path']);
$adapter = new AuthAdapter($options,
$Auth->username,
$Auth->password);
$form->setInputFilter($Auth->getInputFilter());
$form->setData($request->getPost());
$result = $AuthenticationService->authenticate($adapter);
if ($log_path) {
$messages = $result->getMessages();
$logger = new Logger;
$writer = new LogWriter($log_path);
$logger->addWriter($writer);
$filter = new LogFilter(Logger::DEBUG);
$writer->addFilter($filter);
foreach ($messages as $i => $message) {
if ($i-- > 1) { // $messages[2] and up are log messages
$message = str_replace("\n", "\n ", $message);
$logger->debug("Ldap: $i: $message");
}
}
}
//return $this->redirect()->toRoute('login');
}
return array('form' => $form);
}
public function LoginAction()
{
}
public function LogoutAction()
{
}
}
AuthForm.php
//AuthForm.php
namespace Auth\Form;
use Zend\Form\Form;
class AuthForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('auth');
$this->add(array(
'name' => 'username',
'type' => 'Text',
'options' => array(
'label' => 'Nombre de usuario',
),
));
$this->add(array(
'name' => 'password',
'type' => 'Password',
'options' => array(
'label' => 'Contraseña',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Iniciar sesión',
'id' => 'submitbutton',
),
));
}
}
Auth.php (inside Model folder, for filters that i may implement later)
namespace Auth\Model;
// Add these import statements
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class Auth implements InputFilterAwareInterface
{
public $username;
public $password;
protected $inputFilter; // <-- Add this variable
public function exchangeArray($data)
{
$this->username = (isset($data['username'])) ? $data['username'] : null;
$this->password = (isset($data['password'])) ? $data['password'] : null;
}
// Add content to these methods:
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
And Module.php, i don't think that index.phtml needs to be posted here.
namespace Auth;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function getAutoloaderConfig()
{
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}