हाँ एसीएल जाने का रास्ता है। VoterInterface को कार्यान्वित करने वाला एक कंपनीवॉटर बनाएं और जांचें कि क्या उपयोगकर्ता उसी वोट पर है() विधि।
कुकबुक प्रविष्टि "How to implement your own Voter to blacklist IP Addresses" एक अच्छा परिचय देता है।
अपने सर्व-निर्णय-प्रबंधक की रणनीति को 'सर्वसम्मति' में बदलें। इसका मतलब है कि अगर केवल एक मतदाता पहुंच से इनकार करता है (उदा। कंपनीवॉटर), अंतिम उपयोगकर्ता को पहुंच प्रदान नहीं की जाती है।
# app/config/security.yml
security:
access_decision_manager:
strategy: unanimous
अब बनाने के अपने मतदाता
// src/Acme/AcmeBundle/YourBundle/Security/Authorization/Voter/CompanyVoter.php
namespace Acme\YourBundle\Security\Authorization\Voter;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Acme\YourUserBundleBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
class CompanyVoter implements VoterInterface
{
private $container;
public function __construct($container)
{
$this->container = $container;
}
public function supportsAttribute($attribute)
{
return in_array($attribute, array(
'EDIT',
'ACTIVATE',
// ...
));
}
public function supportsClass($class)
{
return in_array("FOS\UserBundle\Model\UserInterface", class_implements($class));
}
public function vote(TokenInterface $token, $object, array $attributes)
{
if (!($this->supportsClass(get_class($object)))) {
return VoterInterface::ACCESS_ABSTAIN;
}
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
return VoterInterface::ACCESS_ABSTAIN;
}
}
$user = $token->getUser();
if (!($user instanceof UserInterface)) {
return VoterInterface::ACCESS_DENIED;
}
// check if the user has the same company
if ($user->getCompany() == $object->getCompany()) {
return VoterInterface::ACCESS_GRANTED;
}
return VoterInterface::ACCESS_DENIED;
}
}
अंत के रूप में एक सेवा
# src/Acme/AcmeBundle/Resources/config/services.yml
services:
security.access.company_voter:
class: Acme\YourBundle\Security\Authorization\Voter\CompanyVoter
public: false
tags:
- { name: security.voter }
के रूप में मतदाता रजिस्टर ... अब अपनी टहनी टेम्पलेट में इसका इस्तेमाल
{% if is_granted('EDIT', user) %}<a href="#">Edit</a>{% endif %}
{% if is_granted('ACTIVATE', user) %}<a href="#">activate</a>{% endif %}
या आपके नियंत्रक में ...
public function editAction(UserInterface $user)
{
if ($this->get('security.context')->isGranted('EDIT',$user)) {
throw new \Symfony\ComponentSecurity\Core\Exception\AccessDeniedException();
}
}
या JMSSecurityExtraBundle का उपयोग कर ...
/**
* @SecureParam(name="user", permissions="EDIT")
*/
public function editUser(UserInterface $user)
{
// ...
}
मैं इस तरह की कोशिश की के बारे में स्वीकार कर लिया गया है, यह संस्करण के लिए काम करता है (अन्य कंपनियों के लिए संस्करण से बचाता है), लेकिन सूची अभी भी सभी उपयोगकर्ताओं को प्रदर्शित करता है। ऐसा लगता है कि मतदाता को लाइन प्रदर्शित करने के लिए बुलाया नहीं जाता है। –
लाइन प्रदर्शित करके आपका क्या मतलब है? – nifr
जैसा कि मुझे सूची ग्रिड में केवल कुछ उपयोगकर्ताओं को दिखाने की ज़रूरत है, उनके लिए कोई भी लाइन प्रदर्शित नहीं की जानी चाहिए (केवल पढ़ने के बजाय) नोट: मैं सोनाटाएडमिन ग्रिड –