BakeryUserHooks.php in Bakery Single Sign-On System 8.2
File
src/BakeryUserHooks.php
View source
<?php
namespace Drupal\bakery;
use Drupal\bakery\Cookies\Stroopwafel;
use Drupal\bakery\Exception\MissingKeyException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Link;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
class BakeryUserHooks {
protected $config;
protected $currentUser;
protected $kitchen;
public function __construct(Kitchen $kitchen, ConfigFactoryInterface $config_factory, AccountProxy $current_user) {
$this->kitchen = $kitchen;
$this->config = $config_factory
->get('bakery.settings');
$this->currentUser = $current_user;
}
private function isMain() {
return (bool) $this->config
->get('bakery_is_master');
}
public function login(UserInterface $account) {
if ($this
->isMain() && $account
->id() != 0) {
$this->kitchen
->reBakeChocolateChipCookie($account);
}
}
public function logout(AccountInterface $account) {
try {
$cookie = $this->kitchen
->taste(Kitchen::CHOCOLATE_CHIP);
if ($account
->id() && $cookie && $cookie['name'] === $account
->getAccountName()) {
$this->kitchen
->eat(Kitchen::CHOCOLATE_CHIP);
}
} catch (MissingKeyException $exception) {
}
}
public function presave(UserInterface $account) {
if ($this
->isMain() && $account
->id() > 1 && isset($account->original)) {
foreach ($this->config
->get('bakery_supported_fields') as $type => $enabled) {
$original = $account->original;
if ($enabled) {
switch ($type) {
case 'name':
$o_v = $original
->getAccountName();
$n_v = $account
->getAccountName();
break;
case 'mail':
$o_v = $original
->getEmail();
$n_v = $account
->getEmail();
break;
case 'status':
$o_v = $original
->isActive();
$n_v = $account
->isActive();
break;
default:
$o_v = $original
->get($type)
->getValue();
$n_v = $account
->get($type)
->getValue();
break;
}
if ($o_v != $n_v) {
$_SESSION['bakery'][$type] = $n_v;
}
}
}
}
}
public function update(UserInterface $account) {
if ($this
->isMain() && isset($_SESSION['bakery'])) {
$this->kitchen
->ship(new Stroopwafel($account
->id(), $_SESSION['bakery']));
unset($_SESSION['bakery']);
if ($this->currentUser
->id() === $account
->id()) {
$this->kitchen
->reBakeChocolateChipCookie($account);
}
}
}
public function view(UserInterface $account, &$build) {
if (!$this
->isMain()) {
$main_site = $this->config
->get('bakery_master');
$init_url = _bakery_init_field_url($account
->getInitialEmail());
if (parse_url($main_site, PHP_URL_HOST) == parse_url($init_url, PHP_URL_HOST)) {
$build['summary']['primary_profile'] = [
'#type' => 'item',
'#title' => t('Primary profile'),
'link' => Link::fromTextAndUrl(t('Profile on primary site'), Url::fromUri(substr($init_url, 0, strlen($init_url) - 5)))
->toRenderable(),
'#access' => $this->currentUser
->hasPermission('access user profiles'),
];
}
}
}
}