Search in sources :

Example 16 with User

use of com.enonic.xp.security.User 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)

Example 17 with User

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

the class ContentInitializer method initContentNode.

private void initContentNode() {
    final Node contentRootNode = nodeService.getByPath(ContentConstants.CONTENT_ROOT_PATH);
    final User user = ContextAccessor.current().getAuthInfo().getUser();
    if (contentRootNode == null) {
        LOG.info("Content root-node not found, creating");
        PropertyTree data = new PropertyTree();
        data.setString(ContentPropertyNames.TYPE, "base:folder");
        data.setString(ContentPropertyNames.DISPLAY_NAME, "Content");
        data.addSet(ContentPropertyNames.DATA);
        data.addSet(ContentPropertyNames.FORM);
        data.setString(ContentPropertyNames.CREATOR, user.getKey().toString());
        data.setInstant(ContentPropertyNames.CREATED_TIME, Instant.now());
        final Node contentRoot = nodeService.create(CreateNodeParams.create().data(data).name(ContentConstants.CONTENT_ROOT_NAME).parent(NodePath.ROOT).permissions(Objects.requireNonNullElse(this.accessControlList, CONTENT_ROOT_DEFAULT_ACL)).childOrder(CONTENT_DEFAULT_CHILD_ORDER).build());
        LOG.info("Created content root-node: {}", contentRoot);
        nodeService.refresh(RefreshMode.ALL);
        nodeService.push(NodeIds.from(contentRoot.id()), ContentConstants.BRANCH_MASTER);
    }
}
Also used : User(com.enonic.xp.security.User) Node(com.enonic.xp.node.Node) PropertyTree(com.enonic.xp.data.PropertyTree)

Example 18 with User

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

the class SystemRepoInitializer method createAdminContext.

private Context createAdminContext() {
    final User admin = User.create().key(SUPER_USER).login(SUPER_USER.getId()).build();
    final AuthenticationInfo authInfo = AuthenticationInfo.create().principals(RoleKeys.ADMIN).user(admin).build();
    return ContextBuilder.create().branch(SecurityConstants.BRANCH_SECURITY).repositoryId(SystemConstants.SYSTEM_REPO_ID).authInfo(authInfo).build();
}
Also used : User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo)

Example 19 with User

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

the class ScheduleAuditLogSupportImplTest method testUpdateContent.

@Test
public void testUpdateContent() throws Exception {
    final PropertyTree config = new PropertyTree();
    config.addString("property", "value");
    final CronCalendarImpl calendar = CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getTimeZone("GMT+5:30")).build();
    final DescriptorKey descriptor = DescriptorKey.from("appKey:descriptorName");
    final String jobDescription = "Job description";
    final PrincipalKey userKey = PrincipalKey.from("user:system:user");
    final OneTimeCalendarImpl oneTimeCalendar = OneTimeCalendarImpl.create().value(Instant.parse("2021-04-25T10:44:33.170079900Z")).build();
    final ModifyScheduledJobParams params = ModifyScheduledJobParams.create().name(name).editor(edit -> edit.calendar = oneTimeCalendar).build();
    final ScheduledJob job = ScheduledJob.create().name(name).calendar(oneTimeCalendar).descriptor(descriptor).description(jobDescription).config(config).enabled(true).user(userKey).creator(PrincipalKey.from("user:system:creator")).modifier(PrincipalKey.from("user:system:creator")).createdTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).build();
    context.runWith(() -> support.modify(params, job));
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
    final ArgumentCaptor<LogAuditLogParams> argumentCaptor = ArgumentCaptor.forClass(LogAuditLogParams.class);
    verify(auditLogService, times(1)).log(argumentCaptor.capture());
    assertEquals(user.getKey().toString(), argumentCaptor.getValue().getData().getSet("params").getString("modifier"));
    assertEquals("2021-04-25T10:44:33.170079900Z", argumentCaptor.getValue().getData().getSet("result").getSet("calendar").getString("value"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) IdProviderKey(com.enonic.xp.security.IdProviderKey) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ArgumentCaptor(org.mockito.ArgumentCaptor) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) AuditLogService(com.enonic.xp.audit.AuditLogService) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) User(com.enonic.xp.security.User) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TimeZone(java.util.TimeZone) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.times(org.mockito.Mockito.times) Instant(java.time.Instant) Executors(java.util.concurrent.Executors) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) Context(com.enonic.xp.context.Context) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 20 with User

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

