Skip to content

Commit

Permalink
Add @permissionchecker annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Oct 4, 2024
1 parent 8e9154b commit 070b54b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/main/java/io/quarkus/security/PermissionChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.security;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Used to annotate CDI bean methods that check if a user holds a permission specified by the {@link #value()}.
* Quarkus Security will augment every {@link io.quarkus.security.identity.SecurityIdentity} with this permission checker.
* Such a permission checker grants access to methods secured with the {@link PermissionsAllowed} security annotation.
* Following example shows how it works:
* <pre>
* {@code
* &#64;Path("hello")
* public class HelloResource {
*
* &#64;PermissionsAllowed("speak")
* &#64;GET
* public String sayHello() {
* return "Hello World!";
* }
*
* &#64;PermissionChecker("speak")
* public boolean canSpeak(SecurityIdentity identity) {
* return "speaker".equals(identity.getPrincipal().getName());
* }
* }
* }
* </pre>
* The permission checker methods can include any of secured method parameters (matched by name).
* Consider the following secured method:
* <pre>
* {@code
* &#64;PermissionsAllowed("update")
* public String updateString(String a, String b, String c, String d) {
* ...
* }
* }
* </pre>
* The permission checker that grants access to the {@code updateString} method can inject
* any arguments it requires and optionally even {@link io.quarkus.security.identity.SecurityIdentity}:
* <pre>
* {@code
* &#64;PermissionChecker("update")
* public boolean canUpdate(String c, String a, SecurityIdentity identity) {
* ...
* }
* }
* </pre>
* The permission checker method parameters are matched with the secured method parameters in exactly same fashion
* as are constructor parameters of a custom permission. Please see {@link PermissionsAllowed#params()} for more information.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionChecker {

/**
* Specifies a permission this checker grants.
*
* @see PermissionsAllowed#value()
* @return name of the permission this checker grants
*/
String value();

}

0 comments on commit 070b54b

Please sign in to comment.