Skip to content

Instantly share code, notes, and snippets.

@merk
merk / Menu.php
Created May 7, 2012 03:15
Event driven menus
<?php
/*
* TODO: Copyright notices on Infinite Networks files.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au/>
*/
namespace Infinite\MenuBundle\Menu;
@baldurrensch
baldurrensch / gist:3363685
Created August 15, 2012 21:05
Credit Card Model class
<?php
namespace Acme\DV2Bundle\Model;
use Acme\DV2Bundle\Exception\UnsupportedCreditCardTypeException;
use Acme\DV2Bundle\Exception\UnknownCreditCardTypeException;
/**
* Acme\DV2Bundle\Entity\CreditCard
*/
@merk
merk / JobController.php
Last active October 10, 2015 14:17
Use of a FormType to build a filter for a list of entities
<?php
namespace Ibms\JobBundle\Controller;
class JobController extends BaseJobController
{
/**
* Job list.
*
* @param \Symfony\Component\HttpFoundation\Request $request
@merk
merk / Enabled.php
Last active November 18, 2015 02:01
Example Specification pattern implementation
<?php
/**
* This file is part of the Infinite SSO project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
@tystr
tystr / getErrorsForForm.php
Created January 18, 2013 05:33
Gets symfony2 form errors as an array.
<?php
protected function getErrorsForForm(Form $form)
{
$errors = [];
foreach ($form->all() as $child) {
foreach ($child->getErrors() as $error) {
$errors[$child->getName()][] = $error->getMessage();
}
if ($child->count() > 0) {
$childErrors = $this->getErrorsForForm($child);
@pjedrzejewski
pjedrzejewski / ItemResolver.php
Last active December 12, 2015 06:29
Adding different item types to cart.
<?php
namespace App\Bundle\AppBundle\Resolver;
use Doctrine\Common\Persistence\ObjectRepository;
use Sylius\Bundle\CartBundle\Model\CartItemInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolverInterface;
use Sylius\Bundle\CartBundle\Resolver\ItemResolvingException;
use Symfony\Component\HttpFoundation\Request;
@kriswallsmith
kriswallsmith / test.php
Last active December 20, 2015 00:29
A test harness for your Symfony2 project. Runs functional tests in chunks.
#!/usr/bin/env php
<?php
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput as Input;
use Symfony\Component\Console\Input\InputOption as Option;
require_once __DIR__.'/../vendor/autoload.php';
/** Outputs and runs a command. */
@merk
merk / UserController.php
Last active December 22, 2015 08:19
User View Model
<?php
/**
* This file is part of the Project
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
@dbu
dbu / README.md
Last active January 13, 2016 13:53
Symfony2: Role Hierarchy check independent of firewall

We needed to decide whether a user loaded from FOSUserBundle is granted a specific role. Because of the role hierarchy, this is not as simple as doing in_array($role, $user->getRoles()). The user model only knows about its roles, not about what other roles those roles grant it.

The only thing that handles this situation that i found is the SecurityContext::isGranted method. But the problem of that is that its a check about the role of the "current" user. We needed this information in a command that generates a file and needs to know which user has permission for a specific role.

The RoleHierarchy service can not do decisions but only explode roles into all roles granted through the tree. The RoleHiararchyVoter is part of the security manager. Both are private service and thus not intended to be reused in application code.

The simplest we could come up with is this code, which we use like this:

$roleHierarchy = $this->getContainer()->get('acme_demo.security.role_hierarchy_checker');
@dragonmantank
dragonmantank / gist:6723460
Last active January 21, 2016 17:39
Reset Apache httpd to run as the vagrant user
exec { "change_httpd_user":
command => "sed -i 's/www-data/vagrant/g' /etc/apache2/envvars",
onlyif => "/bin/grep -q 'www-data' '/etc/apache2/envvars'",
notify => Service['apache2'],
require => Package['apache2'],
}
file { "/var/lock/apache2":
ensure => "directory",
owner => "vagrant",