Skip to content
Snippets Groups Projects
Commit 11cf40e5 authored by imiksu's avatar imiksu Committed by Rodrigue Tusse
Browse files

Resolve #3134109 "Make emailing configurable"

parent 5021af6f
No related branches found
No related tags found
No related merge requests found
......@@ -10,7 +10,7 @@ CONTENTS OF THIS FILE
INTRODUCTION
------------
The Block Inactive Users module automatically blocks user who haven't been
The Block Inactive Users module automatically blocks user who haven't been
active for a designated amount of time.
* For a full description of the module, visit the project page:
......@@ -37,12 +37,12 @@ CONFIGURATION
-------------
1. Navigate to "Adminstration > Extend" and enable module.
2. Navigate to "Adminstration > People > Block inactive users"
2. Navigate to "Adminstration > Configuration > People > Block inactive users"
to configure options for automatically blocking inactive users.
3. Enter the time period, in months, of inactivity to disable users.
4. Configure an email for the inactive user. Fill in the admin's email
address and the subject fields. Use of tokens is available in the help
text.
5. Pressing the 'Disable inactive users' button will immediately
5. Pressing the 'Disable inactive users' button will immediately
invoke the script.
6. Save configuration to run on the next Drupal cron jobs.
......@@ -11,3 +11,14 @@
function block_inactive_users_uninstall() {
\Drupal::configFactory()->getEditable('block_inactive_users.settings')->delete();
}
/**
* Adding config option to send/not send email on processing.
* Set to 1 to keep backward compatibility.
*/
function block_inactive_users_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('block_inactive_users.settings');
$config->set('block_inactive_users_send_email', 1);
$config->save(TRUE);
}
......@@ -47,6 +47,7 @@ function block_inactive_users_block_users() {
$idle_time = $config->get('block_inactive_users_idle_time');
$exclude_user_roles = $config->get('block_inactive_users_exclude_roles');
$include_never_accessed = $config->get('block_inactive_users_include_never_accessed');
$send_email = $config->get('block_inactive_users_send_email');
$query = \Drupal::entityQuery('user')->condition('status', 1);
if (!empty($exclude_user_roles)) {
......@@ -64,14 +65,16 @@ function block_inactive_users_block_users() {
if ($last_access != 0 && !$user->hasRole('administrator')) {
if ($usersHandler->timestampdiff($last_access, $current_time) >= $idle_time) {
$usersHandler->disableInactiveUsersStatus($user);
$usersHandler->disableInactiveUsersStatus($user, $send_email);
}
}
// If option enabled to include blocking of users who have never logged in.
// Calculate the creation time and block the user if the idle time period has elapsed.
// Calculate the creation time and block the user if the idle time period
// has elapsed.
if ($include_never_accessed == 1 && $last_access == 0) {
if ($usersHandler->timestampdiff($user->getCreatedTime(), $current_time) >= $idle_time) {
$usersHandler->disableInactiveUsersStatus($user);
$usersHandler->disableInactiveUsersStatus($user, $send_email);
}
}
}
......
......@@ -90,7 +90,7 @@ class SettingsForm extends ConfigFormBase {
'#attributes' => [
'min' => 0,
],
'#default_value' => $config->get('block_inactive_users.block_inactive_users_idle_time'),
'#default_value' => $config->get('block_inactive_users_idle_time'),
'#description' => $this->t('Disable inactive users.'),
];
......@@ -98,7 +98,7 @@ class SettingsForm extends ConfigFormBase {
'#type' => 'select',
'#multiple' => TRUE,
'#title' => $this->t('Exclude users with role(s):'),
'#default_value' => $config->get('block_inactive_users.block_inactive_users_exclude_roles'),
'#default_value' => $config->get('block_inactive_users_exclude_roles'),
'#options' => $roles,
'#description' => $this->t('Select role(s) to avoid disabling account.'),
];
......@@ -106,33 +106,48 @@ class SettingsForm extends ConfigFormBase {
$form['users_settings']['block_inactive_users_include_never_accessed'] = [
'#type' => 'checkbox',
'#title' => $this->t('Include users who have never logged in'),
'#default_value' => $config->get('block_inactive_users.block_inactive_users_include_never_accessed'),
'#default_value' => $config->get('block_inactive_users_include_never_accessed'),
'#description' => $this->t('If checked, will block users whose last access is set to "never."'),
];
$form['email_settings']['block_inactive_users_send_email'] = [
'#type' => 'checkbox',
'#title' => $this->t('Send email'),
'#default_value' => $config->get('block_inactive_users_send_email'),
'#description' => $this->t('If checked, will send email to blocked user. When unchecked, will "silently" block.'),
'#attributes' => [
'name' => 'block_inactive_users_send_email',
],
];
$form['email_settings']['block_inactive_users_from_email'] = [
'#title' => $this->t('From'),
'#required' => TRUE,
'#type' => 'textfield',
'#default_value' => $config->get('block_inactive_users.block_inactive_users_from_email'),
'#default_value' => $config->get('block_inactive_users_from_email'),
'#description' => $this->t('Email "from" email address configuration.'),
'#states' => [
// only required if the selected to send emails
'required' => [
':input[name="block_inactive_users_send_email"]' => ['checked' => TRUE],
],
],
];
$form['email_settings']['block_inactive_users_email_subject'] = [
'#title' => $this->t('Email subject'),
'#type' => 'textfield',
'#default_value' => $config->get('block_inactive_users.block_inactive_users_email_subject'),
'#default_value' => $config->get('block_inactive_users_email_subject'),
'#description' => $this->t('Email subject text.'),
];
$form['email_settings']['block_inactive_users_email_content'] = [
'#title' => $this->t('Email template'),
'#type' => 'textarea',
'#default_value' => $config->get('block_inactive_users.block_inactive_users_email_content'),
'#default_value' => $config->get('block_inactive_users_email_content'),
'#description' => $this->t('Provide a template of email to be sent to disabled users. <br/>Replacements values available: [account-name], [site-name], [activation-link].'),
];
$form['actions']['block_inactive_users.block_inactive_users_update'] = [
$form['actions']['block_inactive_users_update'] = [
'#type' => 'submit',
'#value' => $this->t('Disable inactive users'),
'#submit' => ['::updateUsers'],
......@@ -148,12 +163,13 @@ class SettingsForm extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->configFactory->getEditable('block_inactive_users.settings')
->set('block_inactive_users.block_inactive_users_idle_time', $form_state->getValue('block_inactive_users_idle_time'))
->set('block_inactive_users.block_inactive_users_from_email', $form_state->getValue('block_inactive_users_from_email'))
->set('block_inactive_users.block_inactive_users_email_content', $form_state->getValue('block_inactive_users_email_content'))
->set('block_inactive_users.block_inactive_users_email_subject', $form_state->getValue('block_inactive_users_email_subject'))
->set('block_inactive_users.block_inactive_users_exclude_roles', $form_state->getValue('block_inactive_users_exclude_roles'))
->set('block_inactive_users.block_inactive_users_include_never_accessed', $form_state->getValue('block_inactive_users_include_never_accessed'))
->set('block_inactive_users_idle_time', $form_state->getValue('block_inactive_users_idle_time'))
->set('block_inactive_users_from_email', $form_state->getValue('block_inactive_users_from_email'))
->set('block_inactive_users_email_content', $form_state->getValue('block_inactive_users_email_content'))
->set('block_inactive_users_email_subject', $form_state->getValue('block_inactive_users_email_subject'))
->set('block_inactive_users_exclude_roles', $form_state->getValue('block_inactive_users_exclude_roles'))
->set('block_inactive_users_include_never_accessed', $form_state->getValue('block_inactive_users_include_never_accessed'))
->set('block_inactive_users_send_email', $form_state->getValue('block_inactive_users_send_email'))
->save();
parent::submitForm($form, $form_state);
......
......@@ -8,6 +8,8 @@ use Drupal\user\Entity\User;
/**
* Checks that automatic block of old users works as expected.
*
* @group block_inactive_users
*/
class BlockUsersTest extends KernelTestBase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment