3737import org .aopalliance .intercept .MethodInvocation ;
3838import org .apache .commons .lang3 .StringUtils ;
3939import org .apache .gravitino .Entity ;
40+ import org .apache .gravitino .GravitinoEnv ;
4041import org .apache .gravitino .MetadataObject ;
4142import org .apache .gravitino .NameIdentifier ;
4243import org .apache .gravitino .authorization .AuthorizationUtils ;
44+ import org .apache .gravitino .exceptions .ForbiddenException ;
45+ import org .apache .gravitino .exceptions .MetalakeNotInUseException ;
46+ import org .apache .gravitino .exceptions .NoSuchMetalakeException ;
47+ import org .apache .gravitino .metalake .MetalakeManager ;
4348import org .apache .gravitino .server .authorization .annotations .AuthorizationExpression ;
4449import org .apache .gravitino .server .authorization .annotations .AuthorizationRequest ;
4550import org .apache .gravitino .server .authorization .expression .AuthorizationExpressionEvaluator ;
@@ -131,37 +136,6 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
131136 AuthorizationExpression expressionAnnotation =
132137 method .getAnnotation (AuthorizationExpression .class );
133138
134- // Check current user exists in metalake before authorization
135- if (expressionAnnotation != null ) {
136- Object [] args = methodInvocation .getArguments ();
137- Map <Entity .EntityType , NameIdentifier > metadataContext =
138- extractNameIdentifierFromParameters (parameters , args );
139-
140- // Check if current user exists in the metalake.
141- NameIdentifier metalakeIdent = metadataContext .get (Entity .EntityType .METALAKE );
142-
143- if (metalakeIdent != null ) {
144- String currentUser = PrincipalUtils .getCurrentUserName ();
145- try {
146- AuthorizationUtils .checkCurrentUser (metalakeIdent .name (), currentUser );
147- } catch (org .apache .gravitino .exceptions .ForbiddenException ex ) {
148- LOG .warn (
149- "User validation failed - User: {}, Metalake: {}, Reason: {}" ,
150- currentUser ,
151- metalakeIdent .name (),
152- ex .getMessage ());
153- return Utils .forbidden (ex .getMessage (), ex );
154- } catch (Exception ex ) {
155- LOG .error (
156- "Unexpected error during user validation - User: {}, Metalake: {}" ,
157- currentUser ,
158- metalakeIdent .name (),
159- ex );
160- return Utils .internalError ("Failed to validate user" , ex );
161- }
162- }
163- }
164-
165139 try {
166140 AuthorizationExecutor executor ;
167141 if (expressionAnnotation != null ) {
@@ -172,22 +146,57 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
172146 extractNameIdentifierFromParameters (parameters , args );
173147
174148 Map <String , Object > pathParams = Utils .extractPathParamsFromParameters (parameters , args );
175- AuthorizationExpressionEvaluator authorizationExpressionEvaluator =
176- new AuthorizationExpressionEvaluator (expression );
177- AuthorizationRequest .RequestType requestType =
178- extractAuthorizationRequestTypeFromParameters (parameters );
179- executor =
180- AuthorizeExecutorFactory .create (
181- requestType ,
182- metadataContext ,
183- authorizationExpressionEvaluator ,
184- pathParams ,
185- entityType ,
186- parameters ,
187- args );
188- boolean authorizeResult = executor .execute ();
189- if (!authorizeResult ) {
190- return buildNoAuthResponse (expressionAnnotation , metadataContext , method , expression );
149+
150+ // Check metalake and user existence before authorization
151+ NameIdentifier metalakeIdent = metadataContext .get (Entity .EntityType .METALAKE );
152+ if (metalakeIdent != null ) {
153+ try {
154+ MetalakeManager .checkMetalake (
155+ metalakeIdent , GravitinoEnv .getInstance ().entityStore ());
156+ } catch (NoSuchMetalakeException | MetalakeNotInUseException ex ) {
157+ // If metalake doesn't exist or is not in use, return no auth response
158+ return buildNoAuthResponse (expressionAnnotation , metadataContext , method , expression );
159+ }
160+
161+ String currentUser = PrincipalUtils .getCurrentUserName ();
162+ try {
163+ AuthorizationUtils .checkCurrentUser (metalakeIdent .name (), currentUser );
164+ } catch (ForbiddenException ex ) {
165+ LOG .warn (
166+ "User validation failed - User: {}, Metalake: {}, Reason: {}" ,
167+ currentUser ,
168+ metalakeIdent .name (),
169+ ex .getMessage ());
170+ return Utils .forbidden (ex .getMessage (), ex );
171+ } catch (Exception ex ) {
172+ LOG .error (
173+ "Unexpected error during user validation - User: {}, Metalake: {}" ,
174+ currentUser ,
175+ metalakeIdent .name (),
176+ ex );
177+ return Utils .internalError ("Failed to validate user" , ex );
178+ }
179+ }
180+
181+ // If expression is empty, skip authorization check (method handles its own filtering)
182+ if (StringUtils .isNotBlank (expression )) {
183+ AuthorizationExpressionEvaluator authorizationExpressionEvaluator =
184+ new AuthorizationExpressionEvaluator (expression );
185+ AuthorizationRequest .RequestType requestType =
186+ extractAuthorizationRequestTypeFromParameters (parameters );
187+ executor =
188+ AuthorizeExecutorFactory .create (
189+ requestType ,
190+ metadataContext ,
191+ authorizationExpressionEvaluator ,
192+ pathParams ,
193+ entityType ,
194+ parameters ,
195+ args );
196+ boolean authorizeResult = executor .execute ();
197+ if (!authorizeResult ) {
198+ return buildNoAuthResponse (expressionAnnotation , metadataContext , method , expression );
199+ }
191200 }
192201 }
193202 return methodInvocation .proceed ();
0 commit comments