You are here

public function ChildController::eatStroopwafelCookie in Bakery Single Sign-On System 8.2

Menu callback, invoked on the child.

File

src/Controller/ChildController.php, line 74

Class

ChildController

Namespace

Drupal\bakery\Controller

Code

public function eatStroopwafelCookie(Request $request) {

  // The session got set during validation.
  $stroopwafel = Stroopwafel::fromData($request->attributes
    ->get('bakery'));
  $request->attributes
    ->remove('bakery');
  $response = new Response();

  // Check if the user exists.

  /** @var \Drupal\user\UserInterface[] $account */
  $account = $this
    ->entityTypeManager()
    ->getStorage('user')
    ->loadByProperties([
    'init' => $this->kitchen
      ->generateInitField($stroopwafel
      ->getUid()),
  ]);
  if (empty($account)) {

    // User not present.
    $response
      ->setContent(t('Account not found on @child.', [
      '@child' => $this
        ->config('system.site')
        ->get('name'),
    ]));
  }
  else {
    $account = reset($account);
    $response->headers
      ->set('X-Drupal-bakery-UID', $account
      ->id());

    // If profile field is enabled we manually save profile fields along.
    $field_data = $stroopwafel
      ->getData();
    $this->bakeryService
      ->updateUserFields($account, $field_data);
    if ($account
      ->save() != SAVED_UPDATED) {
      $this
        ->getLogger('bakery')
        ->error('User update from name %name_old to %name_new, mail %mail_old to %mail_new failed.', [
        '%name_old' => $account
          ->getAccountName(),
        '%name_new' => $field_data['name'],
        '%mail_old' => $account
          ->getEmail(),
        '%mail_new' => $field_data['mail'],
      ]);
      $response
        ->setContent(t('There was a problem updating your account on @child. Please contact the administrator.', [
        '@child' => $this
          ->config('system.site')
          ->get('name'),
      ]));
      $response
        ->setStatusCode(Response::HTTP_CONFLICT);
    }
    else {
      $this
        ->getLogger('bakery')
        ->notice('user updated name %name_old to %name_new, mail %mail_old to %mail_new.', [
        '%name_old' => $account
          ->getAccountName(),
        '%name_new' => $field_data['name'],
        '%mail_old' => $account
          ->getEmail(),
        '%mail_new' => $field_data['mail'],
      ]);
      $response
        ->setContent(t('Successfully updated account on @child.', [
        '@child' => $this
          ->config('system.site')
          ->get('name'),
      ]));
    }
  }
  return $response;
}