RoleForceForm.php in Force Password Change 2.0.x
File
src/Form/RoleForceForm.php
View source
<?php
namespace Drupal\force_password_change\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface;
use Drupal\user\RoleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RoleForceForm extends FormBase {
protected $configFactory;
protected $passwordChangeService;
protected $dateFormatter;
public function __construct(ConfigFactoryInterface $configFactory, DateFormatterInterface $dateFormatter, ForcePasswordChangeServiceInterface $passwordChangeService) {
$this->configFactory = $configFactory;
$this->dateFormatter = $dateFormatter;
$this->passwordChangeService = $passwordChangeService;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('date.formatter'), $container
->get('force_password_change.service'));
}
public function getFormId() {
return 'force_password_change_role_admin_form';
}
public function buildForm(array $form, FormStateInterface $form_state, RoleInterface $role = NULL) {
if ($role) {
if ($this->configFactory
->get('force_password_change.settings')
->get('check_login_only')) {
$description = $this
->t('Users will be required to change their password upon their next login.');
}
else {
$description = $this
->t('Users who are not signed in will be required to change their password immediately upon login. Users who are currently signed in will be required to change their password upon their next page click, but after changing their password will be redirected back to the page they were attempting to access.');
}
$description .= '<br />' . $this
->t('Note: When you return to this page, this box will be unchecked. This is because this setting is a trigger, not a persistant state.');
$form['force_password_change'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Force users in this role to change their password'),
'#description' => $description,
'#weight' => -1,
];
$form['role'] = [
'#type' => 'value',
'#value' => $role,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Force Password Change'),
];
}
else {
$form['no_role'] = [
'#prefix' => '<p>',
'#suffix' => '</p>',
'#markup' => $this
->t('No role supplied'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getValue('force_password_change')) {
$role = $form_state
->getValue('role');
if ($role
->id() == 'authenticated') {
$this->passwordChangeService
->forceUsersPasswordChange();
}
else {
$uids = $this->passwordChangeService
->getUsersForRole($role
->id());
if (count($uids)) {
$this->passwordChangeService
->forceUsersPasswordChange($uids);
}
}
$this->passwordChangeService
->updateLastChangeForRoles([
$role
->id(),
]);
if ($this->configFactory
->get('force_password_change.settings')
->get('check_login_only')) {
\Drupal::messenger()
->addMessage($this
->t('Users in this role will be required to change their password on next login'));
}
else {
\Drupal::messenger()
->addMessage($this
->t('Users in this role will be required to immediately change their password'));
}
}
}
}