Class PermissionChecker
java.lang.Object
com.github.alex1304.ultimategdbot.api.command.PermissionChecker
public final class PermissionChecker extends Object
Class used to check permissions for using a command.
-
Constructor Summary
Constructors Constructor Description PermissionChecker()
-
Method Summary
Modifier and Type Method Description reactor.core.publisher.Mono<Boolean>
isGranted(PermissionLevel level, Context ctx)
Checks whether the given permission level is granted in the specified context.reactor.core.publisher.Mono<Boolean>
isGranted(String name, Context ctx)
Checks whether the given permission is granted in the specified context.void
register(PermissionLevel level, Function<Context,reactor.core.publisher.Mono<Boolean>> checker)
Registers a new permission into this checker.void
register(String name, Function<Context,reactor.core.publisher.Mono<Boolean>> checker)
Registers a new permission into this checker.void
registerAll(PermissionChecker source)
Copies all permissions registered in the specified permission checker into this one.
-
Constructor Details
-
PermissionChecker
public PermissionChecker()
-
-
Method Details
-
register
Registers a new permission into this checker.- Parameters:
name
- a name that uniquely identifies this permissionchecker
- a function that takes a Context as input and asynchronously tells whether the permission is granted or not. An empty Mono is considered the same as false. This function is called when usingisGranted(String, Context)
- Throws:
IllegalArgumentException
- if a permission with this name already exists, or if the permission name is blank as perString.isBlank()
-
register
public void register(PermissionLevel level, Function<Context,reactor.core.publisher.Mono<Boolean>> checker)Registers a new permission into this checker.- Parameters:
level
- the level the permission should be attached tochecker
- a function that takes a Context as input and asynchronously tells whether the permission is granted or not. An empty Mono is considered the same as false. This function is called when usingisGranted(PermissionLevel, Context)
-
registerAll
Copies all permissions registered in the specified permission checker into this one.- Parameters:
source
- the source permission checker
-
isGranted
Checks whether the given permission is granted in the specified context. If the permission has not been registered into this checker, false will be emitted. If the given permission name is the empty string "", it will always emit true.- Parameters:
name
- the name of the permission to checkctx
- the context that applies for the check- Returns:
- a Mono that emits true if either the given permission is granted, false otherwise.
-
isGranted
Checks whether the given permission level is granted in the specified context. If no permission attached to this level is granted, it will check permissions attached to higher levels. If at least one permission is granted, true is emitted.- Parameters:
level
- the permission level to checkctx
- the context that applies for the check- Returns:
- a Mono that emits true if any permission attached to the specified level or a higher level is granted, false otherwise.
- Throws:
IllegalArgumentException
- if level is negative
-