Skip to content
Snippets Groups Projects
Commit ad6e475e authored by Bojan Bogdanovic's avatar Bojan Bogdanovic
Browse files

Issue #3512143: Add extra checks in simple_oauth_update_8604

parent 418ede91
No related branches found
No related tags found
Loading
Pipeline #466511 passed with warnings
...@@ -280,7 +280,6 @@ function simple_oauth_update_8603() { ...@@ -280,7 +280,6 @@ function simple_oauth_update_8603() {
* Migrate roles used as scope and migrate settings to the consumer. * Migrate roles used as scope and migrate settings to the consumer.
*/ */
function simple_oauth_update_8604() { function simple_oauth_update_8604() {
$scopes = [];
$consumers = \Drupal::entityTypeManager()->getStorage('consumer')->loadMultiple(); $consumers = \Drupal::entityTypeManager()->getStorage('consumer')->loadMultiple();
$grant_types = array_keys(Oauth2GrantManager::getAvailablePluginsAsOptions()); $grant_types = array_keys(Oauth2GrantManager::getAvailablePluginsAsOptions());
$config = \Drupal::configFactory()->get('simple_oauth.settings'); $config = \Drupal::configFactory()->get('simple_oauth.settings');
...@@ -291,27 +290,31 @@ function simple_oauth_update_8604() { ...@@ -291,27 +290,31 @@ function simple_oauth_update_8604() {
->execute() ->execute()
->fetchAll(); ->fetchAll();
$scopes = [];
foreach ($role_values as $role_value) { foreach ($role_values as $role_value) {
/** @var \Drupal\user\RoleInterface $role */ /** @var \Drupal\user\RoleInterface $role */
$role = \Drupal::entityTypeManager()->getStorage('user_role')->load($role_value->roles_target_id); $role = \Drupal::entityTypeManager()->getStorage('user_role')->load($role_value->roles_target_id);
// Scope doesn't exist, so we need to create one. // Role doesn't exist, so don't do anything.
if (!isset($scopes[$role->id()])) { if (!$role) {
$scope = Oauth2Scope::create([ continue;
'name' => $role->id(), }
'description' => $role->label(), // Scope already exist.
'grant_types' => [ if (\Drupal::entityTypeManager()->getStorage('oauth2_scope')->load($role->id())) {
'authorization_code' => ['status' => TRUE], continue;
'client_credentials' => ['status' => TRUE],
'refresh_token' => ['status' => TRUE],
],
'granularity' => Oauth2ScopeInterface::GRANULARITY_ROLE,
'role' => $role->id(),
]);
$scope->save();
$scopes[$role->id()] = $scope;
} }
// Scope doesn't exist, so we need to create one.
Oauth2Scope::create([
'name' => $role->id(),
'description' => $role->label(),
'grant_types' => [
'authorization_code' => ['status' => TRUE],
'client_credentials' => ['status' => TRUE],
'refresh_token' => ['status' => TRUE],
],
'granularity' => Oauth2ScopeInterface::GRANULARITY_ROLE,
'role' => $role->id(),
])->save();
$insert_query = $database->insert('consumer__scopes') $insert_query = $database->insert('consumer__scopes')
->fields([ ->fields([
'bundle', 'bundle',
...@@ -336,6 +339,17 @@ function simple_oauth_update_8604() { ...@@ -336,6 +339,17 @@ function simple_oauth_update_8604() {
foreach ($consumers as $consumer) { foreach ($consumers as $consumer) {
foreach ($grant_types as $delta => $grant_type) { foreach ($grant_types as $delta => $grant_type) {
$grant_type_exist = $database->select('consumer__grant_types', 'gc')
->fields('gc', ['entity_id'])
->condition('gc.entity_id', $consumer->id())
->condition('gc.langcode', $consumer->language()->getId())
->condition('gc.grant_types_value', $grant_type)
->execute()
->fetchField();
// Grant type already exist.
if ($grant_type_exist) {
continue;
}
$insert_query = $database->insert('consumer__grant_types') $insert_query = $database->insert('consumer__grant_types')
->fields([ ->fields([
'bundle', 'bundle',
...@@ -371,7 +385,9 @@ function simple_oauth_update_8604() { ...@@ -371,7 +385,9 @@ function simple_oauth_update_8604() {
// Remove roles field. // Remove roles field.
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager(); $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$roles_field_definition = $entity_definition_update_manager->getFieldStorageDefinition('roles', 'consumer'); $roles_field_definition = $entity_definition_update_manager->getFieldStorageDefinition('roles', 'consumer');
$entity_definition_update_manager->uninstallFieldStorageDefinition($roles_field_definition); if ($roles_field_definition) {
$entity_definition_update_manager->uninstallFieldStorageDefinition($roles_field_definition);
}
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment