Search in sources :

Example 51 with User

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

the class AuthenticationInfoTest method testSerializationWithUserProfile.

@Test
void testSerializationWithUserProfile() throws Exception {
    final PropertySet data = new PropertySet();
    data.setString("subString", "subStringValue");
    data.setLong("subLong", 123L);
    final PropertyTree userProfile = new PropertyTree();
    userProfile.setSet("myApp", data);
    userProfile.setString("string", "stringValue");
    final User user = User.create().login("userlogin").displayName("my user").key(PrincipalKey.ofUser(IdProviderKey.from("myidprovider"), "userid")).email("user@email").modifiedTime(Instant.now(clock)).profile(userProfile).build();
    final IdProviderKey idProvider = IdProviderKey.from("myStore");
    final PrincipalKey group1 = PrincipalKey.ofGroup(idProvider, "group1");
    final PrincipalKey group2 = PrincipalKey.from("group:myStore:group2");
    final PrincipalKey role1 = PrincipalKey.from("role:administrators");
    final AuthenticationInfo info = AuthenticationInfo.create().user(user).principals(group1).principals(PrincipalKeys.from(group2, role1)).build();
    final byte[] serializedObject = serialize(info);
    final AuthenticationInfo deserializedObject = (AuthenticationInfo) deserialize(serializedObject);
    assertEquals(deserializedObject, info);
}
Also used : User(com.enonic.xp.security.User) PropertyTree(com.enonic.xp.data.PropertyTree) IdProviderKey(com.enonic.xp.security.IdProviderKey) PropertySet(com.enonic.xp.data.PropertySet) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test)

Example 52 with User

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

the class CreateContentCommand method populateCreator.

private void populateCreator(final CreateContentTranslatorParams.Builder builder) {
    final User currentUser = getCurrentUser();
    builder.creator(currentUser.getKey());
}
Also used : User(com.enonic.xp.security.User)

Example 53 with User

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

the class ContentAuditLogSupportImplTest method testCreateContent.

@Test
public void testCreateContent() throws Exception {
    final PropertyTree propertyTree = new PropertyTree();
    propertyTree.addString("test-data", "test-data");
    final CreateContentParams params = CreateContentParams.create().type(ContentTypeName.site()).parent(ContentPath.ROOT).contentData(propertyTree).displayName("displayName").build();
    final Content content = Content.create().id(ContentId.from("contentId")).type(ContentTypeName.site()).name("contentName").displayName("displayName").parentPath(ContentPath.ROOT).build();
    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 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.createContent(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());
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) CreateContentParams(com.enonic.xp.content.CreateContentParams) 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 54 with User

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

the class ProjectServiceImplTest method create_with_role_members.

@Test
void create_with_role_members() {
    final RepositoryId projectRepoId = RepositoryId.from("com.enonic.cms.test-project");
    adminContext().runWith(() -> {
        final User user1 = securityService.createUser(CreateUserParams.create().userKey(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("user1").login("user1").build());
        final User user2 = securityService.createUser(CreateUserParams.create().userKey(PrincipalKey.ofUser(IdProviderKey.system(), "user2")).displayName("user2").login("user2").build());
        doCreateProjectAsAdmin(ProjectName.from(projectRepoId), ProjectPermissions.create().addOwner(user1.getKey()).addOwner(user2.getKey()).build());
        final Set<PrincipalKey> members = securityService.getRelationships(PrincipalKey.ofRole("cms.project.test-project.owner")).stream().map(PrincipalRelationship::getTo).collect(Collectors.toSet());
        assertTrue(members.contains(user1.getKey()));
        assertTrue(members.contains(user2.getKey()));
    });
}
Also used : User(com.enonic.xp.security.User) RepositoryId(com.enonic.xp.repository.RepositoryId) PrincipalKey(com.enonic.xp.security.PrincipalKey) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 55 with User

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

the class ProjectServiceImplTest method modify_default_project_permissions.

@Test
void modify_default_project_permissions() {
    adminContext().runWith(() -> {
        final User user1 = securityService.createUser(CreateUserParams.create().userKey(PrincipalKey.ofUser(IdProviderKey.system(), "user1")).displayName("user1").login("user1").build());
        final RuntimeException ex = Assertions.assertThrows(RuntimeException.class, () -> projectService.modifyPermissions(ProjectConstants.DEFAULT_PROJECT_NAME, ProjectPermissions.create().addOwner(user1.getKey()).build()));
        assertEquals("Default project permissions cannot be modified.", ex.getMessage());
    });
}
Also used : User(com.enonic.xp.security.User) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

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