this.support = support;
}
- private static void writeMembers(JsonWriter json, List<UserMembershipDto> users) {
- json.name("users").beginArray();
- for (UserMembershipDto user : users) {
- json.beginObject()
- .prop(FIELD_LOGIN, user.getLogin())
- .prop(FIELD_NAME, user.getName())
- .prop(FIELD_SELECTED, user.getGroupId() != null)
- .endObject();
- }
- json.endArray();
- }
-
- private static void writePaging(JsonWriter json, Paging paging) {
- json.prop(Param.PAGE, paging.pageIndex())
- .prop(Param.PAGE_SIZE, paging.pageSize())
- .prop("total", paging.total());
- }
-
- private static String getMembership(String selected) {
- SelectionMode selectionMode = SelectionMode.fromParam(selected);
- String membership = GroupMembershipQuery.ANY;
- if (SelectionMode.SELECTED == selectionMode) {
- membership = GroupMembershipQuery.IN;
- } else if (SelectionMode.DESELECTED == selectionMode) {
- membership = GroupMembershipQuery.OUT;
- }
- return membership;
- }
-
@Override
public void define(NewController context) {
NewAction action = context.createAction("users")
json.endObject().close();
}
}
+
+ private static void writeMembers(JsonWriter json, List<UserMembershipDto> users) {
+ json.name("users").beginArray();
+ for (UserMembershipDto user : users) {
+ json.beginObject()
+ .prop(FIELD_LOGIN, user.getLogin())
+ .prop(FIELD_NAME, user.getName())
+ .prop(FIELD_SELECTED, user.getGroupId() != null)
+ .endObject();
+ }
+ json.endArray();
+ }
+
+ private static void writePaging(JsonWriter json, Paging paging) {
+ json.prop(Param.PAGE, paging.pageIndex())
+ .prop(Param.PAGE_SIZE, paging.pageSize())
+ .prop("total", paging.total());
+ }
+
+ private static String getMembership(String selected) {
+ SelectionMode selectionMode = SelectionMode.fromParam(selected);
+ String membership = GroupMembershipQuery.ANY;
+ if (SelectionMode.SELECTED == selectionMode) {
+ membership = GroupMembershipQuery.IN;
+ } else if (SelectionMode.DESELECTED == selectionMode) {
+ membership = GroupMembershipQuery.OUT;
+ }
+ return membership;
+ }
}
@Test
public void return_members_by_group_id() throws Exception {
GroupDto group = db.users().insertGroup();
- UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada").setName("Ada Lovelace"));
+ UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada.login").setName("Ada Lovelace"));
db.users().insertMember(group, user1);
db.users().insertUser(newUserDto().setLogin("grace").setName("Grace Hopper"));
loginAsAdminOnDefaultOrganization();
public void references_group_by_its_name() throws Exception {
OrganizationDto org = db.organizations().insert();
GroupDto group = db.users().insertGroup(org, "the-group");
- UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada").setName("Ada Lovelace"));
+ UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada.login").setName("Ada Lovelace"));
db.users().insertMember(group, user1);
db.users().insertUser(newUserDto().setLogin("grace").setName("Grace Hopper"));
loginAsAdmin(org);
@Test
public void references_group_in_default_organization_by_its_name() throws Exception {
GroupDto group = db.users().insertGroup();
- UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada").setName("Ada Lovelace"));
+ UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada.login").setName("Ada Lovelace"));
db.users().insertMember(group, user1);
db.users().insertUser(newUserDto().setLogin("grace").setName("Grace Hopper"));
loginAsAdminOnDefaultOrganization();
db.users().insertMember(group, graceHopper);
loginAsAdminOnDefaultOrganization();
- String response = newUsersRequest()
- .setParam(PARAM_GROUP_ID, group.getId().toString())
- .execute().outputAsString();
+ String response = newUsersRequest().setParam(PARAM_GROUP_ID, group.getId().toString()).execute().outputAsString();
assertThat(response).contains("Ada Lovelace", "Grace Hopper");
}
}
@Test
- public void filtering() throws Exception {
+ public void filtering_by_name_email_and_login() throws Exception {
GroupDto group = db.users().insertGroup();
- UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada").setName("Ada Lovelace"));
- db.users().insertUser(newUserDto().setLogin("grace").setName("Grace Hopper"));
+ UserDto user1 = db.users().insertUser(newUserDto().setLogin("ada.login").setName("Ada Lovelace").setEmail("ada@email.com"));
+ db.users().insertUser(newUserDto().setLogin("grace").setName("Grace Hopper").setEmail("grace@hopper.com"));
db.users().insertMember(group, user1);
loginAsAdminOnDefaultOrganization();
.execute()
.assertJson(getClass(), "all.json");
- newUsersRequest()
- .setParam("id", group.getId().toString())
- .setParam("q", "love")
+ newUsersRequest().setParam("id", group.getId().toString())
+ .setParam("q", ".logi")
+ .execute()
+ .assertJson(getClass(), "all_ada.json");
+
+ newUsersRequest().setParam("id", group.getId().toString())
+ .setParam("q", "OvE")
+ .execute()
+ .assertJson(getClass(), "all_ada.json");
+
+ newUsersRequest().setParam("id", group.getId().toString())
+ .setParam("q", "mail")
.execute()
.assertJson(getClass(), "all_ada.json");
}
"p": 1,
"total": 2,
"users": [
- {"login": "ada", "name": "Ada Lovelace", "selected": true},
+ {"login": "ada.login", "name": "Ada Lovelace", "selected": true},
{"login": "grace", "name": "Grace Hopper", "selected": false}
]
-}
\ No newline at end of file
+}
"p": 1,
"total": 1,
"users": [
- {"login": "ada", "name": "Ada Lovelace", "selected": true}
+ {
+ "login": "ada.login",
+ "name": "Ada Lovelace",
+ "selected": true
+ }
]
-}
\ No newline at end of file
+}
package org.sonar.db.user;
import com.google.common.collect.ImmutableSet;
+import java.util.Locale;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
+import static org.sonar.db.DatabaseUtils.buildLikeValue;
+import static org.sonar.db.WildcardPosition.BEFORE_AND_AFTER;
public class UserMembershipQuery {
// for internal use in MyBatis
final String memberSearchSql;
+ final String memberSearchSqlLowercase;
// max results per page
private final int pageSize;
this.groupId = builder.groupId;
this.membership = builder.membership;
this.memberSearch = builder.memberSearch;
- this.memberSearchSql = memberSearchToSql(memberSearch);
-
+ this.memberSearchSql = memberSearch == null ? null : buildLikeValue(memberSearch, BEFORE_AND_AFTER);
+ this.memberSearchSqlLowercase = memberSearchSql == null ? null : memberSearchSql.toLowerCase(Locale.ENGLISH);
this.pageSize = builder.pageSize;
this.pageIndex = builder.pageIndex;
}
- private String memberSearchToSql(@Nullable String s) {
- String sql = null;
- if (s != null) {
- sql = StringUtils.replace(StringUtils.upperCase(s), "%", "/%");
- sql = StringUtils.replace(sql, "_", "/_");
- sql = "%" + sql + "%";
- }
- return sql;
- }
-
public long groupId() {
return groupId;
}
}
/**
- * Search for users names/logins containing a given string
+ * Search for users email, login and name containing a given string
*/
@CheckForNull
public String memberSearch() {
</when>
</choose>
<if test="query.memberSearch() != null">
- AND ((UPPER(u.login) LIKE #{query.memberSearchSql} ESCAPE '/') OR (UPPER(u.name) LIKE #{query.memberSearchSql}
- ESCAPE '/'))
+ AND (
+ lower(u.name) like #{query.memberSearchSqlLowercase} ESCAPE '/'
+ or u.login like #{query.memberSearchSql} ESCAPE '/'
+ or u.email like #{query.memberSearchSql} ESCAPE '/' )
</if>
AND u.active=${_true}
</where>
List<UserMembershipDto> result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("admin").build(), 0, 10);
assertThat(result).hasSize(2);
- assertThat(result.get(0).getName()).isEqualTo("Admin");
+ assertThat(result.get(0).getName()).isEqualTo("Admin name");
assertThat(result.get(1).getName()).isEqualTo("Not Admin");
result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("not").build(), 0, 10);
}
@Test
- public void search_by_login_or_name_with_capitalization() {
+ public void search_by_login_name_or_email() {
dbTester.prepareDbUnit(getClass(), "shared_plus_empty_group.xml");
- List<UserMembershipDto> result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("admin").build(), 0, 10);
- assertThat(result).hasSize(2);
+ // search is case insensitive only on name
+ List<UserMembershipDto> result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("NaMe").build(), 0, 10);
+ assertThat(result).hasSize(1);
- result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("AdMiN").build(), 0, 10);
- assertThat(result).hasSize(2);
+ result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("login").build(), 0, 10);
+ assertThat(result).hasSize(1);
+ result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).memberSearch("email").build(), 0, 10);
+ assertThat(result).hasSize(1);
}
@Test
List<UserMembershipDto> result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).build(), 0, 10);
assertThat(result).hasSize(2);
- assertThat(result.get(0).getName()).isEqualTo("Admin");
+ assertThat(result.get(0).getName()).isEqualTo("Admin name");
assertThat(result.get(1).getName()).isEqualTo("Not Admin");
}
List<UserMembershipDto> result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).build(), 0, 2);
assertThat(result).hasSize(2);
- assertThat(result.get(0).getName()).isEqualTo("Admin");
+ assertThat(result.get(0).getName()).isEqualTo("Admin name");
assertThat(result.get(1).getName()).isEqualTo("Not Admin");
result = underTest.selectMembers(dbTester.getSession(), UserMembershipQuery.builder().groupId(100L).build(), 1, 2);
group_id="101"/>
<users id="200"
- login="admin"
- name="Admin"
+ login="admin login"
+ name="Admin name"
+ email="admin@email.com"
active="[true]"
is_root="[false]"/>
<users id="201"
login="not.admin"
name="Not Admin"
+ email="not admin"
active="[true]"
is_root="[false]"/>
<users id="202"