the class ScheduleAuditLogSupportImplTest method testUpdateWithoutCreator.

@Test
public // jobs were produced with an empty `creator` and `createdTime` fields from 7.7.0 to 7.7.2
void testUpdateWithoutCreator() throws Exception {
    final PropertyTree config = new PropertyTree();
    config.addString("property", "value");
    final CronCalendarImpl calendar = CronCalendarImpl.create().value("* * * * *").timeZone(TimeZone.getTimeZone("GMT+5:30")).build();
    final DescriptorKey descriptor = DescriptorKey.from("appKey:descriptorName");
    final String jobDescription = "Job description";
    final PrincipalKey userKey = PrincipalKey.from("user:system:user");
    final OneTimeCalendarImpl oneTimeCalendar = OneTimeCalendarImpl.create().value(Instant.parse("2021-04-25T10:44:33.170079900Z")).build();
    final ModifyScheduledJobParams params = ModifyScheduledJobParams.create().name(name).editor(edit -> edit.calendar = oneTimeCalendar).build();
    final ScheduledJob job = ScheduledJob.create().name(name).calendar(oneTimeCalendar).descriptor(descriptor).description(jobDescription).config(config).enabled(true).user(userKey).modifier(PrincipalKey.from("user:system:creator")).createdTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).modifiedTime(Instant.parse("2021-02-25T10:44:33.170079900Z")).build();
    context.runWith(() -> support.modify(params, job));
    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);
    final ArgumentCaptor<LogAuditLogParams> argumentCaptor = ArgumentCaptor.forClass(LogAuditLogParams.class);
    verify(auditLogService, times(1)).log(argumentCaptor.capture());
    assertNull(argumentCaptor.getValue().getData().getSet("params").getString("creator"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) IdProviderKey(com.enonic.xp.security.IdProviderKey) Mock(org.mockito.Mock) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ArgumentCaptor(org.mockito.ArgumentCaptor) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) AuditLogService(com.enonic.xp.audit.AuditLogService) ContextBuilder(com.enonic.xp.context.ContextBuilder) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) User(com.enonic.xp.security.User) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) TimeZone(java.util.TimeZone) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Mockito.times(org.mockito.Mockito.times) Instant(java.time.Instant) Executors(java.util.concurrent.Executors) TaskId(com.enonic.xp.task.TaskId) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) Context(com.enonic.xp.context.Context) CreateScheduledJobParams(com.enonic.xp.scheduler.CreateScheduledJobParams) OneTimeCalendarImpl(com.enonic.xp.impl.scheduler.distributed.OneTimeCalendarImpl) ModifyScheduledJobParams(com.enonic.xp.scheduler.ModifyScheduledJobParams) CronCalendarImpl(com.enonic.xp.impl.scheduler.distributed.CronCalendarImpl) LogAuditLogParams(com.enonic.xp.audit.LogAuditLogParams) PropertyTree(com.enonic.xp.data.PropertyTree) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) DescriptorKey(com.enonic.xp.page.DescriptorKey) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Aggregations

User (com.enonic.xp.security.User)63 Test (org.junit.jupiter.api.Test)40 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)22 PropertyTree (com.enonic.xp.data.PropertyTree)17 PrincipalKey (com.enonic.xp.security.PrincipalKey)17 Context (com.enonic.xp.context.Context)14 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)11 CreateUserParams (com.enonic.xp.security.CreateUserParams)10 IdProviderKey (com.enonic.xp.security.IdProviderKey)10 Node (com.enonic.xp.node.Node)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)6 UpdateUserParams (com.enonic.xp.security.UpdateUserParams)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Mockito (org.mockito.Mockito)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)4 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)4 UpdateNodeParams (com.enonic.xp.node.UpdateNodeParams)4 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)4 TaskId (com.enonic.xp.task.TaskId)4