ldap user for test connection

This commit is contained in:
Christophe Massin 2014-07-16 15:48:46 +02:00
parent f81b903ae2
commit 2deda3fd81
3 changed files with 389 additions and 9 deletions

View File

@ -84,18 +84,17 @@ class LdapAdapter implements LoginAdapter
$adapter->setPassword($this->getPassword());
$identity = $adapter->authenticate();
$params=array();
if($adapter->authenticate()){
$result = $adapter->getAccountObject();
$params = get_object_vars($result);
$user = new LdapUser();
if($identity){
$user = new AuthKeyValueUser();
$user->setConfiguration($this->getConfiguration());
$user->setIdentifier($params['uri']);
$user->setRoles($params[PROPERTY_USER_ROLES]);
$user->setLanguageUi($params[PROPERTY_USER_UILG]);
$user->setLanguageDefLg($params[PROPERTY_USER_DEFLG]);
$user->setUserRawParameters($params);
$user->setRoles(array('http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole'));
$user->setLanguageUi($params['preferredlanguage']);
$user->setLanguageDefLg($params['preferredlanguage']);
return $user;

283
model/LdapUser.php Normal file
View File

@ -0,0 +1,283 @@
<?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) 2014 (original work) Open Assessment Technologies SA;
*
*
*/
/**
* Authentication user for key value db access
*
* @author christophe massin
* @package authLdap
*/
namespace oat\authLdap\model;
use common_user_User;
use core_kernel_classes_Resource;
use core_kernel_classes_Property;
use common_Logger;
use SebastianBergmann\Exporter\Exception;
class LdapUser extends common_user_User {
/** @var array of configuration */
protected $configuration;
/**
* @var array
*/
protected $userRawParameters;
/**
* @var array
*/
protected $userExtraParameters = array();
/**
* @var string
*/
protected $identifier;
/** @var array $roles */
protected $roles;
/**
* Array that contains the language code as a single string
*
* @var array
*/
protected $languageUi = array(DEFAULT_LANG);
/**
* Array that contains the language code as a single string
*
* @var array
*/
protected $languageDefLg = array(DEFAULT_LANG);
/**
* @param array $configuration
*/
public function setConfiguration($configuration)
{
$this->configuration = $configuration;
}
/**
* @return array
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* Sets the language URI
*
* @param string $languageDefLgUri
*/
public function setLanguageDefLg($languageDefLgUri)
{
$languageResource = new core_kernel_classes_Resource($languageDefLgUri);
$languageCode = $languageResource->getUniquePropertyValue(new core_kernel_classes_Property(RDF_VALUE));
if($languageCode) {
$this->languageDefLg = array((string)$languageCode);
}
return $this;
}
/**
* Returns the language code
*
* @return array
*/
public function getLanguageDefLg()
{
return $this->languageDefLg;
}
/**
* @param array $userExtraParameters
*/
public function setUserExtraParameters(array $userExtraParameters)
{
$this->userExtraParameters = $userExtraParameters;
}
/**
* @return array
*/
public function getUserExtraParameters()
{
return $this->userExtraParameters;
}
/**
* @param rray $userRawParameters
* @return AuthKeyValueUser
*/
public function setUserRawParameters(array $userRawParameters)
{
$this->userRawParameters = $userRawParameters;
return $this;
}
/**
* @return array
*/
public function getUserRawParameters()
{
return $this->userRawParameters;
}
/**
* @param mixed $language
*/
public function setLanguageUi($languageUri)
{
$languageResource = new core_kernel_classes_Resource($languageUri);
$languageCode = $languageResource->getUniquePropertyValue(new core_kernel_classes_Property(RDF_VALUE));
if($languageCode) {
$this->languageUi = array((string)$languageCode);
}
return $this;
}
/**
* @return array
*/
public function getLanguageUi()
{
return $this->languageUi;
}
/**
* @return string
*/
public function getIdentifier(){
return $this->identifier;
}
/**
* @param $identifier
* @return $this
*/
public function setIdentifier($identifier){
$this->identifier = $identifier;
return $this;
}
/**
* @param $property string
* @return array|null
*/
public function getPropertyValues($property)
{
$returnValue = null;
$userParameters = $this->getUserRawParameters();
if( !empty($userParameters) && array_key_exists($property, $userParameters))
{
switch ($property) {
case PROPERTY_USER_DEFLG :
$returnValue = $this->getLanguageDefLg();
break;
case PROPERTY_USER_UILG :
$returnValue = $this->getLanguageUi();
break;
case PROPERTY_USER_ROLES :
$returnValue = $this->getRoles();
break;
default:
$returnValue = array($userParameters[$property]);
}
} else {
$extraParameters = $this->getUserExtraParameters();
// the element has already been accessed
if(!empty($extraParameters) && array_key_exists($property, $extraParameters)){
if(!is_array($extraParameters[$property])){
$returnValue = array($extraParameters[$property]);
} else {
$returnValue = $extraParameters[$property];
}
} else {
// not already accessed, we are going to get it.
$serviceUser = new AuthKeyValueUserService();
$parameter = $serviceUser->getUserParameter($userParameters[PROPERTY_USER_LOGIN], $property);
$config = $this->getConfiguration();
if(isset($config['max_size_cached_element'])){
if( strlen(base64_encode(serialize($parameter))) < $config['max_size_cached_element'] ) {
$extraParameters[$property] = $parameter;
$this->setUserExtraParameters($extraParameters);
}
} else {
throw new Exception('Missing configuration element max_sized_cached_element');
}
$returnValue = array($parameter);
}
}
return $returnValue;
}
/**
* Function that will refresh the parameters.
*/
public function refresh() {
}
/**
* @return array
*/
public function getRoles() {
return $this->roles;
}
/**
* @param array $roles
* @return $this
*/
public function setRoles(array $roles ) {
$this->roles = $roles;
return $this;
}
}

98
model/LdapUserService.php Normal file
View File

@ -0,0 +1,98 @@
<?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) 2014 (original work) Open Assessment Technologies SA;
*
*
*/
/**
* Authentication service to access db
*
* @author christophe massin
* @package authLdap
*/
namespace oat\authLdap\model;
use common_persistence_AdvKeyValuePersistence;
class AuthLdapUserService {
const PREFIXES_KEY = 'auth';
const USER_PARAMETERS = 'parameters';
/**
* @var \common_persistence_Driver
*/
protected $driver;
public function __construct(){
$kvStore = common_persistence_AdvKeyValuePersistence::getPersistence(AuthKeyValueAdapter::KEY_VALUE_PERSISTENCE_ID);
$this->driver = $kvStore->getDriver();
}
/**
* @param $login
* @return mixed
*/
public function getUserData($login){
return $this->driver->hGetAll(AuthKeyValueUserService::PREFIXES_KEY.':'.$login);
}
/**
* @param $userLogin string
* @param $parameter string
* @return mixed
*/
public function getUserParameter($userLogin, $parameter){
return $this->driver->get(AuthKeyValueUserService::PREFIXES_KEY.':'.$userLogin.':'.$parameter);
}
/**
* @param $userLogin string user login
* @param $parameter string parameter
* @param $value mixed
*/
public function addUserParameter($userLogin, $parameter, $value){
$this->driver->set(AuthKeyValueUserService::PREFIXES_KEY.':'.$userLogin.':'.$parameter, $value);
}
/**
* @param $userLogin string
* @param $parameter string
*/
public function deleteUserParameter($userLogin, $parameter){
$this->driver->del(AuthKeyValueUserService::PREFIXES_KEY.':'.$userLogin.':'.$parameter);
}
/**
* @param $userLogin
* @param $parameter
* @param $value
*/
public function editUserParameter($userLogin, $parameter, $value){
$this->driver->set(AuthKeyValueUserService::PREFIXES_KEY.':'.$userLogin.':'.$parameter, $value);
}
}