Commit b9d6ada9 authored by Vladimir Korolev's avatar Vladimir Korolev Committed by Larisse Amorim
Browse files

Issue #2886166 by voviko, YurkinPark, larisse, andypost, marcaddeo,...

Issue #2886166 by voviko, YurkinPark, larisse, andypost, marcaddeo, KondratievaS, vacho: Configuration is not exported when doing config export
parent 0006d49c
Loading
Loading
Loading
Loading
+27 −0
Changes for config/schema/force_password_change.schema.yml: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -16,3 +16,30 @@ force_password_change.settings:
    check_login_only:
      type: boolean
      label: 'Check for force on login only'
    expiry_data:
      type: sequence
      label: 'Role expiry data'
      sequence:
        type: mapping
        mapping:
          rid:
            type: string
            label: 'role'
          expiry:
            type: integer
            label: 'expiry'
          weight:
            type: string
            label: 'weight'
    roles_change_password:
      type: sequence
      label: 'Role change password'
      sequence:
        type: mapping
        mapping:
          rid:
            type: string
            label: 'role'
          last_force:
            type: integer
            label: 'last force'
+9 −0
Changes for force_password_change.services.yml: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ parameters:
  force_password_change.mapper.class: Drupal\force_password_change\Mapper\ForcePasswordChangeMapper
  force_password_change.service.class: Drupal\force_password_change\Service\ForcePasswordChangeService
  force_password_change.event_subscriber.class: Drupal\force_password_change\EventSubscriber\ForcePasswordChangeEventSubscriber
  force_password_change.config_post_update.class: Drupal\force_password_change\EventSubscriber\ForcePasswordChangeConfigPostUpdateSubscriber

services:
  force_password_change.mapper:
@@ -32,3 +33,11 @@ services:
    class: Drupal\force_password_change\Service\ForcePasswordChangeRedirectMiddleware
    tags:
      - { name: http_middleware}

  force_password_change.config_post_update:
    class: '%force_password_change.config_post_update.class%'
    arguments:
      - '@config.factory'
      - '@force_password_change.service'
    tags:
      - { name: event_subscriber }
+91 −0
Changes for src/EventSubscriber/ForcePasswordChangeConfigPostUpdateSubscriber.php: 91 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\force_password_change\EventSubscriber;

use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * {@inheritdoc}
 */
class ForcePasswordChangeConfigPostUpdateSubscriber implements EventSubscriberInterface {
  /**
   * 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;

  /**
   * Creates an instance of the ForcePasswordChangeEventSubscriber class.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory service.
   * @param \Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface $passwordChangeService
   *   The force password change service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ForcePasswordChangeServiceInterface $passwordChangeService) {
    $this->configFactory = $configFactory;
    $this->passwordChangeService = $passwordChangeService;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = ['configSave'];
    return $events;
  }

  /**
   * React to a config object being saved.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   Config crud event.
   */
  public function configSave(ConfigCrudEvent $event) {
    $config = $event->getConfig();
    if ($config->getName() == 'force_password_change.settings') {
      // As an intermediate solution, we will use the
      // force_password_change_expiry table, although we need to get rid of it.
      $old_format_expiry_data = [];
      $expiry_data = $event->getConfig()->get('expiry_data');
      if (is_array($expiry_data)) {
        foreach ($expiry_data as $data) {
          $old_format_expiry_data[$data['rid']] = $data;
        }
        $getRoleExpiryTimePeriods = $this->passwordChangeService->getRoleExpiryTimePeriods();
        if (!empty($getRoleExpiryTimePeriods)) {
          foreach ($getRoleExpiryTimePeriods as $rid_data) {
            if (isset($old_format_expiry_data[$rid_data->rid])) {
              $this->passwordChangeService->updateExpiryForRole(
                $old_format_expiry_data[$rid_data->rid]['rid'],
                $old_format_expiry_data[$rid_data->rid]['expiry'],
                $old_format_expiry_data[$rid_data->rid]['weight']
              );
            }
            else {
              $this->passwordChangeService->insertExpiryForRoles(array_values($old_format_expiry_data));
            }
          }
        }
        else {
          $this->passwordChangeService->insertExpiryForRoles(array_values($old_format_expiry_data));
        }

      }
      \Drupal::messenger()->addStatus('Saved config: ' . $config->getName());
    }
  }

}
+34 −7
Changes for src/Form/AdminForm.php: 34 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\force_password_change\Form;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormBase;
@@ -30,6 +31,13 @@ class AdminForm extends FormBase {
   */
  protected $passwordChangeService;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs an AdminForm object.
   *
@@ -37,10 +45,13 @@ class AdminForm extends FormBase {
   *   The config factory service.
   * @param \Drupal\force_password_change\Service\ForcePasswordChangeServiceInterface $passwordChangeService
   *   The Force Password Change service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(ConfigFactoryInterface $configFactory, ForcePasswordChangeServiceInterface $passwordChangeService) {
  public function __construct(ConfigFactoryInterface $configFactory, ForcePasswordChangeServiceInterface $passwordChangeService, TimeInterface $time) {
    $this->configFactory = $configFactory;
    $this->passwordChangeService = $passwordChangeService;
    $this->time = $time;
  }

  /**
@@ -49,7 +60,8 @@ class AdminForm extends FormBase {
  public static function create(ContainerInterface $container) {
    return new static(
    $container->get('config.factory'),
    $container->get('force_password_change.service')
    $container->get('force_password_change.service'),
    $container->get('datetime.time')
    );
  }

@@ -365,6 +377,20 @@ class AdminForm extends FormBase {
      }
    }

    // Add selected_roles to config.
    // Force users in the following roles to change their password.
    $roles_change_password = [];

    if (!empty($selected_roles)) {
      $request_time = $this->time->getRequestTime();
      foreach ($selected_roles as $key => $rid) {
        $roles_change_password[$key] = [
          'rid' => $rid,
          'last_force' => $request_time,
        ];
      }
    }

    $this->configFactory->getEditable('force_password_change.settings')
      ->set('expire_password', (bool) $form_state->getValue('expire_password'))
      ->save();
@@ -403,12 +429,13 @@ class AdminForm extends FormBase {
      }
    }

    // Execute the query only if new roles were found.
    if (count($insert_roles)) {
      $this->passwordChangeService->insertExpiryForRoles($insert_roles);
    }
    $this->configFactory->getEditable('force_password_change.settings')
      ->set('check_login_only', (bool) $form_state->getValue('login_only'))
      ->set('expiry_data', $insert_roles)
      ->set('roles_change_password', $roles_change_password)
      ->save();

    \Drupal::messenger()->addMessage($this->t('The configuration options have been saved.'));
    $this->messenger()->addMessage($this->t('The configuration options have been saved.'));
  }

}