Commit c60d3845 authored by Fabian de Rijk's avatar Fabian de Rijk
Browse files

Issue #3274005: Include User data requiring select query

parent 1033b280
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
services:
  o365_profile.get_data:
    class: Drupal\o365_profile\O365ProfileGetDataService
    arguments: ['@o365.graph']
    arguments: ['@o365.graph', '@config.factory']
  o365_profile.teams:
    class: Drupal\o365_profile\O365ProfileTeamsService
+29 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\o365_profile;

use Drupal\Core\Config\ConfigFactory;
use Drupal\o365\GraphService;
use GuzzleHttp\Exception\ClientException;

@@ -17,14 +18,24 @@ class O365ProfileGetDataService {
   */
  protected $o365Graph;

  /**
   * The config of the SSO user module.
   *
   * @var \Drupal\Core\Config\Config|\Drupal\Core\Config\ImmutableConfig
   */
  protected $ssoUserConfig;

  /**
   * Constructs an O365ProfileGetDataService object.
   *
   * @param \Drupal\o365\GraphService $o365_graph
   *   The o365.graph service.
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   The config factory.
   */
  public function __construct(GraphService $o365_graph) {
  public function __construct(GraphService $o365_graph, ConfigFactory $configFactory) {
    $this->o365Graph = $o365_graph;
    $this->ssoUserConfig = $configFactory->get('o365_sso_user.settings');
  }

  /**
@@ -43,9 +54,24 @@ class O365ProfileGetDataService {
   * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
   */
  public function getProfileData($user_id = NULL, $photoSize = '64x64') {
    // Get the current user ID.
    $profileData['currentUserId'] = $user_id ?? $this->o365Graph->getCurrentUserId();
    $profileData['presenceData'] = $this->o365Graph->getGraphData('/users/' . $profileData['currentUserId'] . '/presence');
    $profileData['userData'] = $this->o365Graph->getGraphData('/users/' . $profileData['currentUserId']);
    $endPoint = '/users/' . $profileData['currentUserId'];

    // Get the presence data.
    $profileData['presenceData'] = $this->o365Graph->getGraphData($endPoint . '/presence');

    // Get other user data if entered.
    if (!empty($this->ssoUserConfig->get('graph_data_selected_fields'))) {
      $endPoint .= '?$select=' . str_replace(' ', '', $this->ssoUserConfig->get('graph_data_selected_fields'));
      // Add some required fields, they can be duplicate from the selected
      // fields, but they are only returned once.
      $endPoint .= ',displayName,id,givenName,surname';
    }

    dpm($endPoint);
    $profileData['userData'] = $this->o365Graph->getGraphData($endPoint);
    dpm($profileData['userData']);
    $profileData['initials'] = ($profileData['userData']) ? $this->getUserInitials($profileData['userData']) : 'A';

    try {
+16 −0
Original line number Diff line number Diff line
@@ -83,6 +83,21 @@ class SettingsForm extends ConfigFormBase {
      '#default_value' => $config->get('use_graph_data'),
    ];

    $linkText = $this->t('Microsoft documentation');
    $linkUrl = Url::fromUri('https://docs.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http#example-3-use-select-to-retrieve-specific-properties-of-a-user', ['absolute' => TRUE]);
    $link = Link::fromTextAndUrl($linkText, $linkUrl);
    $form['graph_data_selected_fields'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Selected fields from the Graph API'),
      '#description' => $this->t('Input a comma seperated list of fields you would like to get from the GraphApi. You can read the @link for more information about which fields you can select. When no fields are entered a basic set of fields is used: <i>businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName</i>. Some required fields are automatically added.', ['@link' => $link->toString()]),
      '#default_value' => $config->get('graph_data_selected_fields'),
      '#states' => [
        'visible' => [
          ':input[name="use_graph_data"]' => ['checked' => TRUE],
        ],
      ],
    ];

    $form['graph_data_mapping'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Field mapping Graph API > Drupal'),
@@ -141,6 +156,7 @@ class SettingsForm extends ConfigFormBase {
    $config = $this->config('o365_sso_user.settings');
    $config->set('use_graph_data', $form_state->getValue('use_graph_data'))
      ->set('graph_data_mapping', $form_state->getValue('graph_data_mapping'))
      ->set('graph_data_selected_fields', $form_state->getValue('graph_data_selected_fields'))
      ->set('use_profile_module', $form_state->getValue('use_profile_module'))
      ->set('sync_profile_image', $form_state->getValue('sync_profile_image'))
      ->set('profile_image_mapping', $form_state->getValue('profile_image_mapping'));