Skip to content

Instantly share code, notes, and snippets.

@mrclay
Forked from tentacode/ConnectedUser.php
Created August 19, 2014 14:34
Show Gist options
  • Select an option

  • Save mrclay/6b2d941d7a1b9e26b812 to your computer and use it in GitHub Desktop.

Select an option

Save mrclay/6b2d941d7a1b9e26b812 to your computer and use it in GitHub Desktop.

Revisions

  1. @tentacode tentacode created this gist Aug 19, 2014.
    22 changes: 22 additions & 0 deletions ConnectedUser.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    namespace Tentacode\App\Security;

    trait ConnectedUser
    {
    /**
    * @Inject
    * @var Symfony\Component\Security\Core\SecurityContext
    */
    private $securityContext;

    public function getConnectedUser()
    {
    $token = $this->securityContext->getToken();
    if (!$token) {
    throw new \RuntimeException('No user is connected.');
    }

    return $token->getUser();
    }
    }
    32 changes: 32 additions & 0 deletions ProfileController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php

    namespace Tentacode\App\Controller\Person;

    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Tentacode\App\Security\ConnectedUser;
    use Tentacode\App\Templating\Templating;
    use Tentacode\Domain\Person\PersonRepository;
    use Tentacode\Domain\User;

    class ProfileController
    {
    use ConnectedUser;

    protected $templating;
    protected $personRepository;

    public function __construct(Templating $templating, PersonRepository $personRepository)
    {
    $this->templating = $templating;
    $this->personRepository = $personRepository;
    }

    public function myProfileAction()
    {
    $user = $this->getConnectedUser();

    return $this->templating->render('TentacodeAppBundle:Person:profile.html.twig', [
    'user' => $user
    ]);
    }
    }
    6 changes: 6 additions & 0 deletions phpdi_config.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?php

    return [
    'Symfony\Component\Security\Core\SecurityContext' => DI\link('security.context'),
    // ...
    ];