Skip to content

Instantly share code, notes, and snippets.

View jenkoian's full-sized avatar
👨‍💻
Works on my machine

Ian Jenkins jenkoian

👨‍💻
Works on my machine
View GitHub Profile
@jenkoian
jenkoian / LegacyUserProvider.php
Last active February 3, 2016 13:57
Legacy escape route LegacyUserProvider.php
<?php
namespace Acme\Framework\Security;
use Acme\Authentication\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
@jenkoian
jenkoian / LegacyAuthenticator.php
Last active February 3, 2016 13:56
Legacy escape route LegacyAuthenticator.php
<?php
namespace Acme\Framework\Security;
use Acme\Authentication\AuthenticationSessionInterface;
use Acme\Authentication\User;
use Acme\Authentication\UserManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@jenkoian
jenkoian / mapRolesSnippet.php
Last active February 3, 2016 12:57
Legacy escape route mapRolesSnippet.php
<?php
// ...
/**
* Map legacy roles to symfony roles. Legacy roles are in following format:
* ['moderator' => '1']
*/
private function mapRoles()
{
if (empty($this->legacyRoles)) {
@jenkoian
jenkoian / securityConfigSnippet.php
Created February 3, 2016 12:58
Legacy escape route securityConfigSnippet.php
<?php
// ...
$c->loadFromExtension(
'security',
[
'providers' => [
'legacy' => [
'id' => 'app.legacy_user_provider'
]
],
@jenkoian
jenkoian / access-control-configuration.php
Last active March 21, 2016 11:06
Symfony access control example
<?php
//...
$container->loadFromExtension(‘security’, [
//...
‘access_control’ => [
[‘path’ => ‘^/content/add’, ‘role’ => ‘ROLE_EDITOR’],
[‘path’ => ‘^/content/moderate’, ‘role’ => ‘ROLE_MODERATOR’],
[‘path’ => ‘^/user/edit’, ‘role’ => ‘ROLE_ADMIN’]
@jenkoian
jenkoian / roles-access-control-configuration.php
Created March 21, 2016 11:06
Symfony access control with roles
<?php
//...
$container->loadFromExtension(‘security’, [
//...
‘access_control’ => [
[‘path’ => ‘^/content/add’, ‘role’ => ‘add_content’],
[‘path’ => ‘^/content/moderate’, ‘role’ => ‘moderate_content’],
[‘path’ => ‘^/user/edit’, ‘role’ => ‘edit_user’]
@jenkoian
jenkoian / PermissionUrlMap.php
Created March 21, 2016 11:07
PermissionUrlMap
<?php
//...
class PermissionUrlMap
{
/**
* Order matters, in that it will match the first one it comes across. So put more specific URLs higher up.
*
* @var array Format is [‘permission’ => ‘/path/to/page’]
*/
public static $permissionMap = [
@jenkoian
jenkoian / PermissionUrlRequestMatcher.php
Created March 21, 2016 11:08
PermissionUrlRequestMatcher
<?php
//...
class PermissionUrlRequestMatcher implements RequestMatcherInterface
{
/**
* {@inheritdoc}
*/
public function matches(Request $request)
{
@jenkoian
jenkoian / custom-request-matcher-compiler-pass.php
Last active March 22, 2016 19:38
Custom request matcher compiler pass
<?php
//...
use ...CallableCompilerPass;
//...
protected function configurePasses(ContainerBuilder $c)
{
$c->addCompilerPass(new CallableCompilerPass(function (ContainerBuilder $c) {
if (!$c->hasDefinition(‘security.access_map’)) {
@jenkoian
jenkoian / PermissionUrlVoter.php
Created March 21, 2016 11:10
PermissionUrlVoter
<?php
//...
class PermissionUrlVoter implements VoterInterface
{
//...
/**
* {@inheritdoc}
*/
public function vote(TokenInterface $token, $object, array $attributes)