@serenity-is/corelib / Authorization
Namespace: Authorization
Contains permission related functions.
Note
We use a namespace here both for compatibility and for allowing users to override these functions easily in ES modules environment, which is normally hard to do.
Table of contents
Variables
Functions
Variables
isLoggedIn
• isLoggedIn: boolean
Checks if the current user is logged in. Prefer isLoggedInAsync as this one might block the UI if the UserData
is not already loaded.
Example
if (Authorization.isLoggedIn) {
// do something
}
Defined in
isLoggedInAsync
• isLoggedInAsync: Promise<boolean>
Checks if the current user is logged in.
Example
if (await Authorization.isLoggedInAsync) {
// do something
}
Defined in
userDefinition
• userDefinition: UserDefinition
Returns the user data for currently logged user. Prefer userDefinitionAsync as this one might block the UI if the UserData
is not already loaded.
Example
if (Authorization.userDefinition.IsAdmin) {
// do something
}
Defined in
userDefinitionAsync
• userDefinitionAsync: Promise<UserDefinition>
Returns the user data for currently logged user.
Example
if ((await Authorization.userDefinitionAsync).IsAdmin) {
// do something
}
Defined in
username
• username: string
Returns the username for currently logged user. Prefer usernameAsync as this one might block the UI if the UserData
is not already loaded.
Example
if (Authorization.username) {
// do something
}
Defined in
usernameAsync
• usernameAsync: Promise<string>
Returns the username for currently logged user.
Example
if (await Authorization.usernameAsync) {
// do something
}
Defined in
Functions
hasPermission
▸ hasPermission(permission): boolean
Checks if the current user has the permission specified. This should only be used for UI purposes and it is strongly recommended to check permissions server side.
Please prefer the
hasPermissionAsyncvariant as this may block the UI thread if theUserDatascript is not already loaded.
Parameters
| Name | Type | Description |
|---|---|---|
permission |
string |
Permission key. It may contain logical operators like A&B|C. |
Returns
boolean
false for "null or undefined", true for "*", IsLoggedIn for "?". For other permissions,
if the user has the permission or if the user has the IsAdmin flag (super admin) true, otherwise false.
Defined in
hasPermissionAsync
▸ hasPermissionAsync(permission): Promise<boolean>
Checks if the current user has the permission specified. This should only be used for UI purposes and it is strongly recommended to check permissions server side.
Parameters
| Name | Type | Description |
|---|---|---|
permission |
string |
Permission key. It may contain logical operators like A&B|C. |
Returns
Promise<boolean>
false for "null or undefined", true for "*", IsLoggedIn for "?". For other permissions,
if the user has the permission or if the user has the IsAdmin flag (super admin) true, otherwise false.
Defined in
isPermissionInSet
▸ isPermissionInSet(permissionSet, permission): boolean
Checks if the hashset contains the specified permission, also handling logical "|" and "&" operators
Parameters
| Name | Type | Description |
|---|---|---|
permissionSet |
Object |
Set of permissions |
permission |
string |
Permission key or a permission expression containing & | operators |
Returns
boolean
true if set contains permission
Defined in
validatePermission
▸ validatePermission(permission): void
Throws an error if the current user does not have the specified permission.
Prefer await validatePermissionAsync() as this one might block the UI if the UserData
is not already loaded.
Parameters
| Name | Type | Description |
|---|---|---|
permission |
string |
Permission key. It may contain logical operators like A&B|C. |
Returns
void
Defined in
validatePermissionAsync
▸ validatePermissionAsync(permission): Promise<void>
Throws an error if the current user does not have the specified permission.
Example
await Authorization.validatePermissionAsync("A&B|C");
Parameters
| Name | Type | Description |
|---|---|---|
permission |
string |
Permission key. It may contain logical operators like A&B|C. |
Returns
Promise<void>