ldap connection working

This commit is contained in:
Christophe Massin 2014-07-17 13:20:53 +02:00
parent c718e76330
commit 3a8e66c438
2 changed files with 47 additions and 67 deletions

View File

@ -77,14 +77,21 @@ class LdapAdapter implements LoginAdapter
public function authenticate() { public function authenticate() {
$adapter = new Ldap(); $adapter = new Ldap();
$adapter->setOptions( $adapter->setOptions(
array($this->getConfiguration()) 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',
))
); );
$adapter->setUsername($this->getUsername()); $adapter->setUsername($this->getUsername());
$adapter->setPassword($this->getPassword()); $adapter->setPassword($this->getPassword());
$result = $adapter->authenticate();
if($result->isValid()){
if($adapter->authenticate()){
$result = $adapter->getAccountObject(); $result = $adapter->getAccountObject();
$params = get_object_vars($result); $params = get_object_vars($result);
@ -92,9 +99,7 @@ class LdapAdapter implements LoginAdapter
$user = new LdapUser(); $user = new LdapUser();
$user->setConfiguration($this->getConfiguration()); $user->setConfiguration($this->getConfiguration());
$user->setRoles(array('http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole')); $user->setUserRawParameters($params);
$user->setLanguageUi($params['preferredlanguage']);
$user->setLanguageDefLg($params['preferredlanguage']);
return $user; return $user;

View File

@ -96,12 +96,7 @@ class LdapUser extends common_user_User {
*/ */
public function setLanguageDefLg($languageDefLgUri) public function setLanguageDefLg($languageDefLgUri)
{ {
$languageResource = new core_kernel_classes_Resource($languageDefLgUri); $this->languageDefLg = array((string)$languageDefLgUri);
$languageCode = $languageResource->getUniquePropertyValue(new core_kernel_classes_Property(RDF_VALUE));
if($languageCode) {
$this->languageDefLg = array((string)$languageCode);
}
return $this; return $this;
} }
@ -132,15 +127,34 @@ class LdapUser extends common_user_User {
return $this->userExtraParameters; return $this->userExtraParameters;
} }
/**
* @param $property string
* @param $value string
*/
public function setUserParameter($property, $value){
$this->userRawParameters[$property] = $value;
}
public function getUserParameter($property) {
if (isset ($this->userRawParameters[$property] ) )
return $this->userRawParameters[$property];
return null;
}
/** /**
* @param rray $userRawParameters * @param array $params
* @return AuthKeyValueUser * @return AuthKeyValueUser
*/ */
public function setUserRawParameters(array $userRawParameters) public function setUserRawParameters(array $params)
{ {
$this->userRawParameters = $userRawParameters; $this->setRoles(array('http://www.tao.lu/Ontologies/TAO.rdf#DeliveryRole'));
isset($params['preferredlanguage']) ? $this->setLanguageUi($params['preferredlanguage']) : DEFAULT_LANG;
isset($params['preferredlanguage']) ? $this->setLanguageDefLg($params['preferredlanguage']) : DEFAULT_LANG;
isset($params['mail']) ? $this->setUserParameter(PROPERTY_USER_MAIL, $params['mail']) : '';
isset($params['displayname']) ? $this->setUserParameter(PROPERTY_USER_LASTNAME, $params['displayname']) : $this->setUserParameter(PROPERTY_USER_LASTNAME, $params['cn']) ;
return $this; return $this;
} }
@ -159,12 +173,7 @@ class LdapUser extends common_user_User {
*/ */
public function setLanguageUi($languageUri) public function setLanguageUi($languageUri)
{ {
$languageResource = new core_kernel_classes_Resource($languageUri); $this->languageUi = array((string)$languageUri);
$languageCode = $languageResource->getUniquePropertyValue(new core_kernel_classes_Property(RDF_VALUE));
if($languageCode) {
$this->languageUi = array((string)$languageCode);
}
return $this; return $this;
} }
@ -204,10 +213,6 @@ class LdapUser extends common_user_User {
{ {
$returnValue = null; $returnValue = null;
$userParameters = $this->getUserRawParameters();
if( !empty($userParameters) && array_key_exists($property, $userParameters))
{
switch ($property) { switch ($property) {
case PROPERTY_USER_DEFLG : case PROPERTY_USER_DEFLG :
$returnValue = $this->getLanguageDefLg(); $returnValue = $this->getLanguageDefLg();
@ -219,37 +224,7 @@ class LdapUser extends common_user_User {
$returnValue = $this->getRoles(); $returnValue = $this->getRoles();
break; break;
default: default:
$returnValue = array($userParameters[$property]); $returnValue = array($this->getUserParameter($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; return $returnValue;