I’m implementing an ACL system for my models and I want to extract the common code into a common Helper class. I can’t find any examples of how to properly do something like this, but I constantly find the need to do it.
For example, let’s say that in my controller I have a chunk of code (taken right from the docs):
// creating the ACL
$aclProvider = $this->get('security.acl.provider');
$objectIdentity = ObjectIdentity::fromDomainObject($asset);
$acl = $aclProvider->createAcl($objectIdentity);
// retrieving the security identity of the currently logged-in user
$securityContext = $this->get('security.context');
$user = $securityContext->getToken()->getUser();
$securityIdentity = UserSecurityIdentity::fromAccount($user);
// grant owner access
$acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OWNER);
$aclProvider->updateAcl($acl);
I would much rather have the following:
$this->get('my_helpers')->bindUserToObject($asset, MaskBuilder::MASK_OWNER);
How should I go about implementing the ‘my_helpers’ service? I’m pretty sure it would be a service, but I still find the concept of ‘services’ to be a little confusing.
Ok, for some reason the process of defining my question here helped me solve how to do this. I did create a service.
Then I added it to my services.yml file:
Now in my controller, all I have to do is: