Get role from logged in user?

Hi!

I want to get the active users role. I´ve searched a lot now and can´t seem to find a function for it like UserInfo.

My "sollution" as for now is to roll through the USERS.RDB and get it from there, but there must be a better way?

/Anders

Current "sollution": (Pasting it here removes the indents. Oh well..)

FUNCTION SFA()

STRING sROLE
STRING sUserRDB
STRING sUserIn = UserInfo(1)
INT hRdb
INT nRecord

hRdb = RdbOpen("_USERS")
IF (hRdb <> -1) THEN
nRecord = RdbFirstRec(hRdb)
WHILE (nRecord <> -1)DO
sUserRDB = RdbGet(hRdb, "NAME")
IF sUserRDB = sUserIn THEN
sROLE = RdbGet(hRdb, "ROLES")
nRecord = -1
ELSE
nRecord = RdbNextRec(hRdb)
END

END
RdbClose(hRdb)
END

END

  • I have requested a built-in function to provide that information, but it's still not a feature. Feel free to request it on the Citect ideas portal.

    What you use should work. There are 3 limitations that may or may not apply to you. 1: The code will take a short amount of time to run so you can't call it from foreground tasks like expressions on graphic objects. 2: Users can have more than one role assigned--that's why the field name is Roles. 3: If you use windows users, then your code won't work.

    To avoid these issues, I have used the following code. You just have to set each Role's Entry Command and Exit Command to call my function. It keeps track of what roles are in effect for the currently logged-in user. Then there is a function you can call to check if the current user has a specific role.

    I noticed the code was customized for a specific customer to open a home page based on the role of the user and when the log out to load a overview page in case they were on a restricted page. I disabled those lines of code. It also has some ErrLog() commands to write debug messages to the Kernel window and syslog.dat so you can see what is happening. Those can be disabled also.

    //Security.ci	User role tracking functions -- See Q6017
    
    INT		mnMaxRoles = 16;
    STRING	msCurrentRoles[16];
    STRING	msCurrentUser = "";
    INT		mnCurrentRole = 0;
    
    
    //Add the specified role to the current user's roles list. This is called by the Role's Entry Command.
    //Call RoleSet("") from the Role's Exit command to clear the list of roles.
    FUNCTION
    RoleSet(STRING sRole = "")
    
    	STRING sUserName = Name();
    	
    	EnterCriticalSection("RoleSet");
    
    	IF StrLength(sRole) > 0 THEN
    		
    		IF msCurrentUser <> sUserName THEN
    			msCurrentUser = sUserName;
    			mnCurrentRole = 0;
    		END
    	
    		IF mnCurrentRole < mnMaxRoles THEN
    			msCurrentRoles[mnCurrentRole] = sRole;
    			ErrLog("RoleSet('" + sRole + "') added role " + mnCurrentRole:#);
    			mnCurrentRole = mnCurrentRole + 1;
    !			UPA_PageDisplay(sRole + "_Overview");
    		ELSE
    			ErrLog("RoleSet() Too many roles to store");
    		END
    	ELSE
    		ErrLog("RoleSet() clearing roles");
    	
    		mnCurrentRole = 0;
    		msCurrentUser = "";
    		WinSelect(0);
    !		UPA_PageDisplay("Menu");
    	END
    		
    	LeaveCriticalSection("RoleSet");	
    END
    			
    
    //Check if the current user has the specified role
    //
    //Returns TRUE if the user has the role or the role was blank, otherwise FALSE.
    //
    INT
    FUNCTION
    UserHasRole(STRING sRole)
    	
        INT		nRole = 0;
        STRING	sUser = Name();
    
    	//If no role was specified, allow access
    	IF StrLength(sRole) = 0 THEN
    		RETURN TRUE;
    	END
    	
        FOR nRole = 0 TO mnMaxRoles - 1 DO
        	IF msCurrentRoles[nRole] = sRole THEN            
        		RETURN TRUE;
        	END
        END            
        
        RETURN FALSE;
    END

    Note that I switch the forum post editor to HTML mode (with the HTML button) and manually type < PRE >  < / PRE > tags (without the spaces) around my Cicode block so it will appear as a preformatted text block and the indents are not removed.

  • Thanks!

    I'll head on and request it as well, maybe they´ll look in to it if we request it a lot. :)

    Yes, my code has a few limitations, but as a quick fix and for my current application it should work. And since there´s no built in function for it´ll have to do I guess.
    I´ll take your code in consideration though.

    Also, thanks for the tip on pasting code. Maybe they could put code-tags to this forum like other programming forum has?
  • I was told the forum software was being upgraded when the Aveva support website software was upgraded a couple weeks ago. Unfortunately, the forum software upgrade got delayed. Hopefully we'll see it in the future.
  • You could use RdbFind to speed up your search function:

    nRecord = RdbPosRec(hRdb, -1);
    nRecord = RdbFind(hRdb, "NAME", sUserIn);
    IF nRecord <> -1 THEN
    sROLE = RdbGet(hRdb, "ROLES");
    END
    RdbClose(hRdb);

    ...etc.