Check for User Role

Your rating: None Average: 4 (2 votes)

A very handy PeopleCode function to check for a user role(s) and perform specific actions depending on the value returned.

Function RoleToMatch(&RoleToMatch) Returns boolean;
   
   &bFound = False;
/*Returns an array containing all roles associated to the current user*/
   &aRoles = %Roles;
/*Loop through the array*/
   For &I = 1 To &aRoles.Len
      &RoleName = &aRoles.Shift();
      If Upper(LTrim(RTrim(&RoleName))) = Upper(LTrim(RTrim(&RoleToMatch))) Then
         &bFound = True;
         Break;
      End-If;
   End-For;
   
   Return &bFound;
   
End-Function;

...and here is how to call the function
/*Note: Don't forget to declare your function before you call it*/
/* Declare variables
&aRoles is an array of string that will store roles from %roles */

Local array of string &aRoles;
Local number &I;
Local string &RoleName;
Local boolean &bFound;
/*Function Call*/
/*The role that I would like to validate if a user has is "Administrator"*/
If RoleToMatch("Administrator") Then
   /*What you want to do goes here*/
End-If;

Please try to help out with unanswered topics on the forum. Chances are you have had the same issue/question some time in your IT career!

Comments

Have a question? Please ask it on the forum instead.
Bart Spaey's picture
Bart Spaey (not verified)
IsUserInRole

There also exists a PeopleCode function "IsUserInRole" (and also "IsUserInPermissionList").

CompShack's picture
User offline. Last seen 41 weeks 6 days ago. Offline
Joined: 12/09/2007
Posts: 167
Great alternative!

Bart, thanks for pointing out the functions. I guess this will shrink the above code to 1 line! And to follow up on your comment here is the syntax and little explanation for each of the functions.
IsUserInRole

/*Syntax*/
IsUserInRole(rolename1 [, rolename2]. . .)

Returns True if the current user belongs to one or more role specified in the role array, otherwise it returns false.
IsUserInPermissionList
/*Syntax*/
IsUserInPermissionList(PermissionList1 [, PermissionList2]. . .)

Returns True if the current user has access to one or more permission list specified in the passed array, otherwise it returns false.

Thanks again Bart!

n/a
Priyanka's picture
Priyanka (not verified)
Re: Check for User Role

Yes, I agree tht IsUserInRole is a very handy function.

I've used it as given below:

If (&PrevAct = "LOA" And

ACTION <> "RFL") And

Not (IsUserInRole("MBT_JOB_SUPERUSER_VALIDATION")) Then

Error (MsgGetText(20000, 533, ""));

End-If;

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
The question below is to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.