Search in sources :

Example 36 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class AuditLogServiceImplTest_find method find_anonymous.

@Test
public void find_anonymous() {
    final Context context = ContextBuilder.create().repositoryId(AuditLogConstants.AUDIT_LOG_REPO_ID).branch(AuditLogConstants.AUDIT_LOG_BRANCH).authInfo(AuthenticationInfo.create().principals(PrincipalKey.ofAnonymous()).user(User.ANONYMOUS).build()).build();
    AuditLogContext.createAdminContext().runWith(() -> {
        LogAuditLogParams params = LogAuditLogParams.create().type("test").build();
        AuditLog log = auditLogService.log(params);
        context.runWith(() -> {
            FindAuditLogResult result = auditLogService.find(FindAuditLogParams.create().ids(AuditLogIds.from(log.getId())).build());
            assertEquals(0L, result.getCount());
        });
    });
}
Also used : AuditLogContext(com.enonic.xp.core.impl.audit.AuditLogContext) Context(com.enonic.xp.context.Context) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) FindAuditLogResult(com.enonic.xp.audit.FindAuditLogResult) AuditLog(com.enonic.xp.audit.AuditLog) Test(org.junit.jupiter.api.Test)

Example 37 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class AuditLogServiceImplTest_log method log_anonymous.

@Test
public void log_anonymous() {
    final Context context = ContextBuilder.create().repositoryId(AuditLogConstants.AUDIT_LOG_REPO_ID).branch(AuditLogConstants.AUDIT_LOG_BRANCH).authInfo(AuthenticationInfo.create().principals(PrincipalKey.ofAnonymous()).user(User.ANONYMOUS).build()).build();
    context.runWith(() -> {
        LogAuditLogParams params = LogAuditLogParams.create().type("test").build();
        assertNull(auditLogService.log(params));
    });
}
Also used : AuditLogContext(com.enonic.xp.core.impl.audit.AuditLogContext) Context(com.enonic.xp.context.Context) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) Test(org.junit.jupiter.api.Test)

Example 38 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class UnpublishContentHandler method execute.

public List<String> execute() {
    final Branch targetBranch = ContentConstants.BRANCH_MASTER;
    final Context context = ContextBuilder.from(ContextAccessor.current()).branch(targetBranch).build();
    final List<ContentId> contentIds = context.callWith(this::resolveContentIds);
    final UnpublishContentParams unpublishContentParams = UnpublishContentParams.create().contentIds(ContentIds.from(contentIds)).unpublishBranch(targetBranch).includeChildren(true).build();
    final UnpublishContentsResult result = this.contentService.unpublishContent(unpublishContentParams);
    return result.getUnpublishedContents().stream().map(ContentId::toString).collect(Collectors.toList());
}
Also used : BeanContext(com.enonic.xp.script.bean.BeanContext) Context(com.enonic.xp.context.Context) Branch(com.enonic.xp.branch.Branch) UnpublishContentParams(com.enonic.xp.content.UnpublishContentParams) UnpublishContentsResult(com.enonic.xp.content.UnpublishContentsResult) ContentId(com.enonic.xp.content.ContentId)

Example 39 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class ApplicationAuditLogSupportImpl method log.

private void log(final String type, final PropertyTree data, final AuditLogUris uris) {
    final Context rootContext = ContextBuilder.copyOf(ContextAccessor.current()).build();
    final PrincipalKey userPrincipalKey = rootContext.getAuthInfo().getUser() != null ? rootContext.getAuthInfo().getUser().getKey() : PrincipalKey.ofAnonymous();
    ContextBuilder.from(rootContext).authInfo(AuthenticationInfo.copyOf(rootContext.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 40 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class ContentAuditLogSupportImplTest method testUpdateContent.

@Test
public void testUpdateContent() throws Exception {
    final PropertyTree propertyTree = new PropertyTree();
    propertyTree.addString("test-data", "test-data");
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "testUser")).displayName("Test User").modifiedTime(Instant.now()).email("test-user@enonic.com").login("test-user").build();
    final UpdateContentParams params = new UpdateContentParams().requireValid(true).contentId(ContentId.from("contentId")).clearAttachments(true).modifier(user.getKey()).editor(edit -> edit.displayName = "New Display Name");
    final Content content = Content.create().id(ContentId.from("contentId")).type(ContentTypeName.site()).name("contentName").displayName("displayName").parentPath(ContentPath.ROOT).build();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN_LOGIN).build();
    final Context context = ContextBuilder.create().branch(ContentConstants.BRANCH_DRAFT).repositoryId(RepositoryId.from("test-repository")).authInfo(authInfo).build();
    // test
    context.runWith(() -> support.update(params, content));
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
    // verify and assert
    final ArgumentCaptor<LogAuditLogParams> argumentCaptor = ArgumentCaptor.forClass(LogAuditLogParams.class);
    Mockito.verify(auditLogService, Mockito.times(1)).log(argumentCaptor.capture());
    Assertions.assertEquals(user.getKey(), argumentCaptor.getValue().getUser());
    final String modifier = argumentCaptor.getValue().getData().getSet("params").getString("modifier");
    Assertions.assertEquals(user.getKey().toString(), modifier);
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) UpdateContentParams(com.enonic.xp.content.UpdateContentParams) Content(com.enonic.xp.content.Content) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Test(org.junit.jupiter.api.Test)

Aggregations

Context (com.enonic.xp.context.Context)101 Test (org.junit.jupiter.api.Test)35 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)21 Node (com.enonic.xp.node.Node)16 InternalContext (com.enonic.xp.repo.impl.InternalContext)16 User (com.enonic.xp.security.User)13 Content (com.enonic.xp.content.Content)11 PropertyTree (com.enonic.xp.data.PropertyTree)11 PrincipalKey (com.enonic.xp.security.PrincipalKey)10 Branch (com.enonic.xp.branch.Branch)9 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)9 ContextAccessor (com.enonic.xp.context.ContextAccessor)8 NodePath (com.enonic.xp.node.NodePath)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)7 ContentId (com.enonic.xp.content.ContentId)6 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)6 Repository (com.enonic.xp.repository.Repository)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)5 ProcessUpdateParams (com.enonic.xp.content.processor.ProcessUpdateParams)5 NodeComparison (com.enonic.xp.node.NodeComparison)5