Commit 45c9b1c1 authored by Fabian de Rijk's avatar Fabian de Rijk
Browse files

Issue #3272675 by fabianderijk: Cannot create groups with "local" user

parent f6f5c0f5
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -31,15 +31,22 @@ function o365_groups_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type->id() === 'group') {
    $fields['field_o365_groups_teams_id'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Connected Office 365 group'))->setRevisionable(TRUE)
      ->setTranslatable(FALSE)->setDisplayOptions('form', [
      ->setLabel(t('Connected Office 365 group'))
      ->setRevisionable(TRUE)
      ->setTranslatable(FALSE)
      ->setDisplayOptions('form', [
        'type' => 'select',
        'weight' => 30,
      ])->setSettings([
      ])
      ->setSettings([
        'allowed_values_function' => 'o365_groups_teams_id_allowed',
      ])->setDisplayConfigurable('form', TRUE)->setRequired(FALSE)
      ->setTranslatable(TRUE)->setStorageRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE)->setComputed(FALSE);
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setRequired(FALSE)
      ->setTranslatable(TRUE)
      ->setStorageRequired(TRUE)
      ->setDisplayConfigurable('view', TRUE)
      ->setComputed(FALSE);
  }
  return $fields;
}
@@ -86,11 +93,18 @@ function o365_groups_form_group_form_alter(&$form, FormStateInterface $form_stat
 * @see options_allowed_values()
 */
function o365_groups_teams_id_allowed(BaseFieldDefinition $definition, ContentEntityInterface $entity = NULL, $cacheable) {
  /** @var \Drupal\o365\GraphService $graphService */
  $graphService = \Drupal::service('o365.graph');

  if ($graphService && $graphService->getCurrentUserId()) {
    /** @var \Drupal\o365_groups\GroupsService $groupsService */
    $groupsService = \Drupal::service('o365_groups.groups');
    return $groupsService->getGroupsFromTeams();
  }

  return [];
}

/**
 * Implements hook_o365_auth_scopes().
 */
+26 −22
Original line number Diff line number Diff line
@@ -96,13 +96,14 @@ class GroupsService {
  protected function getTeamsFromGraph($endpoint = FALSE) {
    $cid = 'getTeamsFromGraph::' . $this->currentUser->id();
    $cache = $this->cacheBackend->get($cid);
    $teams = [];

    if (!$cache) {
      try {
        if (!$endpoint) {
          $endpoint = $this->getTeamsEndpoint;
        }

      $teams = [];
        $data = $this->o365Graph->getGraphData($endpoint, 'GET', FALSE, FALSE, Group::class);

        if ($data) {
@@ -124,8 +125,11 @@ class GroupsService {

        $expireDate = strtotime('+4 hours');
        $this->cacheBackend->set($cid, $teams, $expireDate);
      }
      finally {
        $this->teams = $teams;
      }
    }
    else {
      $this->teams = $cache->data;
    }