Search in sources :

Example 16 with CreateUserParams

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

the class SecurityServiceImplTest method testUpdateUser.

@Test
public void testUpdateUser() throws Exception {
    runAsAdmin(() -> {
        final CreateUserParams createUser = CreateUserParams.create().userKey(PrincipalKey.ofUser(SYSTEM, "User1")).displayName("User 1").email("user1@enonic.com").login("User1").build();
        final User user = securityService.createUser(createUser);
        refresh();
        final UpdateUserParams updateUserParams = UpdateUserParams.create(user).email("u2@enonic.net").build();
        final User updateUserResult = securityService.updateUser(updateUserParams);
        refresh();
        final User updatedUser = securityService.getUser(user.getKey()).get();
        assertEquals("u2@enonic.net", updateUserResult.getEmail());
        assertEquals("u2@enonic.net", updatedUser.getEmail());
        assertEquals("User1", updatedUser.getLogin());
        assertEquals("User 1", updatedUser.getDisplayName());
        assertEquals(PrincipalKey.ofUser(SYSTEM, "User1"), updatedUser.getKey());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) UpdateUserParams(com.enonic.xp.security.UpdateUserParams) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 17 with CreateUserParams

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

the class SecurityServiceImplTest method testAuthenticateByUsername.

@Test
public void testAuthenticateByUsername() throws Exception {
    runAsAdmin(() -> {
        final CreateUserParams createUser = CreateUserParams.create().userKey(PrincipalKey.ofUser(SYSTEM, "User1")).displayName("User 1").email("user1@enonic.com").login("User1").password("runar").build();
        final User user = securityService.createUser(createUser);
        refresh();
        final VerifiedUsernameAuthToken authToken = new VerifiedUsernameAuthToken();
        authToken.setUsername("user1");
        authToken.setIdProvider(SYSTEM);
        final AuthenticationInfo authInfo = securityService.authenticate(authToken);
        assertTrue(authInfo.isAuthenticated());
        assertEquals(user.getKey(), authInfo.getUser().getKey());
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 18 with CreateUserParams

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

the class SecurityServiceImplTest method testAddRelationship.

@Test
public void testAddRelationship() throws Exception {
    runAsAdmin(() -> {
        // set up
        final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
        final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("123456").build();
        final PrincipalKey userKey2 = PrincipalKey.ofUser(SYSTEM, "user2");
        final CreateUserParams createUser2 = CreateUserParams.create().userKey(userKey2).displayName("User 2").email("user2@enonic.com").login("user2").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "Group-a");
        final CreateGroupParams createGroup = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        securityService.createUser(createUser1);
        securityService.createUser(createUser2);
        securityService.createGroup(createGroup);
        PrincipalRelationship membership = PrincipalRelationship.from(groupKey1).to(userKey1);
        PrincipalRelationship membership2 = PrincipalRelationship.from(groupKey1).to(userKey2);
        // exercise
        securityService.addRelationship(membership);
        securityService.addRelationship(membership2);
        securityService.addRelationship(membership);
        refresh();
        // verify
        final PrincipalRelationships relationships = securityService.getRelationships(groupKey1);
        assertEquals(2, relationships.getSize());
        assertEquals(membership, relationships.get(0));
        assertEquals(membership2, relationships.get(1));
    });
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 19 with CreateUserParams

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

the class SecurityServiceImplTest method testGetAllMemberships.

@Test
public void testGetAllMemberships() throws Exception {
    runAsAdmin(() -> {
        final PrincipalKey userKey = PrincipalKey.ofUser(SYSTEM, "user1");
        final CreateUserParams createUser = CreateUserParams.create().userKey(userKey).displayName("User 1").email("user1@enonic.com").login("user1").password("123456").build();
        final PrincipalKey groupKey1 = PrincipalKey.ofGroup(SYSTEM, "group-a");
        final CreateGroupParams createGroup1 = CreateGroupParams.create().groupKey(groupKey1).displayName("Group A").build();
        final PrincipalKey groupKey2 = PrincipalKey.ofGroup(SYSTEM, "group-b");
        final CreateGroupParams createGroup2 = CreateGroupParams.create().groupKey(groupKey2).displayName("Group B").build();
        final PrincipalKey roleKey1 = PrincipalKey.ofRole("role-a");
        final CreateRoleParams createRole = CreateRoleParams.create().roleKey(roleKey1).displayName("Role A").description("Group A Description").build();
        securityService.createUser(createUser);
        securityService.createGroup(createGroup1);
        securityService.createGroup(createGroup2);
        securityService.createRole(createRole);
        securityService.addRelationship(PrincipalRelationship.from(groupKey1).to(userKey));
        securityService.addRelationship(PrincipalRelationship.from(groupKey2).to(groupKey1));
        securityService.addRelationship(PrincipalRelationship.from(roleKey1).to(groupKey2));
        refresh();
        final PrincipalKeys memberships = securityService.getAllMemberships(userKey);
        assertTrue(memberships.contains(groupKey1));
        assertTrue(memberships.contains(groupKey2));
        assertTrue(memberships.contains(roleKey1));
        assertEquals(3, memberships.getSize());
    });
}
Also used : CreateRoleParams(com.enonic.xp.security.CreateRoleParams) CreateUserParams(com.enonic.xp.security.CreateUserParams) CreateGroupParams(com.enonic.xp.security.CreateGroupParams) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalKey(com.enonic.xp.security.PrincipalKey) AbstractElasticsearchIntegrationTest(com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 20 with CreateUserParams

use of com.enonic.xp.security.CreateUserParams in project app-auth0-idprovider by enonic.

the class Auth0LoginService method createUser.

private User createUser(final Auth0User auth0User, final PrincipalKey principalKey) {
    final String email = auth0User.getEmail();
    final String name = auth0User.getName();
    final PrincipalKeys defaultPrincipals = configurationService.getDefaultPrincipals(principalKey.getIdProviderKey());
    final CreateUserParams createUserParams = CreateUserParams.create().login(principalKey.getId()).displayName(name).email(email).userKey(principalKey).build();
    return runAs(() -> {
        final User user = securityService.createUser(createUserParams);
        for (PrincipalKey defaultPrincipal : defaultPrincipals) {
            securityService.addRelationship(PrincipalRelationship.from(defaultPrincipal).to(principalKey));
        }
        return user;
    }, RoleKeys.ADMIN);
}
Also used : CreateUserParams(com.enonic.xp.security.CreateUserParams) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) Auth0User(com.enonic.app.auth0.impl.user.Auth0User) User(com.enonic.xp.security.User) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Aggregations

CreateUserParams (com.enonic.xp.security.CreateUserParams)20 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)18 Test (org.junit.jupiter.api.Test)18 PrincipalKey (com.enonic.xp.security.PrincipalKey)12 User (com.enonic.xp.security.User)11 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)7 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)6 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)4 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)3 PrincipalRelationships (com.enonic.xp.security.PrincipalRelationships)3 UpdateUserParams (com.enonic.xp.security.UpdateUserParams)3 EmailPasswordAuthToken (com.enonic.xp.security.auth.EmailPasswordAuthToken)3 UsernamePasswordAuthToken (com.enonic.xp.security.auth.UsernamePasswordAuthToken)3 CreateRoleParams (com.enonic.xp.security.CreateRoleParams)2 VerifiedEmailAuthToken (com.enonic.xp.security.auth.VerifiedEmailAuthToken)2 Auth0User (com.enonic.app.auth0.impl.user.Auth0User)1 AuditLogService (com.enonic.xp.audit.AuditLogService)1 Context (com.enonic.xp.context.Context)1 ContextBuilder (com.enonic.xp.context.ContextBuilder)1 AuditLogServiceImpl (com.enonic.xp.core.impl.audit.AuditLogServiceImpl)1