Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
82 / 82 |
|
100.00% |
1 / 1 |
CRAP | n/a |
0 / 0 |
|
| authorization_drupal_roles_update_8004 | |
100.00% |
82 / 82 |
|
100.00% |
1 / 1 |
21 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Contains authorization_drupal_roles.module. |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | /** |
| 11 | * Migrate roles from field 'authorization_drupal_roles_roles' to user data. |
| 12 | */ |
| 13 | function authorization_drupal_roles_update_8004(&$sandbox) { |
| 14 | |
| 15 | if (!isset($sandbox['total'])) { |
| 16 | $update_manager = \Drupal::entityDefinitionUpdateManager(); |
| 17 | $definition = $update_manager->getFieldStorageDefinition('authorization_drupal_roles_roles', 'user'); |
| 18 | if (is_null($definition)) { |
| 19 | $sandbox['#finished'] = 1; |
| 20 | \Drupal::logger('authorization_drupal_roles') |
| 21 | ->error('"authorization_drupal_roles_roles" field is not present. Unable to move roles to user data.'); |
| 22 | return; |
| 23 | } |
| 24 | $profiles = \Drupal::entityTypeManager() |
| 25 | ->getStorage('authorization_profile') |
| 26 | ->loadMultiple(); |
| 27 | |
| 28 | $sandbox['profiles'] = []; |
| 29 | foreach ($profiles as $profile) { |
| 30 | $sync_actions = $profile->get('synchronization_actions'); |
| 31 | if (!$sync_actions['revoke_provider_provisioned']) { |
| 32 | continue; |
| 33 | } |
| 34 | $mappings = $profile->getConsumerMappings(); |
| 35 | $roles = []; |
| 36 | foreach ($mappings as $mapping) { |
| 37 | $role = $mapping['role']; |
| 38 | if ($role == 'none') { |
| 39 | continue; |
| 40 | } |
| 41 | $roles[] = $role; |
| 42 | } |
| 43 | if (empty($roles)) { |
| 44 | continue; |
| 45 | } |
| 46 | $sandbox['profiles'][$profile->id()] = $roles; |
| 47 | |
| 48 | } |
| 49 | if (empty($sandbox['profiles'])) { |
| 50 | $sandbox['#finished'] = 1; |
| 51 | \Drupal::logger('authorization_drupal_roles') |
| 52 | ->notice('No profiles have revoke_provider_provisioned enabled. No roles to move to user data.'); |
| 53 | $update_manager = \Drupal::entityDefinitionUpdateManager(); |
| 54 | $definition = $update_manager->getFieldStorageDefinition('authorization_drupal_roles_roles', 'user'); |
| 55 | $update_manager->uninstallFieldStorageDefinition($definition); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | $sandbox['current'] = 0; |
| 60 | $sandbox['user_ids'] = \Drupal::database()->select('user__authorization_drupal_roles_roles', 'ur') |
| 61 | ->fields('ur', ['entity_id']) |
| 62 | ->distinct() |
| 63 | ->execute() |
| 64 | ->fetchCol(); |
| 65 | |
| 66 | $sandbox['total'] = count($sandbox['user_ids']); |
| 67 | |
| 68 | if ($sandbox['total'] == 0) { |
| 69 | $sandbox['#finished'] = 1; |
| 70 | \Drupal::logger('authorization_drupal_roles') |
| 71 | ->notice('No users have authorization_drupal_roles_roles values. No roles to move to user data.'); |
| 72 | $update_manager = \Drupal::entityDefinitionUpdateManager(); |
| 73 | $definition = $update_manager->getFieldStorageDefinition('authorization_drupal_roles_roles', 'user'); |
| 74 | $update_manager->uninstallFieldStorageDefinition($definition); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $sandbox['#finished'] = 0; |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | $user_storage = \Drupal::entityTypeManager()->getStorage('user'); |
| 83 | $start = \Drupal::time()->getRequestTime(); |
| 84 | $now = \Drupal::time()->getRequestTime(); |
| 85 | $user_service = \Drupal::service('user.data'); |
| 86 | $database = \Drupal::service('database'); |
| 87 | |
| 88 | while (30 > ($now - $start) && !empty($sandbox['user_ids'])) { |
| 89 | $user_id = array_pop($sandbox['user_ids']); |
| 90 | $sandbox['current'] += 1; |
| 91 | $user = $user_storage->load($user_id); |
| 92 | $drupal_roles = $database->select('user__authorization_drupal_roles_roles', 'ur') |
| 93 | ->fields('ur', ['authorization_drupal_roles_roles_value']) |
| 94 | ->condition('entity_id', $user_id) |
| 95 | ->condition('bundle', 'user') |
| 96 | ->execute() |
| 97 | ->fetchCol(); |
| 98 | $roles = []; |
| 99 | foreach ($drupal_roles as $role) { |
| 100 | if (!$user->hasRole($role)) { |
| 101 | continue; |
| 102 | } |
| 103 | $roles[$role] = $role; |
| 104 | } |
| 105 | $user_roles = []; |
| 106 | foreach ($sandbox['profiles'] as $profile_id => $profile_roles) { |
| 107 | foreach ($profile_roles as $role) { |
| 108 | if (!isset($roles[$role])) { |
| 109 | continue; |
| 110 | } |
| 111 | $user_roles[$role] = $profile_id; |
| 112 | unset($roles[$role]); |
| 113 | } |
| 114 | if (empty($user_roles)) { |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (!empty($user_roles)) { |
| 120 | $user_service->set('authorization_drupal_roles', $user_id, 'roles', $user_roles); |
| 121 | } |
| 122 | |
| 123 | $now = \Drupal::time()->getCurrentTime(); |
| 124 | } |
| 125 | |
| 126 | if (empty($sandbox['user_ids'])) { |
| 127 | $update_manager = \Drupal::entityDefinitionUpdateManager(); |
| 128 | $definition = $update_manager->getFieldStorageDefinition('authorization_drupal_roles_roles', 'user'); |
| 129 | $update_manager->uninstallFieldStorageDefinition($definition); |
| 130 | } |
| 131 | |
| 132 | $sandbox['#finished'] = empty($sandbox['user_ids']) ? 1 : $sandbox['current'] / $sandbox['total']; |
| 133 | } |