2012-02-24 105 views
3

我正在使用一些控制器的ACL,並且一直在徘徊是否有一種「乾淨」的方式來使用ACL,而無需反覆寫入相同的代碼。作爲服務實施的Symfony2 ACL?

我談論的ACL代碼

// creating the ACL 
$aclProvider = $this->get('security.acl.provider'); 
$objectIdentity = ObjectIdentity::fromDomainObject($comment); 
$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); 
the sf2 cookbook.

會是一個好主意是實現此代碼作爲一個服務,然後如果需要擴展它,並用它來代替寫在每個控制器相同的代碼?

回答

2

是的,你應該換一個服務中。它不僅可以減少複製&粘貼所需的代碼量,而且由於它將是一項服務,因此您可以將其注入到任何其他服務中,並仍然利用ACL系統。

+3

事實上,[我已經完成了它](https://github.com/Problematic/ProblematicAclManagerBundle) – Problematic 2012-02-24 15:57:04

+0

@Problematic感謝您的鏈接,現在查看它。 – 2012-02-24 16:00:58

+0

謝謝!這真太了不起了。好套餐。 – Tool 2012-02-24 16:48:57