View source
<?php
namespace Drupal\simple_ldap_user;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\simple_ldap\SimpleLdapException;
use Drupal\simple_ldap\SimpleLdapServer;
use Drupal\user\UserInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityStorageException;
class SimpleLdapUserManager {
protected $user;
protected $server;
protected $config;
protected $entity_manager;
protected $cache = [];
public function __construct(SimpleLdapServer $server, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager) {
$this->server = $server;
$this->config = $config_factory
->get('simple_ldap.user');
$this->entity_manager = $entity_manager;
}
public function getLdapUser($name) {
$cid = sprintf('LdapUser::%s', $name);
if (array_key_exists($cid, $this->cache)) {
return $this->cache[$cid];
}
$name = $this
->cleanName($name);
$name_attribute = $this->config
->get('name_attribute');
$mail_attribute = $this->config
->get('mail_attribute');
$base_dn = $this->config
->get('basedn');
$scope = $this->config
->get('user_scope');
if (empty($name_attribute) || empty($mail_attribute)) {
throw new SimpleLdapException('Unable to find valid configuration for LDAP User Drupal module.', NULL);
}
$object_classes = $this->config
->get('object_class');
$object_class_filter = '';
if (isset($object_classes)) {
$object_class_filter = '(&(objectclass=' . implode(')(objectclass=', $object_classes) . '))';
}
$filter_list = array();
$filter_list[] = '(&(' . $name_attribute . '=' . $name . ')' . $object_class_filter . ')';
$filter_list[] = '(&(' . $mail_attribute . '=' . $name . ')' . $object_class_filter . ')';
if (!$this->server
->bind()) {
$this->cache[$cid] = FALSE;
return FALSE;
}
foreach ($filter_list as $filter) {
try {
$results = $this->server
->search($base_dn, $filter, $scope, [], 0, 1);
} catch (SimpleLdapException $e) {
if ($e
->getCode() == -1) {
$results = array();
}
else {
$this->cache[$cid] = FALSE;
throw $e;
}
}
if (count($results) == 1) {
$simple_ldap_user = new SimpleLdapUser(key($results), array_shift($results));
$this->cache[$cid] = $simple_ldap_user;
return $simple_ldap_user;
}
}
return FALSE;
}
public function loadDrupalUser(SimpleLdapUser $user) {
$uid = $this
->userIdFromLdapUser($user);
if ($uid === FALSE) {
return FALSE;
}
try {
$user = $this->entity_manager
->getStorage('user')
->load($uid);
return $user;
} catch (PluginException $exception) {
watchdog_exception('simple_ldap_user', $exception);
}
return FALSE;
}
protected function userIdFromLdapUser(SimpleLdapUser $user) {
$attribute_values = $user
->getAttributes();
$name_attribute = $this->config
->get('name_attribute');
$mail_attribute = $this->config
->get('mail_attribute');
$cid = sprintf('uids::%s:%s', $name_attribute, $mail_attribute);
if (array_key_exists($cid, $this->cache)) {
return $this->cache[$cid];
}
$query = $this->entity_manager
->getStorage('user', 'OR')
->getQuery()
->condition('name', $attribute_values[$name_attribute][0])
->condition('mail', $attribute_values[$mail_attribute][0]);
$results = $query
->execute();
$uid = reset($results);
$this->cache[$cid] = $uid;
return $uid;
}
public function createDrupalUser(SimpleLdapUser $user, $password = NULL) {
$attribute_values = $user
->getAttributes();
$name_attribute = $this->config
->get('name_attribute');
$mail_attribute = $this->config
->get('mail_attribute');
$new_user = $this->entity_manager
->getStorage('user')
->create(array(
'name' => $attribute_values[$name_attribute][0],
'mail' => $attribute_values[$mail_attribute][0],
));
if ($password) {
$new_user
->setPassword($password);
}
$new_user
->enforceIsNew();
$new_user
->activate();
try {
$new_user
->save();
} catch (EntityStorageException $e) {
return FALSE;
}
return $new_user;
}
protected function cleanName($name) {
return preg_replace(array(
'/\\(/',
'/\\)/',
), array(
'\\\\(',
'\\\\)',
), $name);
}
}