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());
});
});
}
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));
});
}
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());
}
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()));
}
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);
}
Aggregations