Get User Group

Hi

I want to check if the current user is an administrator. Have looked into the security functions but getUsergroup fx will i believe create an array for all the groups for that user.

All my users only belong to a single group. Is there an easier way of creating a variable for that users group name?

Hi Rodney,

rodneysieb:
All my users only belong to a single group. Is there an easier way of creating a variable for that users group name?

If your users only belong to one group then ‘getUserGroups’ will return only a single element array which you can then check is ‘Administrator’ and branch accordingly

So, if you have the userID for the logged in user then can you not get what you want using that route ?

Cheers
Harry

Hi Harry

I can use the following sample servoy code fine and watch the output in the debugger:

//get all the users in the security settings (Returns a JSDataset)
var o = security.getUsers()

//loop through each user to get their group
//The getValue call is (row,column) where column 1 == id and 2 == name
for(var i=1 ; i<=o.getMaxRowIndex() ; i++)
{
//print to the output debugger tab: "user: " and the username
application.output(“user:” + o.getValue(i,2));

//set p to the user group for the current user
var p = security.getUserGroups(o.getValue(i,1));

for(k=1;k<=p.getMaxRowIndex();k++)
{
//print to the output debugger tab: “group” and the group(s)
//the user belongs to
application.output("group: " + p.getValue(k,2));
}
}

What i want is to load the user group (eg administrator) into a variable directly that i can watch and use in conditionals statements. Using the following:

var userName = security.getUserName();
var userId = security.getUserId(userName); // gets the id of the given username
var userGroup = security.getUserGroups(userId);

gives me the var userGroup as an array with 1 element but how do i get the actual group name into the variable.

Hi Rodney,

Sorry, I misinterpreted were you were here :oops:

The data you get back from getUserGroups is a dataset and so you have to turn it into an array in order to get the element

So your code would change to :

for(k=1;k<=p.getMaxRowIndex();k++) 
{ 
var dataArray = groups.getColumnAsArray(2) // this column contains the group name
var got_it = dataArray[0] ; // remember that first element is always zero
}

So ‘got_it’ would be your variable containing the Administrator group name and you can use the variable to branch

Hope this helps
Cheers
Harry

rodneysieb:

var userName = security.getUserName();
var userId = security.getUserId(userName); // gets the id of the given username
var userGroup = security.getUserGroups(userId);

gives me the var userGroup as an array with 1 element but how do i get the actual group name into the variable.

getUserGroups returns a JSDataSet so, if you apply a getColumnAsArray to it, you should obtain an array of values. In your case if you do:

var userName = security.getUserName();
var userId = security.getUserUID(userName); 
var userGroup = security.getUserGroups(userId);
var group_array = userGroup.getColumnAsArray(2);

you will obtain an array with the group names of the current logged user in it.

You can also reduce to a single line of code

var group_array = security.getUserGroups(security.getUserUID()).getColumnAsArray(2);

Hope it helps, ciao

Hi

Got it!

Thanks guys. Works well with the limitation of you having to make sure that a user belongs only to 1 Group.

rodneysieb:
Hi

Got it!

Thanks guys. Works well with the limitation of you having to make sure that a user belongs only to 1 Group.

I do not follow you, which limitation?

Hi

If the user belongs to a number of groups there will be a number of elements in the array.

It would be harder to isolate a particular group and use in a conditional statement, or is there another trick?

that should be easy, just loop in the elements of the array and see the desider value is there. Somethink like this maybe:

for (var i in group_array) if (group_array == myDesiderValue) return true; return false;