首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony ACL -如何更新acl对象?

Symfony ACL -如何更新acl对象?
EN

Stack Overflow用户
提问于 2014-08-07 18:29:31
回答 1查看 602关注 0票数 0

我要更新以前写入的ACL (如果不存在,则插入新的ACL)。我想做的不是工作:

代码语言:javascript
复制
public function upgradeUser(User $user, Model $model)
    {
        $acl = null;
        $objectIdentity = ObjectIdentity::fromDomainObject($model);
        $securityIdentity= UserSecurityIdentity::fromAccount($user);

        try {
            $acl = $this->aclProvider->findAcl($objectIdentity);

            /** @var Entry[] $aces */
            $aces = $acl->getObjectAces();
            foreach($aces as $i => $ace) {
                if ($securityIdentity->equals($securityIdentity)) {
                    $acl->updateObjectAce($i, $ace->getMask() & MaskBuilder::MASK_OPERATOR);
                }
            }

        } catch (AclNotFoundException $e) {
            $acl = $this->aclProvider->createAcl($objectIdentity);
            $acl->insertObjectAce($securityIdentity, MaskBuilder::MASK_OPERATOR);
        }

        $this->aclProvider->updateAcl($acl);
    }
EN

回答 1

Stack Overflow用户

发布于 2014-08-08 18:43:55

这个问题是由于错误的“插入”过程造成的。解决方案是在未找到更新的objected时使用InserObjectAcl方法:

代码语言:javascript
复制
protected function addMask(SecurityIdentityInterface $securityIdentity, $mask, $acl, $appendMask = true)
    {
        // flag to determine if mask was really updated or not
        $isUpdated = false;

        // go throw all aces and try to find current user's ace
        foreach ($acl->getObjectAces() as $ace) {
            if (!($ace instanceof Entry) || !$ace->getSecurityIdentity()->equals($securityIdentity))
                continue;

            $maskBuilder = new MaskBuilder($appendMask ? $ace->getMask() : 0);
            $maskBuilder->add($mask);
            $ace->setMask($maskBuilder->get());

            $isUpdated = true;
            break;
        }

        // in the case if object was not found in aces for this user, insert a new ace
        if ($isUpdated === false)
            $acl->insertObjectAce($securityIdentity, $mask);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25180167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档