View source
<?php
use Drupal\Core\Cache\Cache;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
function simple_ldap_user_form_user_login_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
foreach ($form['#validate'] as $key => $validation) {
if ($validation === '::validateAuthentication') {
$first_array = array_slice($form['#validate'], 0, $key + 1);
$first_array[] = 'simple_ldap_user_login_validate';
array_splice($form['#validate'], 0, $key + 1, $first_array);
break;
}
}
}
function simple_ldap_user_form_user_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$server = \Drupal::service('simple_ldap.server');
$user = $form_state
->getFormObject()
->getEntity();
$readonly = FALSE;
if ($server
->isReadOnly() && !$user
->isNew() && $user
->id() != 1) {
\Drupal::messenger()
->addWarning(t('Some fields have been marked <em>read-only</em>, as they are controlled by the LDAP server.'));
$readonly = TRUE;
}
$form['account']['name']['#disabled'] = $readonly;
$form['account']['mail']['#disabled'] = $readonly;
$form['account']['pass']['#disabled'] = $readonly;
$form['account']['status']['#disabled'] = $readonly;
array_unshift($form['#validate'], 'simple_ldap_user_profile_validate');
}
function simple_ldap_user_login_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$name = $form_state
->getValue('name');
$password = $form_state
->getValue('pass');
$authenticator = \Drupal::service('simple_ldap_user.auth');
$manager = \Drupal::service('simple_ldap_user.manager');
if (!$authenticator
->canAuthenticate($name)) {
return;
}
$ldap_user = $manager
->getLdapUser($name);
if (!$ldap_user) {
$form_state
->set('uid', FALSE);
$form_state
->setErrorByName('name', t('An account could not be found or an ID conflict has been detected. Please contact your site administrator.'));
return;
}
if (!$authenticator
->authenticate($ldap_user
->getDn(), $password)) {
$form_state
->set('uid', FALSE);
$form_state
->setError($form, t('Could not authenticate with your username/password in LDAP. Please contact your site administrator.'));
return;
}
$user = \Drupal::service('simple_ldap_user.sync')
->importIntoDrupal($ldap_user, $password);
$form_state
->set('uid', $user
->id());
}
function simple_ldap_user_profile_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement() !== 'submit') {
return;
}
$authenticator = \Drupal::service('simple_ldap_user.auth');
$manager = \Drupal::service('simple_ldap_user.manager');
$drupal_user = $form_state
->getFormObject()
->getEntity();
if ($authenticator
->skipCheck($drupal_user)) {
return;
}
$name = $form_state
->getValue('name');
$password = $form_state
->getValue('pass');
$mail = $form_state
->getValue('mail');
$ldap_user_from_name = $manager
->getLdapUser($name);
$ldap_user_from_email = $manager
->getLdapUser($mail);
if ($ldap_user_from_name && $ldap_user_from_email && $ldap_user_from_name
->getDn() != $ldap_user_from_email
->getDn()) {
$form_state
->setErrorByName('account', t('Another user has that username or email address.'));
}
else {
$form_state
->setErrorByName('account', t('Could not find a corresponding LDAP account for the given credentials.'));
}
if (!$authenticator
->authenticate($ldap_user_from_name
->getDn(), $password)) {
$form_state
->setErrorByName('pass', t('The username/password combination could not authenticate to LDAP.'));
}
}
function simple_ldap_user_cron() {
$config = \Drupal::config('simple_ldap.user');
$step_in_seconds = !empty($config
->get('cron_frequency')) ? $config
->get('cron_frequency') : 0;
if (!empty($step_in_seconds)) {
$curr_time = \Drupal::time()
->getRequestTime();
$time = (int) floor($curr_time / $step_in_seconds) * $step_in_seconds;
$state_key = 'simple_ldap_user.blocked_users.last_check';
$expires = \Drupal::state()
->get($state_key, 0);
if ($time <= $expires) {
\Drupal::logger('simple_ldap')
->notice('Simple LDAP user cron update skipped until @expires.', [
'@expires' => date('r', $expires),
]);
return;
}
\Drupal::state()
->set($state_key, $time);
}
$authenticator = \Drupal::service('simple_ldap_user.auth');
$users = User::loadMultiple();
$users = array_filter($users, function (UserInterface $user) use ($authenticator) {
return $authenticator
->canAuthenticate($user
->getAccountName());
});
$manager = \Drupal::service('simple_ldap_user.manager');
$syncer = \Drupal::service('simple_ldap_user.sync');
array_map(function (UserInterface $user) use ($manager, $syncer) {
$ldap_user = $manager
->getLdapUser($user
->getAccountName());
$was_blocked = $user
->isBlocked();
$force_save = FALSE;
if ($ldap_user === FALSE) {
if (!$was_blocked) {
\Drupal::logger('simple_ldap')
->notice('Simple LDAP user cron blocking @name.', [
'@name' => $user
->getAccountName(),
]);
$user
->block();
$user
->save();
}
return;
}
if ($ldap_user && $was_blocked) {
\Drupal::logger('simple_ldap')
->notice('Simple LDAP user cron activating @name.', [
'@name' => $user
->getAccountName(),
]);
$user
->activate();
$force_save = TRUE;
}
$syncer
->updateDrupalUser($ldap_user, $user, $force_save);
}, $users);
}