Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
Need to check whether logged in user is in Administrators group or not using Thingworx service.
As I checked there is no specific service related to this or list of users in Administrators group can also help.
Please let me know if I can get this Info using any inbuilt thingworx services.
Solved! Go to Solution.
You can use below custom service to get the list of specific Group Members:
Input: UserGroup //string--"A valid UserGroup name",like Administrators
Output: Group_Member //InfoTable with fiels name, description and type
//Declare our recursion depth cap to ensure that the loop will exit at some point incase an error in ThingWorx allows
//for a circular group reference
var recursionLevel = 0;
var recursionLevelCap = 20;
//Determine if the UserGroup variable is actually a user group
if(!Groups[UserGroup])
{
logger.error("Service call " + me.name + "::GetAllUsersInGroup() was supplied invalid UserGroup name, service commencing exit.");
}
else //If our UserGroup input is valid, continue with service
{
//Recursive function call
var groupMembers = buildMemberInfotable(0, UserGroup);
// Return all group members after removing duplicates and sorting by name
//Build params to remove duplicate entries
var params = {
t: groupMembers /* INFOTABLE */,
columns: "name,description,type" /* STRING */
};
//Remove duplicate entries
var result = Resources["InfoTableFunctions"].Distinct(params);
//Build sort params
var params = {
sortColumn: "name" /* STRING */,
t: result /* INFOTABLE */,
ascending: true /* BOOLEAN */
};
//Sort by user name
var result = Resources["InfoTableFunctions"].Sort(params);
}
//Recursive function to find all group members in a User Group structure
function buildMemberInfotable( recursionLevel, UserGroup )
{
var params = {
infoTableName : "InfoTable",
dataShapeName : "GroupMember"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(GroupMember)
var finalMemberReturnTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
//Only proceed if we haven't hit the recursion depth level cap
if(recursionLevel < recursionLevelCap)
{
//Increment the recursionLevel for the next pass
recursionLevel++;
// result: INFOTABLE dataShape: GroupMember
var groupAndMemberInfotable = Groups[UserGroup].GetGroupMembers();
var tableLength = groupAndMemberInfotable.rows.length;
//Loop through the members found, if they are a user, add them to the return table
//If not, use recursion and go deeper
for(var x = 0; x < tableLength; x++)
{
var row = groupAndMemberInfotable.rows[x];
if(row.type == "User")
{
finalMemberReturnTable.AddRow(row);
}
else
{
var recursiveTable = buildMemberInfotable( recursionLevel, row.name );
var params = {
t1: recursiveTable /* INFOTABLE */,
t2: finalMemberReturnTable /* INFOTABLE */
};
// result: INFOTABLE
finalMemberReturnTable = Resources["InfoTableFunctions"].Union(params);
}
}
//Return the members found at this level and deeper
return finalMemberReturnTable;
}
}
First of all, you have to get the logged-in user name, to do this you can use SecurityMonitor Thing which has a built-in event "LoginSucceeded". You can use this event to get the username of the logged-in user. Once you get the user name, you can use the below service to check if that user is a part of the administrator.
let result = Groups["Administrators"].GetGroupMember({
name: "pass the name of the logged in user" /* STRING */
});
If this user is a part of the Administrator, this service will return 1 row for that user, if not this will throw an error.
You can use below custom service to get the list of specific Group Members:
Input: UserGroup //string--"A valid UserGroup name",like Administrators
Output: Group_Member //InfoTable with fiels name, description and type
//Declare our recursion depth cap to ensure that the loop will exit at some point incase an error in ThingWorx allows
//for a circular group reference
var recursionLevel = 0;
var recursionLevelCap = 20;
//Determine if the UserGroup variable is actually a user group
if(!Groups[UserGroup])
{
logger.error("Service call " + me.name + "::GetAllUsersInGroup() was supplied invalid UserGroup name, service commencing exit.");
}
else //If our UserGroup input is valid, continue with service
{
//Recursive function call
var groupMembers = buildMemberInfotable(0, UserGroup);
// Return all group members after removing duplicates and sorting by name
//Build params to remove duplicate entries
var params = {
t: groupMembers /* INFOTABLE */,
columns: "name,description,type" /* STRING */
};
//Remove duplicate entries
var result = Resources["InfoTableFunctions"].Distinct(params);
//Build sort params
var params = {
sortColumn: "name" /* STRING */,
t: result /* INFOTABLE */,
ascending: true /* BOOLEAN */
};
//Sort by user name
var result = Resources["InfoTableFunctions"].Sort(params);
}
//Recursive function to find all group members in a User Group structure
function buildMemberInfotable( recursionLevel, UserGroup )
{
var params = {
infoTableName : "InfoTable",
dataShapeName : "GroupMember"
};
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(GroupMember)
var finalMemberReturnTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
//Only proceed if we haven't hit the recursion depth level cap
if(recursionLevel < recursionLevelCap)
{
//Increment the recursionLevel for the next pass
recursionLevel++;
// result: INFOTABLE dataShape: GroupMember
var groupAndMemberInfotable = Groups[UserGroup].GetGroupMembers();
var tableLength = groupAndMemberInfotable.rows.length;
//Loop through the members found, if they are a user, add them to the return table
//If not, use recursion and go deeper
for(var x = 0; x < tableLength; x++)
{
var row = groupAndMemberInfotable.rows[x];
if(row.type == "User")
{
finalMemberReturnTable.AddRow(row);
}
else
{
var recursiveTable = buildMemberInfotable( recursionLevel, row.name );
var params = {
t1: recursiveTable /* INFOTABLE */,
t2: finalMemberReturnTable /* INFOTABLE */
};
// result: INFOTABLE
finalMemberReturnTable = Resources["InfoTableFunctions"].Union(params);
}
}
//Return the members found at this level and deeper
return finalMemberReturnTable;
}
}