Skip to content

Commit

Permalink
Merge pull request #6 from wieni/feature/account-proxy-getaccount
Browse files Browse the repository at this point in the history
AccountProxy->getAccount() now returns a fully loaded user model
  • Loading branch information
RobinHoutevelts authored Aug 10, 2017
2 parents 3d418d8 + cdf2a38 commit 73a821a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/Session/AccountProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace Drupal\wmmodel\Session;

use Drupal\Core\Session\AccountProxy as DrupalAccountProxy;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Session\AccountInterface;

class AccountProxy extends DrupalAccountProxy
{

/** @var Drupal\user\Entity\User */
protected $fullUser;

public function setAccount(AccountInterface $account)
{
if ($account instanceof DrupalAccountProxy) {
$account = $this->getCachedAccount();
}

$this->account = $account;
$this->id = $account->id();

// Ugh wtf bruh
date_default_timezone_set(drupal_get_user_timezone());
}

public function getAccount()
{
if (!isset($this->fullUser)) {
$this->fullUser = new AnonymousUserSession();
if ($this->id) {
$user = $this->loadUserEntity($this->id);
$this->fullUser = $user;
$this->setAccount($user);
}

$this->account = $this->fullUser;
}

return $this->account;
}

protected function getCachedAccount()
{
return parent::getAccount();
}

public function id()
{
return $this->id;
}

public function getRoles($exclude_locked_roles = false)
{
return $this->getCachedAccount()->getRoles($exclude_locked_roles);
}

public function hasPermission($permission)
{
return $this->getCachedAccount()->hasPermission($permission);
}

public function isAuthenticated()
{
return $this->getCachedAccount()->isAuthenticated();
}

public function isAnonymous()
{
return $this->getCachedAccount()->isAnonymous();
}

public function getPreferredLangcode($fallback_to_default = true)
{
return $this->getCachedAccount()->getPreferredLangcode($fallback_to_default);
}

public function getPreferredAdminLangcode($fallback_to_default = true)
{
return $this->getCachedAccount()->getPreferredAdminLangcode($fallback_to_default);
}

public function getAccountName()
{
return $this->getCachedAccount()->getAccountName();
}

public function getDisplayName()
{
return $this->getCachedAccount()->getDisplayName();
}

public function getEmail()
{
return $this->getCachedAccount()->getEmail();
}

public function getTimeZone()
{
return $this->getCachedAccount()->getTimeZone();
}

public function getLastAccessedTime()
{
return $this->getCachedAccount()->getLastAccessedTime();
}
}
19 changes: 19 additions & 0 deletions src/WmmodelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Drupal\wmmodel;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Symfony\Component\DependencyInjection\Reference;

class WmmodelServiceProvider implements ServiceModifierInterface
{
public function alter(ContainerBuilder $container)
{
try {
$container->getDefinition('current_user')
->setClass('Drupal\\wmmodel\\Session\\AccountProxy');
} catch (ServiceNotFoundException $e) {
}
}
}

0 comments on commit 73a821a

Please sign in to comment.