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;
Bookmark/Search this post with:
Comments
There also exists a PeopleCode function "IsUserInRole" (and also "IsUserInPermissionList").
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
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
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!
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