Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update ACL on programmatically created object with Sonata Admin

I'm using the Sonata-Admin bundle with ACL, but I have to create some objects programmatically. But I can't figure out how I properly update the ACL tables for the created entity. So I always have to execute

php app/console sonata:admin:generate-object-acl

which is of course not a permanent solution.

I tried doing it like described here: http://symfony.com/doc/current/cookbook/security/acl.html#creating-an-acl-and-adding-an-ace so I implement the DomainObjectInterface in my entity and added the getObjectIdentifier method.

But now I'm getting an Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException exception when executing:

php app/console sonata:admin:generate-object-acl

So I guess that's not the proper way to do it when using sonata-admin. But I can't find anything in the docs.

like image 734
n3on Avatar asked May 27 '15 17:05

n3on


1 Answers

Ok, I took some time to debug a little more and I think I found a good solution:

Get the admin class of the object you want to create:

$whateverAdmin = $this->get('app.admin.whatever');

//create the object
$whatever = new Whatever();
$whatever->setName('test');

And now use the admin class to create the object:

$whateverAdmin->create($whatever);

Or if you want to use the entityManager to persist you could just update the ACL with the admin class:

$em->persist($whatever);
$em->flush(); // important to flush first so an ID ist generated

$whateverAdmin->createObjectSecurity($whatever);
like image 101
n3on Avatar answered Oct 20 '22 03:10

n3on