class ForcePasswordChangeController in Force Password Change 8
Same name and namespace in other branches
- 2.0.x src/Controller/ForcePasswordChangeController.php \Drupal\force_password_change\Controller\ForcePasswordChangeController
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\force_password_change\Controller\ForcePasswordChangeController implements ForcePasswordChangeControllerInterface
Expanded class hierarchy of ForcePasswordChangeController
File
- src/
Controller/ ForcePasswordChangeController.php, line 19
Namespace
Drupal\force_password_change\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
ForcePasswordChangeController:: |
protected | property |
* The configuration factory
*
* Overrides ControllerBase:: |
|
ForcePasswordChangeController:: |
protected | property |
* The current user
*
* Overrides ControllerBase:: |
|
ForcePasswordChangeController:: |
protected | property | * The date formatter service * * | |
ForcePasswordChangeController:: |
protected | property | * The database connection * * | |
ForcePasswordChangeController:: |
protected | property |
* A form builder object
*
* Overrides ControllerBase:: |
|
ForcePasswordChangeController:: |
protected | property | * The config factory object * * | |
ForcePasswordChangeController:: |
public | function |
Overrides ForcePasswordChangeControllerInterface:: |
|
ForcePasswordChangeController:: |
public static | function |
* Overrides ControllerBase:: |
|
ForcePasswordChangeController:: |
public | function |
Overrides ForcePasswordChangeControllerInterface:: |
|
ForcePasswordChangeController:: |
public | function | * Constructs a FriendController object. * * | |
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. | |
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. |