Search in sources :

Example 1 with WidgetDescriptor

use of com.enonic.xp.admin.widget.WidgetDescriptor in project xp by enonic.

the class WidgetHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    if (this.request.getMode() != RenderMode.ADMIN) {
        throw WebException.forbidden("Render mode must be ADMIN.");
    }
    // Retrieves the WidgetDescriptor
    final WidgetDescriptor widgetDescriptor = widgetDescriptorService.getByKey(descriptorKey);
    if (widgetDescriptor == null) {
        throw WebException.notFound(String.format("Widget [%s] not found", descriptorKey));
    }
    // Checks if the access to WidgetDescriptor is allowed
    final PrincipalKeys principals = ContextAccessor.current().getAuthInfo().getPrincipals();
    if (!widgetDescriptor.isAccessAllowed(principals)) {
        throw WebException.forbidden(String.format("You don't have permission to access [%s]", descriptorKey));
    }
    // Renders the widget
    this.request.setApplicationKey(this.descriptorKey.getApplicationKey());
    final ResourceKey scriptDir = ResourceKey.from(descriptorKey.getApplicationKey(), "admin/widgets/" + descriptorKey.getName());
    final ControllerScript controllerScript = this.controllerScriptFactory.fromDir(scriptDir);
    return controllerScript.execute(this.request);
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) ControllerScript(com.enonic.xp.portal.controller.ControllerScript) WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey)

Example 2 with WidgetDescriptor

use of com.enonic.xp.admin.widget.WidgetDescriptor in project xp by enonic.

the class WidgetDescriptorLoader method load.

@Override
public WidgetDescriptor load(final DescriptorKey key, final Resource resource) {
    final WidgetDescriptor.Builder builder = WidgetDescriptor.create();
    builder.key(key);
    final String descriptorXml = resource.readString();
    parseXml(key.getApplicationKey(), builder, descriptorXml);
    final Icon icon = loadIcon(key);
    builder.setIcon(icon);
    return builder.build();
}
Also used : WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) Icon(com.enonic.xp.icon.Icon)

Example 3 with WidgetDescriptor

use of com.enonic.xp.admin.widget.WidgetDescriptor in project xp by enonic.

the class WidgetHandlerTest method mockDescriptor.

private void mockDescriptor(boolean hasAccess) {
    WidgetDescriptor descriptor = Mockito.mock(WidgetDescriptor.class);
    Mockito.when(descriptor.isAccessAllowed(Mockito.any(PrincipalKeys.class))).thenReturn(hasAccess);
    Mockito.when(this.widgetDescriptorService.getByKey(Mockito.any(DescriptorKey.class))).thenReturn(descriptor);
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) DescriptorKey(com.enonic.xp.page.DescriptorKey) WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor)

Example 4 with WidgetDescriptor

use of com.enonic.xp.admin.widget.WidgetDescriptor in project xp by enonic.

the class WidgetDescriptorLoaderTest method testPostProcess.

@Test
public void testPostProcess() {
    final WidgetDescriptor descriptor = WidgetDescriptor.create().key(DescriptorKey.from("myapp:a")).build();
    assertSame(descriptor, this.loader.postProcess(descriptor));
}
Also used : WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) Test(org.junit.jupiter.api.Test)

Example 5 with WidgetDescriptor

use of com.enonic.xp.admin.widget.WidgetDescriptor in project xp by enonic.

the class WidgetDescriptorLoaderTest method testLoadMin.

@Test
public void testLoadMin() {
    final DescriptorKey descriptorKey = DescriptorKey.from("myapp1:widget2");
    final ResourceKey resourceKey = this.loader.toResource(descriptorKey);
    final Resource resource = this.resourceService.getResource(resourceKey);
    final WidgetDescriptor descriptor = this.loader.load(descriptorKey, resource);
    assertEquals("MyWidget2", descriptor.getDisplayName());
    assertEquals("MyWidget2 description", descriptor.getDescription());
    assertEquals(1, descriptor.getInterfaces().size());
    assertTrue(descriptor.getInterfaces().contains("com.enonic.xp.my-interface"));
    assertNull(descriptor.getAllowedPrincipals());
}
Also used : Resource(com.enonic.xp.resource.Resource) DescriptorKey(com.enonic.xp.page.DescriptorKey) WidgetDescriptor(com.enonic.xp.admin.widget.WidgetDescriptor) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Aggregations

WidgetDescriptor (com.enonic.xp.admin.widget.WidgetDescriptor)10 Test (org.junit.jupiter.api.Test)6 DescriptorKey (com.enonic.xp.page.DescriptorKey)4 ResourceKey (com.enonic.xp.resource.ResourceKey)3 Resource (com.enonic.xp.resource.Resource)2 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)2 Context (com.enonic.xp.context.Context)1 Icon (com.enonic.xp.icon.Icon)1 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)1 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)1