How to Determine if the User is a Member of a Security Group

Description

The context.security object has numerous methods for determining if a user is a member of a specific group (a.k.a. "role").

Discussion

If the security system has been enabled in an application, the context.security object can be used to determine the groups, or "roles", to which a user belongs.

"Role" and "Group" both refer to Security Groups in Alpha Anywhere. The Alpha Anywhere IDE generally uses the term "Group" while the context.security properties and methods use the term "Role". Several methods exist for testing if a user is a member of a specific role (group).

For example, the Xbasic below checks to see if the user "[email protected]" is a member of the "Sales" group:

dim inSales as L
inSales = context.security.isUserInRole("[email protected]","Sales")

' Verify the operation succeeded
if (.not. context.security.callResult.Success) then
    ' An error occurred
    error_generate(context.security.callResult.text)
end if

if (inSales) then
    ' User is in the group "Sales"
else
    ' User is not in the group "Sales"
end if

This example uses the context.security.isUserInRole() method to test if the user is a member of the "Sales" group.

Other methods available for testing a user's role membership include:

context.security.isCurrentUserInRole()

Test if the currently logged in user is a member of a group

context.security.isCurrentUserInRoles()

Test if the currently logged in user is a member of at least one of a set of groups

context.security.isUserInRoles()

Test if a user is a member of at least one of a set of groups

In addition to testing for membership, methods exist for adding a user to a group, removing a user from a group, and listing a user's groups. See context.security for more information.

You should always check the value of context.security.callResult before using the value from any property or method of the context.security object.

See Also