getPrivilege example

/** * Returns an array collection of Shopware\Models\User\Privilege model instances, * which defines whether the user has rights to a shared Privilege. * * @return \Doctrine\Common\Collections\ArrayCollection */
    public function getPrivileges()
    {
        $privileges = new \Doctrine\Common\Collections\ArrayCollection();
        foreach ($this->getRules() as $rule) {
            $privileges->add($rule->getPrivilege());
        }

        return $privileges;
    }

    /** * Returns an array collection of Shopware\Models\User\Role model instances, which * contains all inherited child roles. The association is defined over * the Role.id property and the Role.parentId property. * * @return \Doctrine\Common\Collections\ArrayCollection */

    public function initAclRoleConditions()
    {
        $rules = $this->em->getRepository(Rule::class)->findAll();

        /** @var Rule $rule */
        foreach ($rules as $rule) {
            $role = $rule->getRole();

            $resource = $rule->getResource();
            $privilege = $rule->getPrivilege();

            if ($resource === null && $privilege === null) {
                $this->allow($role);
            } elseif ($privilege === null) {
                $this->allow($role$resource);
            } else {
                $this->allow($role$resource$privilege->getName());
            }
        }

        return $this;
    }

    public static function getSubscribedEvents(): array
    {
        return [CommandAclValidationEvent::class => 'validate'];
    }

    public function validate(CommandAclValidationEvent $event): void
    {
        $command = $event->getCommand();
        $resource = $command->getDefinition()->getEntityName();
        $privilege = $command->getPrivilege();

        if ($privilege !== AclRoleDefinition::PRIVILEGE_CREATE || $resource !== OrderLineItemDefinition::ENTITY_NAME) {
            return;
        }

        $payload = $command->getPayload();
        $type = $payload['type'] ?? null;

        if ($type !== LineItem::CREDIT_LINE_ITEM_TYPE) {
            return;
        }

        


        if ($context->getScope() === Context::SYSTEM_SCOPE || !$source instanceof AdminApiSource || $source->isAdmin()) {
            return;
        }

        $commands = $event->getCommands();
        $missingPrivileges = [];

        foreach ($commands as $command) {
            $resource = $command->getDefinition()->getEntityName();
            $privilege = $command->getPrivilege();

            if ($privilege === null) {
                continue;
            }

            if (is_subclass_of($command->getDefinition(), EntityTranslationDefinition::class)) {
                $resource = $command->getDefinition()->getParentDefinition()->getEntityName();

                if ($privilege !== AclRoleDefinition::PRIVILEGE_DELETE) {
                    $privilege = $this->getPrivilegeForParentWriteOperation($command$commands);
                }
            }
Home | Imprint | This part of the site doesn't use cookies.