]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8060 WS api/permissions/users searchs users users by name, login and email
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 1 Feb 2017 16:57:39 +0000 (17:57 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 2 Feb 2017 15:09:52 +0000 (16:09 +0100)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/UsersAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/UsersActionTest.java
sonar-db/src/main/java/org/sonar/db/permission/PermissionQuery.java
sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml
sonar-db/src/main/resources/org/sonar/db/permission/UserPermissionMapper.xml
sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java

index 9b1f28cd243124d0c626621eec7e8e389383284f..16924c5ac767d5a7dc4ed43209ea8eefcfeea164 100644 (file)
@@ -121,9 +121,7 @@ public class UsersAction implements PermissionsWsAction {
       .setPageIndex(request.mandatoryParamAsInt(Param.PAGE))
       .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
       .setSearchQuery(textQuery);
-    if (project.isPresent()) {
-      permissionQuery.setComponentUuid(project.get().getUuid());
-    }
+    project.ifPresent(projectId -> permissionQuery.setComponentUuid(projectId.getUuid()));
     if (permission != null) {
       if (project.isPresent()) {
         validateProjectPermission(permission);
index 3daadeeb18af94648dacb0d9e462939e00cc7656..7ee13f82ae3f2f4438cf27c0680d50780057a2f5 100644 (file)
@@ -127,24 +127,51 @@ public class UsersActionTest extends BasePermissionWsTest<UsersAction> {
   public void search_also_for_users_without_permission_when_filtering_name() throws Exception {
     // User with permission on project
     ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()));
-    UserDto user = db.users().insertUser(newUserDto("with-permission", "with-permission", null));
+    UserDto user = db.users().insertUser(newUserDto("with-permission-login", "with-permission-name", "with-permission-email"));
     db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
 
     // User without permission
-    UserDto withoutPermission = db.users().insertUser(newUserDto("without-permission", "without-permission", null));
-    UserDto anotherUser = db.users().insertUser(newUserDto("another-user", "another-user", null));
+    UserDto withoutPermission = db.users().insertUser(newUserDto("without-permission-login", "without-permission-name", "without-permission-email"));
+    UserDto anotherUser = db.users().insertUser(newUserDto("another-user", "another-user", "another-user"));
 
     loginAsAdminOnDefaultOrganization();
-    String result = newRequest()
-      .setParam(PARAM_PROJECT_ID, project.uuid())
-      .setParam(TEXT_QUERY, "with")
-      .execute()
-      .getInput();
+    String result = newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).setParam(TEXT_QUERY, "name").execute().getInput();
 
-    assertThat(result)
-      .contains(user.getLogin())
-      .contains(withoutPermission.getLogin())
-      .doesNotContain(anotherUser.getLogin());
+    assertThat(result).contains(user.getLogin(), withoutPermission.getLogin()).doesNotContain(anotherUser.getLogin());
+  }
+
+  @Test
+  public void search_also_for_users_without_permission_when_filtering_email() throws Exception {
+    // User with permission on project
+    ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()));
+    UserDto user = db.users().insertUser(newUserDto("with-permission-login", "with-permission-name", "with-permission-email"));
+    db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
+
+    // User without permission
+    UserDto withoutPermission = db.users().insertUser(newUserDto("without-permission-login", "without-permission-name", "without-permission-email"));
+    UserDto anotherUser = db.users().insertUser(newUserDto("another-user", "another-user", "another-user"));
+
+    loginAsAdminOnDefaultOrganization();
+    String result = newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).setParam(TEXT_QUERY, "email").execute().getInput();
+
+    assertThat(result).contains(user.getLogin(), withoutPermission.getLogin()).doesNotContain(anotherUser.getLogin());
+  }
+
+  @Test
+  public void search_also_for_users_without_permission_when_filtering_login() throws Exception {
+    // User with permission on project
+    ComponentDto project = db.components().insertComponent(newProjectDto(db.organizations().insert()));
+    UserDto user = db.users().insertUser(newUserDto("with-permission-login", "with-permission-name", "with-permission-email"));
+    db.users().insertProjectPermissionOnUser(user, ISSUE_ADMIN, project);
+
+    // User without permission
+    UserDto withoutPermission = db.users().insertUser(newUserDto("without-permission-login", "without-permission-name", "without-permission-email"));
+    UserDto anotherUser = db.users().insertUser(newUserDto("another-user", "another-user", "another-user"));
+
+    loginAsAdminOnDefaultOrganization();
+    String result = newRequest().setParam(PARAM_PROJECT_ID, project.uuid()).setParam(TEXT_QUERY, "login").execute().getInput();
+
+    assertThat(result).contains(user.getLogin(), withoutPermission.getLogin()).doesNotContain(anotherUser.getLogin());
   }
 
   @Test
