You are here

class RoleForceForm in Force Password Change 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/RoleForceForm.php \Drupal\force_password_change\Form\RoleForceForm

Hierarchy

Expanded class hierarchy of RoleForceForm

File

src/Form/RoleForceForm.php, line 16

Namespace

Drupal\force_password_change\Form
View 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\Core\Datetime\DateFormatterInterface $dateFormatter
   *   The date formatter service.
   * @param Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface $passwordChangeService
   *   The force password change 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::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'));
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RoleForceForm::$configFactory protected property The config factory object. Overrides FormBase::$configFactory
RoleForceForm::$dateFormatter protected property The date formatter service.
RoleForceForm::$passwordChangeService protected property The force password change service.
RoleForceForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
RoleForceForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
RoleForceForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RoleForceForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
RoleForceForm::__construct public function Constructs an AdminForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.