TestStand Users and Groups C# API

Wednesday, December 30, 2015

               

               

        /*

            These instructions from the NI forum post were

                key in putting the pieces together


            You can get the current user from Engine.CurrentUser.
            You can get the list of groups by Engine.UsersFile.UserGroupList
            Each array item in the list of groups is a group (in the form of a user object)
            You can get the list of users from a group by it's User.Members property.
       

            ------------------

 

            You cannot of course run this code directly. This example comes from a custom

            UI application. The first line gets the engine instance from the

            ApplicationMgr and needs to be defined and placed in the right scope(s)

            for your situation. All the pieces are here so if you study this carefully

            it should provide the detail you need to get group names and members in those

            groups.

 

            broxsoft@outlook.com

        */

 

        _eng = axApplicationMgr.GetEngine();

 

        private void WriteToBox2(string msg)
        {
            textBox1.Text += 
string.Format("[{0}]{1}{1}", msg, Environment.NewLine);
        }

 

        private void WriteToBox(string msg)
        {
            textBox1.Text += 
string.Format("[{0}]{1}", msg, Environment.NewLine);
        }


        
private void button5_Click(object sender, EventArgs e)
        {
 
            WriteToBox2(
@"_eng.CurrentUser.LoginName: " + _eng.CurrentUser.LoginName);

            //PROVIDES LIST OF GROUPS
            
PropertyObject groupListObject = _eng.UsersFile.UserGroupList;
 
            
int countOfGroups = groupListObject.GetNumElements();
 
            
for (int i = 0; i < countOfGroupsi++)
            {
               
//ITERATE THROUGH EACH GROUP
                
string groupName = groupListObject.GetPropertyObjectByOffset(i, 0).Name;
 
                WriteToBox(
string.Format("Group number {0} is {1}", i, groupName));
 
                
PropertyObject currentGroupObject = groupListObject.GetPropertyObjectByOffset(i, 0);
 
                
//CAST GROUP AS USER OBJECT
                
User groupAsUserObject = (User)currentGroupObject;
 
                
int countOfGroupMembers = groupAsUserObject.Members.GetNumElements();
 
                
PropertyObject membersObject=groupAsUserObject.Members;
 
                
for (int n = 0; n < countOfGroupMembers; n++)
                {
                   
//SHOW USERS IN THIS GROUP

                   string currentUser = membersObject.GetValStringByOffset(n, 0);
                   WriteToBox(
string.Format("user {0} is in the {1} group",currentUser,groupName));
                }
            }
        }

 

        /*

            Here is a basic method that returns a bool as to whether the login user is in the group you provide

            _eng is a class global of course…

 

            broxsoft@outlook.com

        */

 

 

        private bool LoginUserIsInGroup(string groupName)

        {

            bool retVal = false;

            string userName = _eng.CurrentUser.LoginName;

 

            //PROVIDES LIST OF GROUPS

            PropertyObject groupListObject = _eng.UsersFile.UserGroupList;

 

            int countOfGroups = groupListObject.GetNumElements();

 

            //LOOK AT ALL GROUPS

            for (int i = 0; i < countOfGroupsi++)

            {

                string groupNameFromEng = groupListObject.GetPropertyObjectByOffset(i, 0).Name;

 

                if (groupNameFromEng.ToLower() == groupName.ToLower())

                {

                    //FOUND MATCHING GROUP

 

                    PropertyObject currentGroupObject = groupListObject.GetPropertyObjectByOffset(i, 0);

 

                    //CAST GROUP AS USER OBJECT

                    User groupAsUserObject = (User)currentGroupObject;

 

                    int countOfGroupMembers = groupAsUserObject.Members.GetNumElements();

 

                    PropertyObject membersObject = groupAsUserObject.Members;

 

                    for (int n = 0; n < countOfGroupMembers; n++)

                    {

                        string currentUser = membersObject.GetValStringByOffset(n, 0);

                        if (currentUser.ToLower() == userName.ToLower()) retVal = true;                         

                    }

                }

            }

            return retVal;

        }