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 {
protected $bakeryService;
protected $kitchen;
public function __construct(BakeryService $bakeryService, Kitchen $kitchen) {
$this->bakeryService = $bakeryService;
$this->kitchen = $kitchen;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('bakery.bakery_service'), $container
->get('bakery.kitchen'));
}
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();
}
public function eatStroopwafelCookie(Request $request) {
$stroopwafel = Stroopwafel::fromData($request->attributes
->get('bakery'));
$request->attributes
->remove('bakery');
$response = new Response();
$account = $this
->entityTypeManager()
->getStorage('user')
->loadByProperties([
'init' => $this->kitchen
->generateInitField($stroopwafel
->getUid()),
]);
if (empty($account)) {
$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());
$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;
}
}