Skip to content
Permalink
Browse files

Implemented: User Demo: Add the CurrentUser view helper/controller pl…

…ugin #49
  • Loading branch information
olegkrivtsov committed Jul 12, 2018
1 parent 5f83e4b commit a62cc0f66c20c388c1018229c1e668a8c240f3a2

Some generated files are not rendered by default. Learn more.

@@ -55,11 +55,11 @@ public function aboutAction()
*/
public function settingsAction()
{
$user = $this->entityManager->getRepository(User::class)
->findOneByEmail($this->identity());
// Use the CurrentUser controller plugin to get the current user.
$user = $this->currentUser();

if ($user==null) {
throw new \Exception('Not found user with such email');
throw new \Exception('Not logged in');
}

return new ViewModel([
@@ -70,6 +70,15 @@
Controller\UserController::class => Controller\Factory\UserControllerFactory::class,
],
],
// We register module-provided controller plugins under this key.
'controller_plugins' => [
'factories' => [
Controller\Plugin\CurrentUserPlugin::class => Controller\Plugin\Factory\CurrentUserPluginFactory::class,
],
'aliases' => [
'currentUser' => Controller\Plugin\CurrentUserPlugin::class,
],
],
// The 'access_filter' key is used by the User module to restrict or permit
// access to certain controller actions for unauthorized visitors.
'access_filter' => [
@@ -96,6 +105,15 @@
__DIR__ . '/../view',
],
],
// We register module-provided view helpers under this key.
'view_helpers' => [
'factories' => [
View\Helper\CurrentUser::class => View\Helper\Factory\CurrentUserFactory::class,
],
'aliases' => [
'currentUser' => View\Helper\CurrentUser::class,
],
],
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
@@ -0,0 +1,72 @@
<?php
namespace User\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use User\Entity\User;

/**
* This controller plugin is designed to let you get the currently logged in User entity
* inside your controller.
*/
class CurrentUserPlugin extends AbstractPlugin
{
/**
* Entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager;

/**
* Authentication service.
* @var Zend\Authentication\AuthenticationService
*/
private $authService;

/**
* Logged in user.
* @var User\Entity\User
*/
private $user = null;

/**
* Constructor.
*/
public function __construct($entityManager, $authService)
{
$this->entityManager = $entityManager;
$this->authService = $authService;
}

/**
* This method is called when you invoke this plugin in your controller: $user = $this->currentUser();
* @param bool $useCachedUser If true, the User entity is fetched only on the first call (and cached on subsequent calls).
* @return User|null
*/
public function __invoke($useCachedUser = true)
{
// If current user is already fetched, return it.
if ($useCachedUser && $this->user!==null)
return $this->user;

// Check if user is logged in.
if ($this->authService->hasIdentity()) {

// Fetch User entity from database.
$this->user = $this->entityManager->getRepository(User::class)
->findOneByEmail($this->authService->getIdentity());
if ($this->user==null) {
// Oops.. the identity presents in session, but there is no such user in database.
// We throw an exception, because this is a possible security problem.
throw new \Exception('Not found user with such email');
}

// Return found User.
return $this->user;
}

return null;
}
}



@@ -0,0 +1,18 @@
<?php
namespace User\Controller\Plugin\Factory;

use Interop\Container\ContainerInterface;
use User\Controller\Plugin\CurrentUserPlugin;

class CurrentUserPluginFactory
{
public function __invoke(ContainerInterface $container)
{
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$authService = $container->get(\Zend\Authentication\AuthenticationService::class);

return new CurrentUserPlugin($entityManager, $authService);
}
}


@@ -0,0 +1,69 @@
<?php
namespace User\View\Helper;

use Zend\View\Helper\AbstractHelper;
use User\Entity\User;

/**
* This view helper is used for retrieving the User entity of currently logged in user.
*/
class CurrentUser extends AbstractHelper
{
/**
* Entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager;

/**
* Authentication service.
* @var Zend\Authentication\AuthenticationService
*/
private $authService;

/**
* Previously fetched User entity.
* @var User\Entity\User
*/
private $user = null;

/**
* Constructor.
*/
public function __construct($entityManager, $authService)
{
$this->entityManager = $entityManager;
$this->authService = $authService;
}

/**
* Returns the current User or null if not logged in.
* @param bool $useCachedUser If true, the User entity is fetched only on the first call (and cached on subsequent calls).
* @return User|null
*/
public function __invoke($useCachedUser = true)
{
// Check if User is already fetched previously.
if ($useCachedUser && $this->user!==null)
return $this->user;

// Check if user is logged in.
if ($this->authService->hasIdentity()) {

// Fetch User entity from database.
$this->user = $this->entityManager->getRepository(User::class)->findOneBy(array(
'email' => $this->authService->getIdentity()
));
if ($this->user==null) {
// Oops.. the identity presents in session, but there is no such user in database.
// We throw an exception, because this is a possible security problem.
throw new \Exception('Not found user with such ID');
}

// Return the User entity we found.
return $this->user;
}

return null;
}
}
@@ -0,0 +1,16 @@
<?php
namespace User\View\Helper\Factory;

use Interop\Container\ContainerInterface;
use User\View\Helper\CurrentUser;

class CurrentUserFactory
{
public function __invoke(ContainerInterface $container)
{
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$authService = $container->get(\Zend\Authentication\AuthenticationService::class);

return new CurrentUser($entityManager, $authService);
}
}

0 comments on commit a62cc0f

Please sign in to comment.
You can’t perform that action at this time.