index 14824ae435fd9c894c75108db75740a3bf3ef8fb..787d80a8c9a9f068c7b1efd7de512677764d2dc6 100644 (file)
@@ -47,9 +47,10 @@ public class PermissionQuery {
   private final String componentUuid;
   private final String template;
 
-  // filter on name of users or groups
+  // filter on login, email or name of users or groups
   private final String searchQuery;
   private final String searchQueryToSql;
+  private final String searchQueryToSqlLowercase;
 
   // filter users or groups who have at least one permission. It does make
   // sense when the filter "permission" is set.
@@ -64,7 +65,8 @@ public class PermissionQuery {
     this.componentUuid = builder.componentUuid;
     this.template = builder.template;
     this.searchQuery = builder.searchQuery;
-    this.searchQueryToSql = builder.searchQuery == null ? null : buildLikeValue(builder.searchQuery, WildcardPosition.BEFORE_AND_AFTER).toLowerCase(Locale.ENGLISH);
+    this.searchQueryToSql = builder.searchQuery == null ? null : buildLikeValue(builder.searchQuery, WildcardPosition.BEFORE_AND_AFTER);
+    this.searchQueryToSqlLowercase = builder.searchQuery == null ? null : searchQueryToSql.toLowerCase(Locale.ENGLISH);
     this.pageSize = builder.pageSize;
     this.pageOffset = offset(builder.pageIndex, builder.pageSize);
   }
@@ -99,6 +101,11 @@ public class PermissionQuery {
     return searchQueryToSql;
   }
 
+  @CheckForNull
+  public String getSearchQueryToSqlLowercase() {
+    return searchQueryToSqlLowercase;
+  }
+
   public int getPageSize() {
     return pageSize;
   }
index bfdf258f4be4a3fb87e1fbcaa4b62f6e5bd7a172..9b254aa693a3b6c79c0a3f6cb7f7052a784fac64 100644 (file)
@@ -59,7 +59,7 @@
     left join projects p on sub.componentId = p.id
     <where>
       <if test="query.searchQueryToSql != null">
-        and lower(sub.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
+        and lower(sub.name) like #{query.searchQueryToSqlLowercase,jdbcType=VARCHAR} ESCAPE '/'
       </if>
       <!-- filter rows with group permissions -->
       <if test="query.withAtLeastOnePermission()">
index 86fb41937e2a260135d3322fba4409d74c737f4a..cfb0ea572b1f48b39c9f3b19fab3a124d1d11316 100644 (file)
         and u.login in <foreach collection="userLogins" open="(" close=")" item="userLogin" separator=",">#{userLogin,jdbcType=VARCHAR}</foreach>
       </if>
       <if test="query.searchQueryToSql != null">
-        and lower(u.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
+        and (
+        lower(u.name) like #{query.searchQueryToSqlLowercase,jdbcType=VARCHAR} ESCAPE '/'
+        or u.email like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
+        or u.login like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/')
+
       </if>
       <!-- filter rows with user permissions -->
       <if test="query.withAtLeastOnePermission()">
index fa79c9a5addcd9a1bbda8a10905f9ade48e15b34..769a0f539076d35bfa87528aaf67eb279d122c83 100644 (file)
     <where>
       u.active = ${_true}
       <if test="query.getSearchQueryToSql() != null">
-        AND lower(u.name) like #{query.searchQueryToSql} ESCAPE '/'
+        AND lower(u.name) like #{query.searchQueryToSqlLowercase} ESCAPE '/'
       </if>
       <if test="query.withAtLeastOnePermission()">
         and ptu.permission_reference is not null
     ) groups
     <where>
       <if test="query.searchQueryToSql != null">
-        AND LOWER(groups.name) LIKE #{query.searchQueryToSql} ESCAPE '/'
+        AND LOWER(groups.name) LIKE #{query.searchQueryToSqlLowercase} ESCAPE '/'
       </if>
       <if test="query.withAtLeastOnePermission()">
         AND groups.permission IS NOT NULL
index e8aec5196441087b8979e0aaaf0458d9445608b7..6e2426cd991d1916e85e6bdd29cf5e507ab1dd5e 100644 (file)
@@ -49,9 +49,9 @@ public class UserPermissionDaoTest {
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
   private UserPermissionDao underTest = new UserPermissionDao();
-  private UserDto user1 = newUserDto().setLogin("login1").setName("Marius").setActive(true);
-  private UserDto user2 = newUserDto().setLogin("login2").setName("Marie").setActive(true);
-  private UserDto user3 = newUserDto().setLogin("login3").setName("Bernard").setActive(true);
+  private UserDto user1 = newUserDto().setLogin("login1").setName("Marius").setEmail("email1@email.com").setActive(true);
+  private UserDto user2 = newUserDto().setLogin("login2").setName("Marie").setEmail("email2@email.com").setActive(true);
+  private UserDto user3 = newUserDto().setLogin("zanother").setName("Zoe").setEmail("zanother3@another.com").setActive(true);
   private OrganizationDto organizationDto;
   private ComponentDto project1;
   private ComponentDto project2;
@@ -88,7 +88,7 @@ public class UserPermissionDaoTest {
     // (that's a non-sense, but still this is required for api/permissions/groups
     // when filtering users by name)
     query = PermissionQuery.builder().build();
-    expectPermissions(organizationDto, query, null, project1Perm, global2, global3, org2Global2, global1, org2Global1);
+    expectPermissions(organizationDto, query, null, global2, global3, org2Global2, global1, org2Global1, project1Perm);
 
     // return empty list if non-null but empty logins
     expectPermissions(organizationDto, query, Collections.emptyList());
@@ -126,9 +126,17 @@ public class UserPermissionDaoTest {
     expectPermissions(organizationDto, query, null);
 
     // search by user name (matches 2 users)
-    query = PermissionQuery.builder().withAtLeastOnePermission().setSearchQuery("Mari").build();
+    query = PermissionQuery.builder().withAtLeastOnePermission().setSearchQuery("mari").build();
     expectPermissions(organizationDto, query, null, global2, global3, global1);
 
+    // search by user login
+    query = PermissionQuery.builder().withAtLeastOnePermission().setSearchQuery("ogin2").build();
+    expectPermissions(organizationDto, query, null, global2, global3);
+
+    // search by user email
+    query = PermissionQuery.builder().withAtLeastOnePermission().setSearchQuery("mail2").build();
+    expectPermissions(organizationDto, query, null, global2, global3);
+
     // search by user name (matches 2 users) and global permission
     query = PermissionQuery.builder().setSearchQuery("Mari").setPermission(PROVISIONING).build();
     expectPermissions(organizationDto, query, null, global3);