Problem/Motivation

It would be nice to have an option to enable this plugin for all users.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

edvanleeuwen created an issue. See original summary.

mingsong’s picture

edvanleeuwen’s picture

Thanks, @mingsong. Although my question would be for current users as well, enforcing the plugin in stead of having users do this themselves. A one-of SQL-update statement could be enough for me, I'll look into that.

mingsong’s picture

As this is just a plugin module of the TFA module, I think the upstream module is the right place to manage all plugins, including enabling a plugin by default, or enabling for all.

edvanleeuwen’s picture

Just for the record. I have used the following:


/**
 * Voeg tfa_email_otp toe aan het plugin-segment voor alle actieve gebruikers.
 */
function mymodule_update_11003(&$sandbox) {
  $user_data = \Drupal::service('user.data');
  $time = \Drupal::time()->getRequestTime();

  if (!isset($sandbox['uids'])) {
    $sandbox['uids'] = array_values(
      \Drupal::entityQuery('user')
        ->accessCheck(FALSE)
        ->condition('status', 1)
        ->execute()
    );
    $sandbox['total'] = count($sandbox['uids']);
    $sandbox['current'] = 0;
  }

  $batch_size = 50;
  $slice = array_slice($sandbox['uids'], $sandbox['current'], $batch_size);

  foreach ($slice as $uid) {
    $settings = $user_data->get('tfa', $uid, 'tfa_user_settings');

    if (!is_array($settings)) {
      $settings = [];
    }

    if (!isset($settings['data']) || !is_array($settings['data'])) {
      $settings['data'] = [];
    }

    if (!isset($settings['data']['plugins']) || !is_array($settings['data']['plugins'])) {
      $settings['data']['plugins'] = [];
    }

    $settings['data']['plugins']['tfa_email_otp'] = 'tfa_email_otp';

    if (!isset($settings['status'])) {
      $settings['status'] = 1;
    }

    $settings['saved'] = $time;

    if (!isset($settings['validation_skipped'])) {
      $settings['validation_skipped'] = 0;
    }

    $user_data->set('tfa', $uid, 'tfa_user_settings', $settings);
    $sandbox['current']++;
  }

  $sandbox['#finished'] = $sandbox['total'] > 0
    ? ($sandbox['current'] / $sandbox['total'])
    : 1;

  return t('TFA Email OTP is toegevoegd aan data.plugins voor @current van @total actieve gebruikers.', [
    '@current' => $sandbox['current'],
    '@total' => $sandbox['total'],
  ]);
}