User
Current user API – application.user
An object that contains following properties:
- isLoggerIn (
bool
) – is true of the user is currently logged in - id (
string
) – id of the current logged in user (Id of the User entity) - firstName (
string
) – first name of the current user - lastName (
string
) – last name of the current user - userName (
string
) – user name of the current user - hasPermissionAsync (
function(permission: string, permissionedEntity?: IEntityReferenceDto) => Promise<boolean>
) – asynchronous function that check is the current user granted a specified permission. It allows to provide Permissioned Entity reference (id
and_className
) to checking for permissions scoped by Permissioned Entity - hasRoleAsync (
function(role: string) => Promise<boolean>
) – asynchronous function that check is the current user appointer to a specified role - personId (
string
) - personId of the current user
Example of the usage:
const onAfterDataLoad = async (): Promise<void> => {
const { user } = application;
if (user.isLoggedIn) {
console.log('User is logged in ');
console.log("Current user is: ", user);
console.log(`user id: ${user.id}`);
console.log(`userName: ${user.userName}`);
console.log(`firstName: ${user.firstName}`);
console.log(`lastName: ${user.lastName}`);
console.log(`person id: ${user.personId}`);
const adminRole = 'System Administrator';
console.log(`Check is user granted role '${adminRole}' ... `);
try {
const isGranted = await user.hasRoleAsync(adminRole);
console.log(`Check is user granted role "${adminRole}" - ${isGranted ? "✅" : "❌"}`);
}
catch (error) {
console.log("Failed to check role", error);
}
}
else {
console.log('User is NOT logged in');
}
};