Skip to content

Instantly share code, notes, and snippets.

@alexwilson
Created September 23, 2016 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexwilson/f8bcb5f249b7caf7b26ff04fbfe20644 to your computer and use it in GitHub Desktop.
Save alexwilson/f8bcb5f249b7caf7b26ff04fbfe20644 to your computer and use it in GitHub Desktop.
how to curl up into a fetal position and cry
<?php // Why does syntax highlighting need this?
use eZ\Publish\Core\MVC\Symfony\Security\User as EzUser;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
$securityContext = $container->get('security.context');
$router = $container->get('router');
$requestStack = $container->get('request_stack');
if ($requestStack->getCurrentRequest() === null) {
// Log in as admin to subvert permissions from outside standard context.
$user = new EzUser($repository->getUserService()->loadUserByLogin('admin'), ['ROLE_USER']);
$token = new UsernamePasswordToken(
$user,
null,
'main',
$user->getRoles()
);
$securityContext->setToken($token);
// Generate full URL, optionally with site-access, and use it to build a new request.
$url = $router->generate('ez_urlalias', ['locationId' => 2, 'siteaccess' => '<OPTIONAL SITEACCESS HERE>'], true);
$request = Request::create($url);
// Start a new Session and assign it to our request.
$session = new Session();
$request->setSession($session);
$requestStack->push($request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment