cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Scripting question: How do I get the list of groups that a user belongs to?

smccoy
12-Amethyst

Scripting question: How do I get the list of groups that a user belongs to?

Hi...

I have a script that executes the command: "aa user --groups myuserid" but I'm not able to extract all of the output. I get the first line (which is the userid), but none of the groups. Can someone provide and example for how to do this? I can't help but think that this has been solved several times already. Below is the function-snippet from our trigger that has the core logic.

I have a very similar function that performs the command "aa groups" that works just fine. So I'm at a loss as to why this doesn't work.

Kind regards,

-Sean

function GetGroupsForUser(theuser)

{

  var readResult = new Array();

  var cmd = new Packages.com.mks.api.Command("aa","users");

  cmd.addOption(new Packages.com.mks.api.Option("groups")); 

  cmd.addSelection(theuser);

  var response = api.executeCmdAs(theuser, cmd);

  if( response.getExitCode() == 0 )

  {                

     if (response.getWorkItemListSize() != 0 )

     {

          var iter = response.getWorkItems()

          while (iter.hasNext())

          {

               var workitem = iter.next()

               readResult.push(workitem);

          }

     }

  }

  return readResult;

}

ACCEPTED SOLUTION

Accepted Solutions

Hello Sean,

What happens if you run the same command with the same arguments through the mksapiviewer?  Once I ran it through that, I realized you needed to process the fields.

I think you need to add a block to get a java.util.Iterator from each workitem with a workitem.getFields() call, iterating through the fields to get the data types, and if necessary (as in this case) iterating through the datatype(s).

Here's my excessively commented version of the loop inside the exit code check:

exitCode = response.getExitCode();
            System.out.println("Exit Code=" + exitCode);
           
            if( 0 == exitCode ){
                if( response.getWorkItemListSize() != 0 ){
                    WorkItemIterator workitemIterator;
                    workitemIterator = response.getWorkItems();
                   
                    // Set up variables for getting work items from workitemIterator.
                    WorkItem workitem;
                    workitem = null;
                   
                    // Set up variables for workitem.getFields
                    Field field;
                    int fieldSize;
                    java.util.Iterator fieldIterator;

                   
                    while (workitemIterator.hasNext()) {
                        workitem = workitemIterator.next();

                        // Try getting fields from work item
                        System.out.println("workitem: " + workitem.toString());
                        fieldSize = workitem.getFieldListSize();
                        System.out.println("  Field Size=" + fieldSize);
                        if( 0 <= fieldSize ){                           
                            fieldIterator = workitem.getFields();
                           
                            while(fieldIterator.hasNext()){
                                field = (Field) fieldIterator.next();
                                System.out.println("    field: " + field);
                                System.out.println("        " + field.getDataType());
                                List itemList = field.getList();
                                Iterator ilIterator = itemList.iterator();
                                while(ilIterator.hasNext()){
                                    System.out.println("            "+ ilIterator.next());
                                } //
                            } // while (fieldIterator.hasNext()
                        } else {// if( 0 < fieldSize )
                            System.out.println("No work items");
                        } // if( 0 < fieldSize ) else

                    } //while (workitemIterator.hasNext ()
                } // if( res.getWorkItemListSize() != 0 )
            } // if( 0 == exitCode )

It even worked once I realized I'd I hadn't copied the setup code accurately into my existing template.

I hope that helps. 

Regards,
Kael


Kind Regards,
Kael Lizak

Senior Technical Support Engineer
PTC Integrity Lifecycle Manager

View solution in original post

2 REPLIES 2

Hello Sean,

What happens if you run the same command with the same arguments through the mksapiviewer?  Once I ran it through that, I realized you needed to process the fields.

I think you need to add a block to get a java.util.Iterator from each workitem with a workitem.getFields() call, iterating through the fields to get the data types, and if necessary (as in this case) iterating through the datatype(s).

Here's my excessively commented version of the loop inside the exit code check:

exitCode = response.getExitCode();
            System.out.println("Exit Code=" + exitCode);
           
            if( 0 == exitCode ){
                if( response.getWorkItemListSize() != 0 ){
                    WorkItemIterator workitemIterator;
                    workitemIterator = response.getWorkItems();
                   
                    // Set up variables for getting work items from workitemIterator.
                    WorkItem workitem;
                    workitem = null;
                   
                    // Set up variables for workitem.getFields
                    Field field;
                    int fieldSize;
                    java.util.Iterator fieldIterator;

                   
                    while (workitemIterator.hasNext()) {
                        workitem = workitemIterator.next();

                        // Try getting fields from work item
                        System.out.println("workitem: " + workitem.toString());
                        fieldSize = workitem.getFieldListSize();
                        System.out.println("  Field Size=" + fieldSize);
                        if( 0 <= fieldSize ){                           
                            fieldIterator = workitem.getFields();
                           
                            while(fieldIterator.hasNext()){
                                field = (Field) fieldIterator.next();
                                System.out.println("    field: " + field);
                                System.out.println("        " + field.getDataType());
                                List itemList = field.getList();
                                Iterator ilIterator = itemList.iterator();
                                while(ilIterator.hasNext()){
                                    System.out.println("            "+ ilIterator.next());
                                } //
                            } // while (fieldIterator.hasNext()
                        } else {// if( 0 < fieldSize )
                            System.out.println("No work items");
                        } // if( 0 < fieldSize ) else

                    } //while (workitemIterator.hasNext ()
                } // if( res.getWorkItemListSize() != 0 )
            } // if( 0 == exitCode )

It even worked once I realized I'd I hadn't copied the setup code accurately into my existing template.

I hope that helps. 

Regards,
Kael


Kind Regards,
Kael Lizak

Senior Technical Support Engineer
PTC Integrity Lifecycle Manager
smccoy
12-Amethyst
(To:KaelLizak)

That works perfectly, Kael. You're the rockstar, yet again.

Thank you.

Announcements


Top Tags