You are here

class ForcePasswordChangeController in Force Password Change 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/ForcePasswordChangeController.php \Drupal\force_password_change\Controller\ForcePasswordChangeController

Hierarchy

Expanded class hierarchy of ForcePasswordChangeController

File

src/Controller/ForcePasswordChangeController.php, line 19

Namespace

Drupal\force_password_change\Controller
View source
class ForcePasswordChangeController extends ControllerBase implements ForcePasswordChangeControllerInterface {

  /**
   * A form builder object
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * The date formatter service
   *
   * @var \Drupal\Core\Datetime\DateFormatter
   */
  protected $dateFormatter;

  /**
   * The current user
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * The configuration factory
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The config factory object
   *
   * @var \Drupal\user\UserData
   */
  protected $userData;

  /**
   * The database connection
   *
   * @var \Drupal\force_password_change\Service\ForcePasswordChangeService
   */
  protected $forcePasswordChangeService;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('form_builder'), $container
      ->get('date.formatter'), $container
      ->get('current_user'), $container
      ->get('config.factory'), $container
      ->get('user.data'), $container
      ->get('force_password_change.service'));
  }

  /**
   * Constructs a FriendController object.
   *
   * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
   *   The form builder service.
   * @param \Drupal\Core\Datetime\DateFormatter $dateFormatter
   *   The date formatter service
   * @param \Drupal\Core\Session\AccountProxy $currentUser
   *   The current user
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   The configuration factory
   * @param \Drupal\user\UserData $userData
   *   The user data service
   * @param \Drupal\force_password_change\Service\ForcePasswordChangeService $forcePasswordChangeService
   *   The force password change service
   */
  public function __construct(FormBuilderInterface $formBuilder, DateFormatter $dateFormatter, AccountProxy $currentUser, ConfigFactory $configFactory, UserData $userData, ForcePasswordChangeService $forcePasswordChangeService) {
    $this->formBuilder = $formBuilder;
    $this->dateFormatter = $dateFormatter;
    $this->currentUser = $currentUser;
    $this->configFactory = $configFactory;
    $this->userData = $userData;
    $this->forcePasswordChangeService = $forcePasswordChangeService;
  }
  public function adminPage() {
    $page = array(
      '#prefix' => '<div id="force_password_change_admin_page">',
      '#suffix' => '</div>',
      'form' => $this->formBuilder
        ->getForm('Drupal\\force_password_change\\Form\\AdminForm'),
    );
    return $page;
  }
  public function roleListPage($rid) {
    $page = array(
      '#prefix' => '<div id="force_password_change_role_page">',
      '#suffix' => '</div>',
    );
    $role = Role::load($rid);
    if ($role && $role
      ->id() != 'anonymous') {

      // Get a list of users that have a pending forced password change
      $pending_users = $this->forcePasswordChangeService
        ->getPendingUsersForRole($rid);

      // build the header for the table
      $header = [
        $this
          ->t('Username'),
        $this
          ->t('Last Force'),
        $this
          ->t('Last Change'),
      ];

      // Next build the rows of the table, and the stats that will be included for each user shown
      $rows = [];
      $force_password_change_installation_date = $this->configFactory
        ->get('force_password_change.settings')
        ->get('installation_date');
      $first_time_uids = $this->forcePasswordChangeService
        ->getFirstTimeLoginUids();
      foreach ($pending_users as $pending_user) {
        $row = [];
        if ($this->currentUser
          ->hasPermission('access user profiles')) {
          $url = Url::fromRoute('entity.user.canonical', [
            'user' => $pending_user
              ->id(),
          ]);
          $row[] = Link::fromTextAndUrl($pending_user
            ->getDisplayName(), $url);
        }
        else {
          $row[] = $pending_user
            ->getDisplayName();
        }
        $last_force_time = $this->userData
          ->get('force_password_change', $pending_user
          ->id(), 'last_force');
        if ($last_force_time) {
          $last_force = $this->dateFormatter
            ->format($last_force_time, 'short');
        }
        elseif ($this->configFactory
          ->get('force_password_change.settings')
          ->get('first_time_login_password_change') && $pending_user
          ->getCreatedTime() > $force_password_change_installation_date) {
          $last_force = $this
            ->t('First login');
        }
        else {
          if (is_array($first_time_uids) && in_array($pending_user
            ->id(), $first_time_uids)) {
            $last_force = $this
              ->t('First login');
          }
          else {
            $last_force = $this
              ->t('Never');
          }
        }
        $row[] = $last_force;
        $user_change_time = $this->userData
          ->get('force_password_change', $pending_user
          ->id(), 'last_change');
        $row[] = $user_change_time ? $this->dateFormatter
          ->format($user_change_time, 'short') : $this
          ->t('Never');
        $rows[] = $row;
      }

      // Build the table containing the retreived data
      $page['pending_users_table'] = array(
        'header' => array(
          '#prefix' => '<h2>',
          '#suffix' => '</h2>',
          '#markup' => $this
            ->t('Users in this role with pending password changes'),
        ),
        'table' => array(
          '#type' => 'table',
          '#header' => $header,
          '#rows' => $rows,
          '#empty' => $this
            ->t('No users found'),
          '#caption' => $this
            ->t('Only active users are shown'),
        ),
        'pager' => array(
          '#type' => 'pager',
          '#quantity' => 5,
        ),
      );

      // Perform the same steps as the previous table, for users who do not have a pending forced password change
      $nonpending_users = $this->forcePasswordChangeService
        ->getNonPendingUsersForRole($rid);
      $header = [
        $this
          ->t('Username'),
        $this
          ->t('Last Force'),
        $this
          ->t('Last Change'),
      ];
      $rows = [];
      foreach ($nonpending_users as $nonpending_user) {
        $row = [];
        if ($this->currentUser
          ->hasPermission('access user profiles')) {
          $url = Url::fromRoute('entity.user.canonical', [
            'user' => $nonpending_user
              ->id(),
          ]);
          $row[] = Link::fromTextAndUrl($nonpending_user
            ->getDisplayName(), $url);
        }
        else {
          $row[] = $nonpending_user
            ->getDisplayName();
        }
        $last_force_time = $this->userData
          ->get('force_password_change', $nonpending_user
          ->id(), 'last_force');
        if ($last_force_time) {
          $last_force = $this->dateFormatter
            ->format($last_force_time, 'short');
        }
        elseif ($this->configFactory
          ->get('force_password_change.settings')
          ->get('first_time_login_password_change') && $nonpending_user
          ->getCreatedTime() > $force_password_change_installation_date) {
          $last_force = $this
            ->t('First login');
        }
        else {
          if (is_array($first_time_uids) && in_array($nonpending_user
            ->id(), $first_time_uids)) {
            $last_force = $this
              ->t('First login');
          }
          else {
            $last_force = $this
              ->t('Never');
          }
        }
        $row[] = $last_force;
        $last_change_time = $this->userData
          ->get('force_password_change', $nonpending_user
          ->id(), 'last_change');
        $row[] = $last_change_time ? $this->dateFormatter
          ->format($last_change_time, 'short') : $this
          ->t('Never');
        $rows[] = $row;
      }

      // Build the table containing the retrieved data
      $page['nonpending_users_table'] = array(
        'header' => array(
          '#prefix' => '<h2>',
          '#suffix' => '</h2>',
          '#markup' => $this
            ->t('Users in this role without pending password changes'),
        ),
        'table' => array(
          '#type' => 'table',
          '#header' => $header,
          '#rows' => $rows,
          '#empty' => $this
            ->t('No users found'),
          '#caption' => $this
            ->t('Only active users are shown'),
        ),
        'pager' => array(
          '#type' => 'pager',
          '#quantity' => 5,
        ),
      );
      $page['form'] = $this
        ->formBuilder()
        ->getForm('Drupal\\force_password_change\\Form\\RoleForceForm', $role);
    }
    else {
      $page['invalid_role'] = array(
        '#prefix' => '<p>',
        '#suffix' => '</p>',
        '#markup' => $this
          ->t('Invalid role'),
      );
    }
    return $page;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
ForcePasswordChangeController::$configFactory protected property * The configuration factory * * Overrides ControllerBase::$configFactory
ForcePasswordChangeController::$currentUser protected property * The current user * * Overrides ControllerBase::$currentUser
ForcePasswordChangeController::$dateFormatter protected property * The date formatter service * *
ForcePasswordChangeController::$forcePasswordChangeService protected property * The database connection * *
ForcePasswordChangeController::$formBuilder protected property * A form builder object * * Overrides ControllerBase::$formBuilder
ForcePasswordChangeController::$userData protected property * The config factory object * *
ForcePasswordChangeController::adminPage public function Overrides ForcePasswordChangeControllerInterface::adminPage
ForcePasswordChangeController::create public static function * Overrides ControllerBase::create
ForcePasswordChangeController::roleListPage public function Overrides ForcePasswordChangeControllerInterface::roleListPage
ForcePasswordChangeController::__construct public function * Constructs a FriendController object. * *
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.