Search for objects

Description
sebastianmellmann
Posts: 4
Joined: Tue Oct 11, 2011 10:48 am

Search for objects

Postby sebastianmellmann » Tue Oct 11, 2011 10:54 am

Hi!

Right now I'm fetching a specific set of users with the following command

Code: Select all

#set($users = $trackerService.getDataService().searchInstances("User", "id:(userA OR userB OR userC)", "name"))


I've searched the API for other queries instead of "User".
Is there any way to get only the user which have a specific role?
I've tried "Role", but it doesn't seem to work.

Thanks for help.


Sebastian

NickEntin
Posts: 472
Joined: Tue Oct 24, 2006 10:27 am
Location: Polarion Software GmbH, Stuttgart
Contact:

Re: Search for objects

Postby NickEntin » Wed Oct 12, 2011 10:06 am

Hi Sebastian,

I believe you go to deeper API, than required.

If you need users, why don't you search for users directly, like
$trackerService.getProjectsService().searchUsers("id:(userA OR userB OR userC)")

Unfortunately there is no index implemented to search by roles, therefore you'd need to iterate found users.

You can use {users} macro, if you just need to display list of users in a role.

Best regards,
Nick

sebastianmellmann
Posts: 4
Joined: Tue Oct 11, 2011 10:48 am

Re: Search for objects

Postby sebastianmellmann » Wed Oct 12, 2011 10:53 am

Hi Nick!

Thanks for the fast reply!

Is there any chance to use something like:

$trackerService.getProjectsService().searchUsers("id:(userA OR userB OR userC OR (NOT userD))")

Background: I don't want to have some users in my list.


Regards,
Sebastian

keith.roseberry
Posts: 5
Joined: Wed Apr 02, 2008 7:10 pm
Location: Vermont, USA
Contact:

Re: Search for objects

Postby keith.roseberry » Sat Oct 29, 2011 12:56 pm

There are two API methods in the ISecurityService to retrieve a list of users that may be pertinent to your situation:

getUsersForContextRole(roleId, ctx) - Returns the ids of all users, which do have the given role in given context.
getUsersForGlobalRole(roleId) - Returns the ids of all users, which do have the given role.

I've used the getUsersForGlobalRole method frequently since we use global roles to group our nearly 700 users into product teams. Below is a wiki script that displays the users in the repository, their project role assignments for the selected project, and those users assigned to the selected global role highlighted in red.

Code: Select all

{pre}

## ----------
## Set some global data.
## ----------

#set($assignable_label = "_project_assignable")
#set($developer_label = "_project_developer")
#set($user_label = "_project_user")
#set($noGlobalRole = "None")

## ----------
## Retrieve a list of all projects in the repository, ordered by project name.
## ----------

#set($allProjects = $projectService.searchProjects("","name"))

## ----------
## Retrieve a list of all users in the repository.
## ----------

#set($allUsers = $projectService.getUsers())

## ----------
## Retrieve a list of global roles. The user can select a global role for highlighting.
## ----------

#set($globalRoles = $securityService.getGlobalRoles())
#set($globalRoles = $sortTool.sort($globalRoles))
#set($globalRoles = $globalRoles.list($globalRoles.enumeration($globalRoles)))
#set($void        = $globalRoles.add(0,$noGlobalRole))

## ----------
## Build options for a combo box with the list of projects in it.
##
##   1. Retrieve the selected project if one is available in the POST data
##      for the form. Otherwise, default to the first project in the list.
##   2. Iterate through the list of projects and build the option tags
##      for each project in a string.
##
##   The result is placed in the $projectOptions string.
## ----------

#if($allProjects.size() > 0)

  #set($selectedProject = "")

  #set($selectedProjectId = $allProjects.get(0).id)
  #set($selectedProjectId = $request.getParameter("project"))

  #set($projectOptions = "")

  #foreach($project in $allProjects)

    #set($selected = "")
    #if($project.id.equals($selectedProjectId))
      #set($selected = "selected='true'")
      #set($selectedProject = $project)
    #end

    #set($projectOption = "<option value='$project.id' $selected>$project.name</option>")
    #set($projectOptions = "$projectOptions$projectOption")

  #end ## foreach($project in $allProjects)

#end ## if($allProjects.size() > 0)

## ----------
## Build options for a combo box with the list of global roles in it.
##
## The result is placed in the $globalRolesOptions string.
## ----------

#if($globalRoles.size() > 0)

  #set($selectedGlobalRole = "")
  #set($selectedGlobalRole = $request.getParameter("globalrole"))

  #set($globalRoleOptions = "")

  #foreach($globalRole in $globalRoles)

    #set($selected = "")
    #if($globalRole.equals($selectedGlobalRole))
      #set($selected = "selected='true'")
    #end

    #set($globalRoleOption = "<option value='$globalRole' $selected>$globalRole</option>")
    #set($globalRoleOptions = "$globalRoleOptions$globalRoleOption")

  #end

#end

## ----------
## Retrieve the users in the selected global role.
## ----------

