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 / is-granted-permission.twig
Created March 21, 2016 11:13
Permission is_granted
{% if is_granted(‘add_content’) %}
<button>Add content</button>
{% endif %}
@jenkoian
jenkoian / is-granted-example.twig
Created March 21, 2016 11:12
Twig is_granted example
{% if is_granted(‘ROLE_ADMIN’) %}
<button>Secret Button</button>
{% endif %}
@jenkoian
jenkoian / routes-catch-all.yml
Created March 21, 2016 11:11
Catch all routes
catch_all:
path: /{route}
requirements:
route: .*
@jenkoian
jenkoian / configuration-role-map.yml
Created March 21, 2016 11:11
Configuration role map
editor:
- add_content
moderator:
- moderate_content
admin:
- edit_user
@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)
@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 / PermissionUrlRequestMatcher.php
Created March 21, 2016 11:08
PermissionUrlRequestMatcher
<?php
//...
class PermissionUrlRequestMatcher implements RequestMatcherInterface
{
/**
* {@inheritdoc}
*/
public function matches(Request $request)
{
@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 / 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 / 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’]