class RoleForceForm in Force Password Change 8
Same name and namespace in other branches
- 2.0.x src/Form/RoleForceForm.php \Drupal\force_password_change\Form\RoleForceForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\force_password_change\Form\RoleForceForm
Expanded class hierarchy of RoleForceForm
File
- src/
Form/ RoleForceForm.php, line 13
Namespace
Drupal\force_password_change\FormView source
class RoleForceForm extends FormBase {
/**
* The config factory object
*
* @var Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The force password change service
*
* @var Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface
*/
protected $passwordChangeService;
/**
* The date formatter service
*
* @var Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs an AdminForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
* @param Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface $passwordChangeService
* The force password change service
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter
* The date formatter service
*/
public function __construct(ConfigFactoryInterface $configFactory, DateFormatterInterface $dateFormatter, ForcePasswordChangeServiceInterface $passwordChangeService) {
$this->configFactory = $configFactory;
$this->dateFormatter = $dateFormatter;
$this->passwordChangeService = $passwordChangeService;
}
/**
* {@inheritdoc}
*/
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) {
// Only execute the code if the checkbox was selected
if ($form_state
->getValue('force_password_change')) {
$role = $form_state
->getValue('role');
// If the role is the authenticated users role, force the change for
// for all users
if ($role
->id() == 'authenticated') {
$this->passwordChangeService
->forceUsersPasswordChange();
}
else {
// Get all UIDS for all members of the role
$uids = $this->passwordChangeService
->getUsersForRole($role
->id());
// If any users are found, force their password change
if (count($uids)) {
$this->passwordChangeService
->forceUsersPasswordChange($uids);
}
}
// Log the force time for the role for statistics sake
$this->passwordChangeService
->updateLastChangeForRoles([
$role
->id(),
]);
// Set a message depending on the site settings
if ($this->configFactory
->get('force_password_change.settings')
->get('check_login_only')) {
drupal_set_message($this
->t('Users in this role will be required to change their password on next login'));
}
else {
drupal_set_message($this
->t('Users in this role will be required to immediately change their password'));
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RoleForceForm:: |
protected | property |
* The config factory object
*
* Overrides FormBase:: |
|
RoleForceForm:: |
protected | property | * The date formatter service * * | |
RoleForceForm:: |
protected | property | * The force password change service * * | |
RoleForceForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
RoleForceForm:: |
public static | function |
* Overrides FormBase:: |
|
RoleForceForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RoleForceForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
RoleForceForm:: |
public | function | * Constructs an AdminForm object. * * | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |