Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix module install #69

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ dependencies:
- field.storage.user.field_last_password_reset
module:
- datetime
- password_policy
- user
enforced:
module:
- password_policy
id: user.user.field_last_password_reset
field_name: field_last_password_reset
entity_type: user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ dependencies:
config:
- field.storage.user.field_password_expiration
module:
- password_policy
- user
enforced:
module:
- password_policy
id: user.user.field_password_expiration
field_name: field_password_expiration
entity_type: user
Expand All @@ -20,4 +23,4 @@ default_value_callback: ''
settings:
on_label: 'Expired Password'
off_label: 'Non-expired Password'
field_type: boolean
field_type: boolean
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ dependencies:
module:
- datetime
- user
- password_policy
enforced:
module:
- password_policy
id: user.field_last_password_reset
field_name: field_last_password_reset
entity_type: user
type: datetime
settings:
datetime_type: datetime
module: datetime
locked: false
locked: true
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
persist_with_no_fields: false
custom_storage: false
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ status: true
dependencies:
module:
- user
- password_policy
enforced:
module:
- password_policy
id: user.field_password_expiration
field_name: field_password_expiration
entity_type: user
type: boolean
settings: { }
module: core
locked: false
locked: true
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
persist_with_no_fields: false
custom_storage: false
83 changes: 39 additions & 44 deletions password_policy.install
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
<?php

use Drupal\field\Entity\FieldStorageConfig;
/**
* @file
* Install, update and uninstall functions for the password_policy module.
*/

/**
* Implements hook_install().
*/
function password_policy_install() {
//set user password reset timestamp to now
$users = \Drupal::entityManager()->getStorage('user')->loadMultiple();
$date = date('Y-m-d\TH:i:s');
// Set user password reset timestamp to now.
$timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME);
/** @var \Drupal\user\UserInterface[] $users */
$users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple();
// @todo Get rid of updating all users.
foreach ($users as $user) {
$user->set('field_last_password_reset', $date);
$user->set('field_password_expiration', '0');
$user->save();
$user
->set('field_last_password_reset', $timestamp)
->set('field_password_expiration', '0')
->save();
}

//rebuild user entity form display for new fields.
$config = \Drupal::service('config.factory')->getEditable('core.entity_form_display.user.user.default');

// Not sure why this isn't set, but it throws an error in simpletest.
$config->set('targetEntityType', 'user');
$config->set('bundle', 'user');

$dep_module = $config->get('dependencies.module');
$dep_module[] = 'field.field.user.user.field_last_password_reset';
$dep_module[] = 'field.field.user.user.field_password_expiration';
$config->set('dependencies.module', $dep_module);

$dep_config = $config->get('dependencies.config');
$dep_config[] = 'field.field.user.user.field_last_password_reset';
$dep_config[] = 'field.field.user.user.field_password_expiration';
$config->set('dependencies.config', $dep_config);


$last_password_reset = [
'type' => 'datetime_default',
'weight' => '6',
'settings' => [],
'third_party_settings' => [],
];
$config->set('content.field_last_password_reset', $last_password_reset);

$field_password_expiration = [
'type' => 'boolean_checkbox',
'weight' => '5',
'settings' => ['display_label' => 'true'],
'third_party_settings' => [],
];
$config->set('content.field_password_expiration', $field_password_expiration);

$config->save();

}
// Rebuild user entity form display for new fields.
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
/** @var $user_form_display \Drupal\Core\Entity\Display\EntityFormDisplayInterface */
$user_form_display = $storage->load('user.user.default');
if (!$user_form_display) {
$user_form_display = $storage->create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
]);
}
$user_form_display
->setComponent('field_last_password_reset', [
'type' => 'datetime_default',
// Display before contact standard profile field.
'weight' => 4,
'settings' => [],
])
->setComponent('field_password_expiration', [
'type' => 'boolean_checkbox',
'weight' => 3,
'settings' => ['display_label' => TRUE],
])
->save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ function validate($password, $user_context) {
switch($configuration['character_operation']) {
case 'minimum':
if (strlen($password) < $configuration['character_length']) {
$validation->setErrorMessage($this->t('The length of the password is !count characters and needs to be at least @length characters', ['!count' => strlen($password), '@length' => $configuration['character_length']]));
$validation->setErrorMessage($this->t('The length of the password is @count characters and needs to be at least @length characters', ['@count' => strlen($password), '@length' => $configuration['character_length']]));
}
break;
case 'maximum':
if (strlen($password) > $configuration['character_length']) {
$validation->setErrorMessage($this->t('The length of the password is !count characters and can only be at most @length characters', ['!count' => strlen($password), '@length' => $configuration['character_length']]));
$validation->setErrorMessage($this->t('The length of the password is @count characters and can only be at most @length characters', ['@count' => strlen($password), '@length' => $configuration['character_length']]));
}
break;
}
Expand Down
9 changes: 3 additions & 6 deletions src/Wizard/PasswordPolicyWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ public function getMachineLabel() {
}

/**
* Retrieve a list of FormInterface classes by their step key in the wizard.
*
* @return array
* A
* {@inheritdoc}
*/
public function getOperations() {
public function getOperations($cached_values) {
return [
'general' => [
'title' => $this->t('General Info'),
Expand All @@ -79,4 +76,4 @@ public function getRouteName() {
return 'entity.password_policy.wizard.edit';
}

}
}