-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from agorapulse/feature/result-requires-permis…
…sions Add annotation to check permission on result
- Loading branch information
Showing
9 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...naut-permissions/src/main/groovy/com/agorapulse/permissions/ResultRequiresPermission.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2023 Agorapulse. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.agorapulse.permissions; | ||
|
||
import io.micronaut.aop.Around; | ||
import io.micronaut.context.annotation.Type; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* You can use {@link GrantsPermission} annotation to declare the required permission checks for every method's argument | ||
* which has the {@link PermissionAdvisor} declared. At least one argument must have the {@link PermissionAdvisor} declared | ||
* and every check must pass. | ||
*/ | ||
@Around | ||
@Documented | ||
@Retention(RUNTIME) | ||
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) | ||
@Type(ResultRequiresPermissionInterceptor.class) | ||
public @interface ResultRequiresPermission { | ||
|
||
/** | ||
* @return the arbitrary permissions' definition, there is no semantics given by default as they are declared by the advisors | ||
*/ | ||
String value(); | ||
|
||
/** | ||
* @return <code>true</code> if the method should return <code>null</code> instead of throwing exception | ||
*/ | ||
boolean returnNull() default false; | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...sions/src/main/groovy/com/agorapulse/permissions/ResultRequiresPermissionInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2023 Agorapulse. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.agorapulse.permissions; | ||
|
||
import io.micronaut.aop.MethodInterceptor; | ||
import io.micronaut.aop.MethodInvocationContext; | ||
import io.micronaut.core.annotation.AnnotationValue; | ||
import io.micronaut.core.type.Argument; | ||
|
||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class ResultRequiresPermissionInterceptor implements MethodInterceptor<Object, Object> { | ||
|
||
public static final int POSITION = -299; | ||
|
||
private final PermissionChecker permissionChecker; | ||
|
||
public ResultRequiresPermissionInterceptor(PermissionChecker permissionChecker) { | ||
this.permissionChecker = permissionChecker; | ||
} | ||
|
||
@Override | ||
public Object intercept(MethodInvocationContext<Object, Object> context) { | ||
AnnotationValue<ResultRequiresPermission> annotation = context.findAnnotation(ResultRequiresPermission.class) | ||
.orElseThrow(() -> new PermissionException(null, context.getTargetMethod(), "Method without @ResultRequiresPermission annotation!")); | ||
|
||
String permissionString = annotation.getRequiredValue(String.class); | ||
Object value = context.proceed(); | ||
|
||
if (value == null) { | ||
return null; | ||
} | ||
|
||
Argument<Object> argument = context.getReturnType().asArgument(); | ||
PermissionCheckResult result = permissionChecker.checkPermission(permissionString, value, argument); | ||
|
||
boolean returnNull = annotation.get("returnNull", Boolean.class, Boolean.FALSE); | ||
|
||
if (returnNull) { | ||
switch (result) { | ||
case DENY: | ||
return null; | ||
case ALLOW: | ||
return value; | ||
default: | ||
throw new PermissionException(permissionString, context.getTargetMethod(), "Cannot determine if the user has the permissions to perform operation"); | ||
} | ||
} | ||
|
||
switch (result) { | ||
case DENY: | ||
throw new PermissionException(permissionString, value, "The user does not have a permissions to perform operation"); | ||
case ALLOW: | ||
return value; | ||
default: | ||
throw new PermissionException(permissionString, context.getTargetMethod(), "Cannot determine if the user has the permissions to perform operation"); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return POSITION; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...ssions/src/test/resources/com/agorapulse/permissions/PostControllerSpec/existingPost.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"id": 1, | ||
"status": "DRAFT", | ||
"authorId": 1, | ||
"message": "Hello" | ||
} |
3 changes: 3 additions & 0 deletions
3
...missions/src/test/resources/com/agorapulse/permissions/PostControllerSpec/viewFailed.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"message": "The user does not have a permissions to perform operation" | ||
} |