Search in sources :

Example 46 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey in project xp by enonic.

the class SecurityAuditLogSupportImpl method log.

private void log(final String type, final PropertyTree data, final AuditLogUris uris) {
    final Context context = ContextBuilder.copyOf(ContextAccessor.current()).build();
    final PrincipalKey userPrincipalKey = context.getAuthInfo().getUser() != null ? context.getAuthInfo().getUser().getKey() : PrincipalKey.ofAnonymous();
    ContextBuilder.from(context).authInfo(AuthenticationInfo.copyOf(context.getAuthInfo()).principals(RoleKeys.AUDIT_LOG).build()).build().callWith(() -> auditLogService.log(LogAuditLogParams.create().type(type).source(SOURCE).data(data).objectUris(uris).user(userPrincipalKey).build()));
}
Also used : Context(com.enonic.xp.context.Context) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 47 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey in project xp by enonic.

the class GetMembershipsHandlerTest method testGetUserMemberships.

@Test
public void testGetUserMemberships() {
    final Group group = TestDataFixtures.getTestGroup();
    final PrincipalKeys principalKeys = PrincipalKeys.from(group.getKey());
    final PrincipalKey pKey = PrincipalKey.from("user:myIdProvider:userId");
    Mockito.when(securityService.getMemberships(pKey)).thenReturn(principalKeys);
    Mockito.verify(securityService, Mockito.never()).getAllMemberships(pKey);
    Mockito.when(securityService.getPrincipals(principalKeys)).thenReturn(Principals.from(group));
    runFunction("/test/getMemberships-test.js", "getUserMemberships");
}
Also used : Group(com.enonic.xp.security.Group) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 48 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey in project xp by enonic.

the class DeletePrincipalHandlerTest method testDeleteNonExistingUser.

@Test
public void testDeleteNonExistingUser() {
    final PrincipalKey principalKey = PrincipalKey.from("user:myIdProvider:XXX");
    Mockito.doThrow(new PrincipalNotFoundException(principalKey)).when(securityService).deletePrincipal(principalKey);
    runFunction("/test/deletePrincipal-test.js", "deleteNonExistingUser");
}
Also used : PrincipalNotFoundException(com.enonic.xp.security.PrincipalNotFoundException) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 49 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey in project xp by enonic.

the class ServiceHandlerTest method testForbiddenService.

@Test
public void testForbiddenService() throws Exception {
    final DescriptorKey serviceDescriptorKey = DescriptorKey.from("demo:test");
    final Set<PrincipalKey> allowedPrincipals = Collections.singleton(PrincipalKey.from("role:system.admin"));
    final ServiceDescriptor serviceDescriptor = ServiceDescriptor.create().key(serviceDescriptorKey).setAllowedPrincipals(allowedPrincipals).build();
    Mockito.when(this.serviceDescriptorService.getByKey(serviceDescriptorKey)).thenReturn(serviceDescriptor);
    this.request.setEndpointPath("/_/service/demo/test");
    boolean forbiddenErrorThrown = false;
    try {
        this.handler.handle(this.request, PortalResponse.create().build(), null);
    } catch (WebException e) {
        if (HttpStatus.UNAUTHORIZED == e.getStatus()) {
            forbiddenErrorThrown = true;
        }
    }
    assertTrue(forbiddenErrorThrown);
}
Also used : WebException(com.enonic.xp.web.WebException) ServiceDescriptor(com.enonic.xp.service.ServiceDescriptor) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 50 with PrincipalKey

use of com.enonic.xp.security.PrincipalKey in project xp by enonic.

the class IssueDataSerializer method toCreateNodeData.

public PropertyTree toCreateNodeData(final CreateIssueParams params) {
    final PropertyTree propertyTree = new PropertyTree();
    final PropertySet issueAsData = propertyTree.getRoot();
    issueAsData.ifNotNull().addEnum(TYPE, params.getIssueType());
    issueAsData.ifNotNull().addString(TITLE, params.getTitle());
    issueAsData.ifNotNull().addString(STATUS, params.getStatus().toString());
    issueAsData.addString(DESCRIPTION, params.getDescription());
    if (params.getApproverIds().getSize() > 0) {
        issueAsData.addStrings(APPROVERS, params.getApproverIds().stream().map(PrincipalKey::toString).collect(Collectors.toList()));
    }
    if (params.getPublishRequest() != null) {
        addPublishRequest(issueAsData, params.getPublishRequest());
    }
    if (params instanceof CreatePublishRequestIssueParams) {
        publishRequestIssueSerializer.toCreateNodeData((CreatePublishRequestIssueParams) params, issueAsData);
    }
    return propertyTree;
}
Also used : CreatePublishRequestIssueParams(com.enonic.xp.issue.CreatePublishRequestIssueParams) PropertyTree(com.enonic.xp.data.PropertyTree) PropertySet(com.enonic.xp.data.PropertySet) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Aggregations

PrincipalKey (com.enonic.xp.security.PrincipalKey)84 Test (org.junit.jupiter.api.Test)47 PropertyTree (com.enonic.xp.data.PropertyTree)22 User (com.enonic.xp.security.User)18 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)15 CreateUserParams (com.enonic.xp.security.CreateUserParams)14 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)14 Instant (java.time.Instant)12 Node (com.enonic.xp.node.Node)10 DescriptorKey (com.enonic.xp.page.DescriptorKey)10 Context (com.enonic.xp.context.Context)9 IdProviderKey (com.enonic.xp.security.IdProviderKey)9 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)9 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)8 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)8 NodeId (com.enonic.xp.node.NodeId)7 NodeQuery (com.enonic.xp.node.NodeQuery)7 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)7 Group (com.enonic.xp.security.Group)6 AccessControlList (com.enonic.xp.security.acl.AccessControlList)6