You are here

ChildController.php in Bakery Single Sign-On System 8.2

File

src/Controller/ChildController.php
View source
<?php

namespace Drupal\bakery\Controller;

use Drupal\bakery\BakeryService;
use Drupal\bakery\Cookies\Stroopwafel;
use Drupal\bakery\Kitchen;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ChildController extends ControllerBase {

  /**
   * @var \Drupal\bakery\BakeryService
   */
  protected $bakeryService;

  /**
   * @var \Drupal\bakery\Kitchen
   */
  protected $kitchen;

  /**
   * Initialize bakery service.
   *
   * @param \Drupal\bakery\BakeryService $bakeryService
   *   For bakery service.
   * @param \Drupal\bakery\Kitchen $kitchen
   *   Kitchen service to work on all those cookies.
   */
  public function __construct(BakeryService $bakeryService, Kitchen $kitchen) {
    $this->bakeryService = $bakeryService;
    $this->kitchen = $kitchen;
  }

  /**
   * When this controller is created, it will get the bakery.bakery_service.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   For getting Bakery service.
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('bakery.bakery_service'), $container
      ->get('bakery.kitchen'));
  }

  /**
   * Validate update request.
   */
  public function tasteStroopwafelCookie(Request $request) {
    $type = 'stroopwafel';
    $post = $request->request;
    if (!$post
      ->has($type)) {
      return AccessResult::forbidden();
    }
    $cookie = $this->kitchen
      ->tasteData($post
      ->get($type), $type);
    if ($cookie === FALSE) {
      return AccessResult::forbidden();
    }
    $request->attributes
      ->set('bakery', $cookie);
    return AccessResult::allowed();
  }

  /**
   * Menu callback, invoked on the child.
   */
  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;
  }

}

Classes

Namesort descending Description
ChildController