#set($globalRoleUsers = "")
#if(!$selectedGlobalRole.equals($noGlobalRole))
  #set($globalRoleUsers = $securityService.getUsersForGlobalRole($selectedGlobalRole))
#end

{/pre}

## ----------
## Display the Project/Users Form.
## ----------

<form id="project_form" action="" method="post">

  #set($submit = "document.getElementById('project_form').submit();")

  <div class="regiontitle">Filter Options</div>

  <table class="wiki-table">
    <tr>
      <td>
        Project: <select name="project" id="project" onChange="$submit">$projectOptions</select>
      </td>
      <td>
        Highlight Global Role: <select name="globalrole" id="globalrole" onChange="$submit">$globalRoleOptions</select>
      </td>
    </tr>
  </table>

  <div class="regiontitle">User Roles Assignments for $selectedProject.name</div>

  <table border="1" cellspacing="3" cellpadding="3">

    <tr>
      <th style="vertical-align:bottom;">\#</th>
      <th style="vertical-align:bottom;">User Name</th>
      <th style="vertical-align:bottom;" width="30px">P\\r\\o\\j\\e\\c\\t\\&nbsp;\\A\\s\\s\\i\\g\\n\\a\\b\\l\\e</th>
      <th style="vertical-align:bottom;" width="30px">P\\r\\o\\j\\e\\c\\t\\&nbsp;\\D\\e\\v\\e\\l\\o\\p\\e\\r</th>
      <th style="vertical-align:bottom;" width="30px">P\\r\\o\\j\\e\\c\\t\\&nbsp;\\U\\s\\e\\r</th>
    </tr>

    #foreach($user in $allUsers)

      #set($contextRoles = $securityService.getContextRolesForUser($user.id,$selectedProject.contextId))

      ## ----------
      ## If the current user is in the selected global role, highlight the row.
      ## ----------

      #set($highlight = "")
      #set($emphasis = "style=white-space:nowrap;font-weight:normal;")

      #if(!$selectedGlobalRole.equals($noGlobalRole))
        #foreach($globalRoleUser in $globalRoleUsers)
          #if($globalRoleUser.equals($user.id))
            #set($highlight = "style='background-color:red'")
            #set($emphasis = "style=white-space:nowrap;font-weight:bold;")
          #end
        #end
      #end

      <tr $highlight>

        <td>$velocityCount</td>
        <td $emphasis>$user.name</td>

        #set($assignable = "")
        #set($developer = "")
        #set($userRole = "")
     
        #foreach($role in $contextRoles)
          #if($role.equals("project_assignable")) #set($assignable="checked=checked") #end
          #if($role.equals("project_developer"))  #set($developer="checked=checked")  #end
          #if($role.equals("project_user"))       #set($userRole="checked=checked")   #end
        #end

        <td align="center"><input type="checkbox" name="$user.id$assignable_label" value="$user.id$assignable_label" $assignable /></td>
        <td align="center"><input type="checkbox" name="$user.id$developer_label"  value="$user.id$developer_label"  $developer /></td>
        <td align="center"><input type="checkbox" name="$user.id$user_label"       value="$user.id$user_label"       $userRole /></td>

      </tr>

    #end

  </table>

</form>

sebastianmellmann
Posts: 4
Joined: Tue Oct 11, 2011 10:48 am

Re: Search for objects

Postby sebastianmellmann » Tue Nov 01, 2011 10:56 am

Thanks for your support Keith!

Unfortunately I only have access to the project scope.
That means I don't have access to the Global Administration.
I can only administer roles in my project scope.
So using the method getUsersForGlobalRole doesn't work here.
I'm not sure how to use getUsersForContextRole. What context do I have to chose here?

Thanks for your help.

keith.roseberry
Posts: 5
Joined: Wed Apr 02, 2008 7:10 pm
Location: Vermont, USA
Contact:

Re: Search for objects

Postby keith.roseberry » Tue Nov 01, 2011 4:50 pm

The "getUsersForContextRole" works the same as "getUsersForGlobalRole", except it is limited to the specified project context. The project context can be retrieved from the project's TrackerProject, which can be retrieved from the TrackerService by supplying the project id. The following code snippet uses the "getUsersForContextRole" method to display the user ids of all users assigned the "project_admin" role for the current project.

Code: Select all

#set ($projectId = $doc.getProject($doc.space))
#set ($project = $trackerService.getTrackerProject($projectId))
#set ($contextId = $project.contextId)

#set ($admins = $securityService.getUsersForContextRole("project_admin", $contextId))

#foreach($admin in $admins)
  $admin \\
#end


Hope this helps!

- Keith

sebastianmellmann
Posts: 4
Joined: Tue Oct 11, 2011 10:48 am

Re: Search for objects

Postby sebastianmellmann » Wed Nov 02, 2011 7:24 am

Thats exactly what I'm looking for.
Thanks for pointing me in the right direction!

-Sebastian


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: Google [Bot] and 5 guests