ChildDeprecatedController.php in Bakery Single Sign-On System 8.2
File
src/Controller/ChildDeprecatedController.php
View source
<?php
namespace Drupal\bakery\Controller;
use Drupal\bakery\Kitchen;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class ChildDeprecatedController extends ControllerBase {
protected $kitchen;
public function __construct(Kitchen $kitchen) {
$this->kitchen = $kitchen;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('bakery.kitchen'));
}
public function registerReturn() {
$cookie = $this->kitchen
->taste(Kitchen::OATMEAL);
if ($cookie) {
$this->kitchen
->eat(Kitchen::OATMEAL);
if (empty($cookie['data']['destination'])) {
$destination = '<front>';
}
else {
$destination = $cookie['data']['destination'];
}
$errors = $cookie['data']['errors'];
if (empty($errors)) {
$this
->messenger()
->addMessage($this
->t('Registration successful. You are now logged in.'));
$this
->redirect($destination);
}
else {
if (!empty($errors['register'])) {
$this
->messenger()
->addError($this
->t('Registration is not enabled on @master. Please contact a site administrator.', [
'@master' => $this
->config('bakery.settings')
->get('bakery_master'),
]));
$this
->getLogger('bakery')
->error('Master Bakery site user registration is disabled', []);
}
if (!empty($errors['validate'])) {
$language = $this
->languageManager()
->getCurrentLanguage()
->getId();
$account = User::create();
$account
->setPassword(user_password());
$account
->enforceIsNew();
$account
->setEmail($cookie['data']['mail']);
$account
->setUsername($cookie['name']);
$account
->set('init', $this->kitchen
->generateInitField($cookie['data']['uid']));
$account
->set('langcode', $language);
$account
->set('preferred_langcode', $language);
$account
->set('preferred_admin_langcode', $language);
$account
->activate();
$account
->save();
_user_mail_notify('register_no_approval_required', $account);
unset($_SESSION['bakery']['register']);
$this
->messenger()
->addMessage($this
->t('A welcome message with further instructions has been sent to your e-mail address.'));
}
if (!empty($errors['name'])) {
$this
->messenger()
->addError($this
->t('Name is already taken.'));
}
if (!empty($errors['mail'])) {
$this
->messenger()
->addError($this
->t('E-mail address is already registered.'));
}
if (!empty($errors['mail_denied'])) {
$this
->messenger()
->addError($this
->t('The e-mail address has been denied access..'));
}
if (!empty($errors['name_denied'])) {
$this
->messenger()
->addError($this
->t('The name has been denied access..'));
}
$this
->redirect('user.register');
}
}
throw new AccessDeniedHttpException();
}
public function loginReturn() {
$cookie = $this->kitchen
->taste(Kitchen::OATMEAL);
if ($cookie) {
$this->kitchen
->eat(Kitchen::OATMEAL);
if (!empty($cookie['data']['errors'])) {
$errors = $cookie['data']['errors'];
if (!empty($errors['incorrect-credentials'])) {
$this
->messenger()
->addError($this
->t('Sorry, unrecognized username or password.'));
}
elseif (!empty($errors['name'])) {
$this
->messenger()
->addError(Xss::filter($errors['name']));
}
}
if (empty($cookie['data']['destination'])) {
return $this
->redirect('user.page');
}
else {
return $this
->redirect($cookie['data']['destination']);
}
}
throw new AccessDeniedHttpException();
}
}