Reputation: 25146
I'm trying to override the MongoRepository::findAll() method to define the custom @Query
. I am following the SpEL JPA Query support docs to have similar logic in MongoRepository to check for user role and create a dynamic query to fetch the authorized records.
I've the following method setup in my BlogRepository which extends MongoRepository:
@Query("{ user_id : ?#{ hasRole('ROLE_ADMIN') ? /./ : principal.userId } } ")
@Override
Page<Blog> findAll(Pageable pageable);
I get the following error from this method:
org.springframework.expression.spel.SpelParseException: EL1049E:(pos 31): Unexpected data after '.': 'div(/)'
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.raiseInternalException(InternalSpelExpressionParser.java:988)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatDottedNode(InternalSpelExpressionParser.java:407)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.maybeEatNode(InternalSpelExpressionParser.java:360)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatPrimaryExpression(InternalSpelExpressionParser.java:345)
at org.springframework.expression.spel.standard.InternalSpelExpressionParser.eatUnaryExpression(InternalSpelExpressionParser.java:337)
As the below mongo query runs and retuns the desired values, I believe, I just need to escape the /./
character so that SpEL doesn't try to parse it.
{ 'user_id' : /./ }
I couldn't find a escape character for SpEL but played with the following options and still similar kind of parse exception is coming up:
'/./'
#{/./}
-- wraping with SpEL expression.
/.*.*/
Could you help me out?
Upvotes: 2
Views: 1891
Reputation: 25146
I got it working :
@Query(" ?#{ hasRole('ROLE_ADMIN') ? '{}' : {'user_id' : principal.userId } } ")
Upvotes: 1