generis-auth-ldap/model/LdapAdapter.php

164 lines
3.8 KiB
PHP
Raw Normal View History

2014-07-15 16:02:32 +02:00
<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*
*/
/**
* Authentication adapter interface to be implemented by authentication methodes
*
* @author christophe massin
* @package authKeyValue
*/
namespace oat\authLdap\model;
use core_kernel_users_Service;
use core_kernel_users_InvalidLoginException;
use oat\authKeyValue\model\AuthKeyValueUser;
use oat\oatbox\user\auth\LoginAdapter;
use Zend\Authentication\Adapter\Ldap;
/**
* Adapter to authenticate users stored in the Ldap implementation
*
* @author Christophe Massin <christope@taotesting.com>
*
*/
class LdapAdapter implements LoginAdapter
{
/** Key used to retrieve the persistence information */
CONST KEY_VALUE_PERSISTENCE_ID = 'authLdap';
/** @var $username string */
private $username;
/** @var $password string */
private $password;
/** @var $configuration array $configuration */
protected $configuration;
/**
* @param array $configuration
*/
public function __construct(array $configuration) {
$this->configuration = $configuration;
}
/**
* Set the credential
*
* @param string $login
* @param string $password
*/
public function setCredentials($login, $password){
$this->username = $login;
$this->password = $password;
}
public function authenticate() {
$adapter = new Ldap();
$adapter->setOptions(
2014-07-17 13:20:53 +02:00
array(array(
'host' => '127.0.0.1',
'accountDomainName' => 'test.com',
'username' => 'cn=admin,dc=test,dc=com',
'password' => 'admin',
'baseDn' => 'OU=organisation,dc=test,dc=com',
'bindRequiresDn' => 'true',
))
2014-07-15 16:02:32 +02:00
);
$adapter->setUsername($this->getUsername());
$adapter->setPassword($this->getPassword());
2014-07-17 13:20:53 +02:00
$result = $adapter->authenticate();
2014-07-15 16:02:32 +02:00
2014-07-17 13:20:53 +02:00
if($result->isValid()){
2014-07-15 16:02:32 +02:00
2014-07-16 15:48:46 +02:00
$result = $adapter->getAccountObject();
$params = get_object_vars($result);
$user = new LdapUser();
2014-07-15 16:02:32 +02:00
$user->setConfiguration($this->getConfiguration());
2014-07-17 13:20:53 +02:00
$user->setUserRawParameters($params);
2014-07-15 16:02:32 +02:00
return $user;
} else {
throw new core_kernel_users_InvalidLoginException();
}
}
/**
* @param array $configuration
*/
public function setConfiguration($configuration)
{
$this->configuration = $configuration;
}
/**
* @return array
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
}