useLdap();
}
public function useLdap()
{
$this->checkConfiguration();
return $this->USE_LDAP;
}
private function buildFakeLdapUsers()
{
return $this->buildFakeLdapUsersFromDB();
}
private function buildFakeLdapUsersFromDB()
{
$users = TableRegistry::get('Users')->find();
$ldapUsers = [];
foreach ($users as $user) {
$names = explode(" ", $user['nom']);
if (isset($names[1])) {
$ldapUsers[] = [
'sn' => [
$names[0]
],
'mail' => [
$user['email']
],
'givenname' => [
$names[1]
],
$this->authenticationType => [
$user['username']
],
'userpassword' => [
$user['password']
]
];
} else {
$ldapUsers[] = [
'sn' => [
$names[0]
],
'mail' => [
$user['email']
],
'givenname' => " ",
$this->authenticationType => [
$user['username']
],
'userpassword' => [
$user['password']
]
];
}
}
// EP (aout 2017)
// ATTENTION : Utilisateur IMPORTANT.
// Avec cet utilisateur, on simule un utilisateur qui n'est PAS dans la table utilisateurs
// Il devrait donc se voir attribuer un role "Utilisateur" sans pour autant que ça soit écrit dans la table !!!
// login = '_NouvelUtilisateur_username'
// pass = '_NouvelUtilisateur_password'
// $prefix = "_NouvelUtilisateur_";
$ldapUsers[] = [
'sn' => [
'UTILISATEUR'
],
'givenname' => [
'FAKE_LDAP'
],
// 'mail' => [$login.'email'],
'mail' => [
'fakeldapuser@domain.fr'
],
// $this->authenticationType => [$prefix.'username'],
$this->authenticationType => [
$this->getTheFakeLdapUser()['login']
],
// $this->authenticationType => ['usere'],
'userpassword' => [
$this->getTheFakeLdapUser()['pass']
]
// 'userpassword' => ['toto'],
];
return $ldapUsers;
}
private function checkConfiguration()
{
$config = TableRegistry::get('Configurations')->find()
->where([
'id =' => 1
])
->first();
$this->USE_LDAP = $config->use_ldap ? TRUE : FALSE;
if (! $this->USE_LDAP) {
$this->authenticationType = $config->authentificationType_ldap;
if (empty($this->fakeLDAPUsers))
$this->fakeLDAPUsers = $this->buildFakeLdapUsers();
return true;
}
// debug($this->fakeLDAPUsers);
$ldapConfig = $config->toArray();
if (! empty($config->host_ldap) && ! empty($config->port_ldap) && ! empty($config->baseDn_ldap) && ! empty($config->authentificationType_ldap) && ! empty($config->filter_ldap)) {
$this->host = $config->host_ldap;
$this->port = $config->port_ldap;
$this->baseDn = $config->baseDn_ldap;
$this->filter = $config->filter_ldap;
$this->authenticationType = $config->authentificationType_ldap;
return true;
}
throw new Exception('The ldap configuration is not valid :