AclPrivilegeCollection example

// Only app lifecycle hooks can be received if app is deactivated         if (!$app->isActive() && !($event instanceof AppChangedEvent || $event instanceof AppDeletedEvent)) {
            return false;
        }

        if (!($this->privileges[$event->getName()] ?? null)) {
            $this->loadPrivileges($event->getName()$affectedRoles);
        }

        $privileges = $this->privileges[$event->getName()][$app->getAclRoleId()]
            ?? new AclPrivilegeCollection([]);

        if (!$event->isAllowed($app->getId()$privileges)) {
            return false;
        }

        return true;
    }

    /** * @param array<string> $affectedRoleIds */
    
public function testIsAllowed(): void
    {
        $appId = Uuid::randomHex();
        $app = (new AppEntity())
            ->assign(['id' => $appId]);
        $context = Context::createDefaultContext();
        $event = new AppActivatedEvent(
            $app,
            $context
        );

        static::assertTrue($event->isAllowed($appIdnew AclPrivilegeCollection([])));
        static::assertFalse($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }
}


    public function testIsAllowed(): void
    {
        $appId = Uuid::randomHex();
        $context = Context::createDefaultContext();
        $event = new AppDeletedEvent(
            $appId,
            $context
        );

        static::assertTrue($event->isAllowed($appIdnew AclPrivilegeCollection([])));
        static::assertFalse($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }
}
'primaryKey' => $entityId,
                'updatedFields' => [],
            ],
        ]$event->getWebhookPayload());
    }

    public function testIsAllowed(): void
    {
        $entityId = Uuid::randomHex();
        $event = HookableEntityWrittenEvent::fromWrittenEvent($this->getEntityWrittenEvent($entityId));

        $allowedPermissions = new AclPrivilegeCollection([
            ProductDefinition::ENTITY_NAME . ':' . AclRoleDefinition::PRIVILEGE_READ,
        ]);
        static::assertTrue($event->isAllowed(
            Uuid::randomHex(),
            $allowedPermissions
        ));

        $notAllowedPermissions = new AclPrivilegeCollection([
            CustomerDefinition::ENTITY_NAME . ':' . AclRoleDefinition::PRIVILEGE_READ,
        ]);
        static::assertFalse($event->isAllowed(
            

        $appId = Uuid::randomHex();
        $app = (new AppEntity())
            ->assign(['id' => $appId]);
        $context = Context::createDefaultContext();
        $event = new AppInstalledEvent(
            $app,
            Manifest::createFromXmlFile(__DIR__ . '/../Manifest/_fixtures/test/manifest.xml'),
            $context
        );

        static::assertTrue($event->isAllowed($appIdnew AclPrivilegeCollection([])));
        static::assertFalse($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }
}
'Content-Type' => 'application/json',
        ];
        $payload = [
            'name' => 'value',
        ];

        $event = new AppFlowActionEvent($eventName$headers$payload);

        static::assertEquals($eventName$event->getName());
        static::assertEquals($headers$event->getWebhookHeaders());
        static::assertEquals($payload$event->getWebhookPayload());
        static::assertTrue($event->isAllowed('11111', new AclPrivilegeCollection([])));
    }
}
/** * @dataProvider getEventsWithoutPermissions */
    public function testIsAllowedForNonEntityBasedEvents(FlowEventAware $rootEvent): void
    {
        $event = HookableBusinessEvent::fromBusinessEvent(
            $rootEvent,
            $this->getContainer()->get(BusinessEventEncoder::class)
        );

        static::assertTrue($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }

    /** * @dataProvider getEventsWithPermissions */
    public function testIsAllowedForEntityBasedEvents(FlowEventAware $rootEvent): void
    {
        $event = HookableBusinessEvent::fromBusinessEvent(
            $rootEvent,
            $this->getContainer()->get(BusinessEventEncoder::class)
        );

        

        $appId = Uuid::randomHex();
        $app = (new AppEntity())
            ->assign(['id' => $appId]);
        $context = Context::createDefaultContext();
        $event = new AppInstalledEvent(
            $app,
            Manifest::createFromXmlFile(__DIR__ . '/../Manifest/_fixtures/test/manifest.xml'),
            $context
        );

        static::assertTrue($event->isAllowed($appIdnew AclPrivilegeCollection([])));
        static::assertFalse($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }
}

        static::assertSame('app.config.changed', (new SystemConfigChangedHook([][]))->getName());
    }

    /** * @param array<string> $permissions * * @dataProvider getPermissionCases */
    public static function testPermissions(SystemConfigChangedHook $hook, array $permissions, bool $allowed): void
    {
        static::assertSame($allowed$hook->isAllowed('app-id', new AclPrivilegeCollection($permissions)));
    }

    public function testGetWebhookPayloadWithApp(): void
    {
        $hook = new SystemConfigChangedHook(['app.foo' => 'bar', 'bla.test' => 'bla']['app-id' => 'app']);
        $app = new AppEntity();
        $app->setName('app');

        static::assertSame(['app.foo']$hook->getWebhookPayload($app)['changes']);
    }

    
public function testIsAllowed(): void
    {
        $appId = Uuid::randomHex();
        $app = (new AppEntity())
            ->assign(['id' => $appId]);
        $context = Context::createDefaultContext();
        $event = new AppDeactivatedEvent(
            $app,
            $context
        );

        static::assertTrue($event->isAllowed($appIdnew AclPrivilegeCollection([])));
        static::assertFalse($event->isAllowed(Uuid::randomHex()new AclPrivilegeCollection([])));
    }
}


    public function testACL(): void
    {
        $mediaId = Uuid::randomHex();
        $context = Context::createDefaultContext();
        $mediaUploadEvent = new MediaUploadedEvent(
            $mediaId,
            $context
        );

        static::assertFalse($mediaUploadEvent->isAllowed('1', new AclPrivilegeCollection(['media:create'])));
        static::assertTrue($mediaUploadEvent->isAllowed('1', new AclPrivilegeCollection(['media:read'])));
    }
}
Home | Imprint | This part of the site doesn't use cookies.