Search in sources :

Example 81 with PrincipalKey

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

the class GetMembersHandler method getMembers.

public List<PrincipalMapper> getMembers() {
    final PrincipalRelationships relationships = this.securityService.get().getRelationships(this.principalKey);
    final PrincipalKeys principalKeys = PrincipalKeys.from(relationships.stream().map(PrincipalRelationship::getTo).toArray(PrincipalKey[]::new));
    final Principals principals = this.securityService.get().getPrincipals(principalKeys);
    return principals.stream().map(PrincipalMapper::new).collect(Collectors.toList());
}
Also used : PrincipalKeys(com.enonic.xp.security.PrincipalKeys) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) Principals(com.enonic.xp.security.Principals) PrincipalKey(com.enonic.xp.security.PrincipalKey) PrincipalRelationships(com.enonic.xp.security.PrincipalRelationships)

Example 82 with PrincipalKey

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

the class Auth0LoginService method updateProfile.

private void updateProfile(final Auth0User auth0User, final PrincipalKey principalKey) {
    final UpdateUserParams updateUserParams = UpdateUserParams.create().userKey(principalKey).editor(editableUser -> this.updateProfile(editableUser.profile, auth0User)).build();
    runAs(() -> securityService.updateUser(updateUserParams), RoleKeys.ADMIN);
}
Also used : IdProviderKey(com.enonic.xp.security.IdProviderKey) UpdateUserParams(com.enonic.xp.security.UpdateUserParams) QueryParser(com.enonic.xp.query.parser.QueryParser) Auth0User(com.enonic.app.auth0.impl.user.Auth0User) Callable(java.util.concurrent.Callable) UserQuery(com.enonic.xp.security.UserQuery) HttpServletRequest(javax.servlet.http.HttpServletRequest) Component(org.osgi.service.component.annotations.Component) ContextAccessor(com.enonic.xp.context.ContextAccessor) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) ContextBuilder(com.enonic.xp.context.ContextBuilder) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) QueryExpr(com.enonic.xp.query.expr.QueryExpr) SecurityService(com.enonic.xp.security.SecurityService) PropertyTree(com.enonic.xp.data.PropertyTree) HttpSession(javax.servlet.http.HttpSession) CreateUserParams(com.enonic.xp.security.CreateUserParams) User(com.enonic.xp.security.User) PropertySet(com.enonic.xp.data.PropertySet) PrincipalRelationship(com.enonic.xp.security.PrincipalRelationship) ConstraintExpr(com.enonic.xp.query.expr.ConstraintExpr) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) PrincipalKey(com.enonic.xp.security.PrincipalKey) RoleKeys(com.enonic.xp.security.RoleKeys) Reference(org.osgi.service.component.annotations.Reference) PrincipalKeys(com.enonic.xp.security.PrincipalKeys) UpdateUserParams(com.enonic.xp.security.UpdateUserParams)

Example 83 with PrincipalKey

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

the class Auth0LoginService method login.

public void login(final HttpServletRequest request, final Auth0User auth0User, final IdProviderKey idProviderKey) {
    // Retrieves the user by key
    final String userId = auth0User.getUserId().replace('|', '-');
    final PrincipalKey principalKey = PrincipalKey.ofUser(idProviderKey, userId);
    User user = runAs(() -> securityService.getUser(principalKey), RoleKeys.AUTHENTICATED).orElse(null);
    // If the user does not exist with this id
    if (user == null && auth0User.getEmail() != null) {
        // Retrieves the user by email
        final ConstraintExpr constraintExpr = QueryParser.parseCostraintExpression("userstorekey = '" + idProviderKey + "' AND email = '" + auth0User.getEmail() + "'");
        final QueryExpr queryExpr = QueryExpr.from(constraintExpr);
        final UserQuery userQuery = UserQuery.create().size(1).queryExpr(queryExpr).build();
        user = (User) runAs(() -> securityService.query(userQuery), RoleKeys.AUTHENTICATED).getUsers().first();
    }
    // If the user does not exist
    if (user == null) {
        // Creates the user
        user = createUser(auth0User, principalKey);
    }
    // Updates the profile
    updateProfile(auth0User, user.getKey());
    // Authenticates the user
    authenticate(request, user.getKey());
}
Also used : Auth0User(com.enonic.app.auth0.impl.user.Auth0User) User(com.enonic.xp.security.User) ConstraintExpr(com.enonic.xp.query.expr.ConstraintExpr) QueryExpr(com.enonic.xp.query.expr.QueryExpr) UserQuery(com.enonic.xp.security.UserQuery) PrincipalKey(com.enonic.xp.security.PrincipalKey)

Example 84 with PrincipalKey

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

PrincipalKey (com.enonic.xp.security.PrincipalKey)84 Test (org.junit.jupiter.api.Test)47 PropertyTree (com.enonic.xp.data.PropertyTree)22 User (com.enonic.xp.security.User)18 AbstractElasticsearchIntegrationTest (com.enonic.xp.repo.impl.elasticsearch.AbstractElasticsearchIntegrationTest)15 CreateUserParams (com.enonic.xp.security.CreateUserParams)14 PrincipalKeys (com.enonic.xp.security.PrincipalKeys)14 Instant (java.time.Instant)12 Node (com.enonic.xp.node.Node)10 DescriptorKey (com.enonic.xp.page.DescriptorKey)10 Context (com.enonic.xp.context.Context)9 IdProviderKey (com.enonic.xp.security.IdProviderKey)9 PrincipalRelationship (com.enonic.xp.security.PrincipalRelationship)9 CreateGroupParams (com.enonic.xp.security.CreateGroupParams)8 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)8 NodeId (com.enonic.xp.node.NodeId)7 NodeQuery (com.enonic.xp.node.NodeQuery)7 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)7 Group (com.enonic.xp.security.Group)6 AccessControlList (com.enonic.xp.security.acl.AccessControlList)6