@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

src/q/authorization.ts:151


isLoggedInAsync

isLoggedInAsync: Promise<boolean>

Checks if the current user is logged in.

Example

if (await Authorization.isLoggedInAsync) {
    // do something
}

Defined in

src/q/authorization.ts:161


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

src/q/authorization.ts:190


userDefinitionAsync

userDefinitionAsync: Promise<UserDefinition>

Returns the user data for currently logged user.

Example

if ((await Authorization.userDefinitionAsync).IsAdmin) {
    // do something
}

Defined in

src/q/authorization.ts:199


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

src/q/authorization.ts:171


usernameAsync

usernameAsync: Promise<string>

Returns the username for currently logged user.

Example

if (await Authorization.usernameAsync) {
    // do something
}

Defined in

src/q/authorization.ts:180

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 hasPermissionAsync variant as this may block the UI thread if the UserData script 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

src/q/authorization.ts:25


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

src/q/authorization.ts:56


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

src/q/authorization.ts:82


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

src/q/authorization.ts:120


validatePermissionAsync

validatePermissionAsync(permission): Promise<void>

Throws an error if the current user does not have the specified permission.

Parameters

Name Type Description
permission string Permission key. It may contain logical operators like A&B|C.

Returns

Promise<void>

Example

await Authorization.validatePermissionAsync("A&B|C");

Defined in

src/q/authorization.ts:133