public class UserJsonWriter {
- private static final String FIELD_LOGIN = "login";
- private static final String FIELD_NAME = "name";
- private static final String FIELD_EMAIL = "email";
- private static final String FIELD_SCM_ACCOUNTS = "scmAccounts";
- private static final String FIELD_GROUPS = "groups";
- private static final String FIELD_ACTIVE = "active";
- private static final String FIELD_TOKENS_COUNT = "tokensCount";
- private static final String FIELD_LOCAL = "local";
+ static final String FIELD_LOGIN = "login";
+ static final String FIELD_NAME = "name";
+ static final String FIELD_EMAIL = "email";
+ static final String FIELD_SCM_ACCOUNTS = "scmAccounts";
+ static final String FIELD_GROUPS = "groups";
+ static final String FIELD_ACTIVE = "active";
+ static final String FIELD_TOKENS_COUNT = "tokensCount";
+ static final String FIELD_LOCAL = "local";
+ static final String FIELD_EXTERNAL_IDENTITY = "externalIdentity";
+ static final String FIELD_EXTERNAL_PROVIDER = "externalProvider";
- public static final Set<String> FIELDS = ImmutableSet.of(FIELD_NAME, FIELD_EMAIL, FIELD_SCM_ACCOUNTS, FIELD_GROUPS, FIELD_ACTIVE, FIELD_LOCAL);
+ public static final Set<String> FIELDS = ImmutableSet.of(FIELD_NAME, FIELD_EMAIL, FIELD_SCM_ACCOUNTS, FIELD_GROUPS, FIELD_ACTIVE, FIELD_LOCAL, FIELD_EXTERNAL_IDENTITY,
+ FIELD_EXTERNAL_PROVIDER);
private static final Set<String> CONCISE_FIELDS = ImmutableSet.of(FIELD_NAME, FIELD_EMAIL, FIELD_ACTIVE);
private final UserSession userSession;
writeIfNeeded(json, user.getEmail(), FIELD_EMAIL, fields);
writeIfNeeded(json, user.isActive(), FIELD_ACTIVE, fields);
writeIfNeeded(json, user.isLocal(), FIELD_LOCAL, fields);
+ writeIfNeeded(json, user.getExternalIdentity(), FIELD_EXTERNAL_IDENTITY, fields);
+ writeIfNeeded(json, user.getExternalIdentityProvider(), FIELD_EXTERNAL_PROVIDER, fields);
writeGroupsIfNeeded(json, groups, fields);
writeScmAccountsIfNeeded(json, fields, user);
writeTokensCount(json, tokensCount);
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.user.GroupDbTester;
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDbTester;
import org.sonar.db.user.UserDto;
import org.sonar.server.user.index.UserIndexDefinition;
import org.sonar.server.ws.WsTester;
+import static com.google.common.collect.Lists.newArrayList;
+import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.db.user.UserTesting.newUserDto;
import static org.sonar.db.user.UserTokenTesting.newUserToken;
-
+import static org.sonar.test.JsonAssert.assertJson;
public class SearchActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
UserDbTester userDb = new UserDbTester(db);
+ GroupDbTester groupDb = new GroupDbTester(db);
DbClient dbClient = db.getDbClient();
final DbSession dbSession = db.getSession();
ws = new WsTester(new UsersWs(new SearchAction(index, dbClient, new UserJsonWriter(userSession))));
}
+ @Test
+ public void search_json_example() throws Exception {
+ UserDto fmallet = userDb.insertUser(newUserDto("fmallet", "Freddy Mallet", "f@m.com")
+ .setActive(true)
+ .setLocal(true)
+ .setScmAccounts(emptyList()));
+ UserDto simon = userDb.insertUser(newUserDto("sbrandhof", "Simon", "s.brandhof@company.tld")
+ .setActive(true)
+ .setLocal(false)
+ .setExternalIdentity("sbrandhof@ldap.com")
+ .setExternalIdentityProvider("LDAP")
+ .setScmAccounts(newArrayList("simon.brandhof", "s.brandhof@company.tld")));
+ GroupDto sonarUsers = groupDb.insertGroup(newGroupDto().setName("sonar-users"));
+ GroupDto sonarAdministrators = groupDb.insertGroup(newGroupDto().setName("sonar-administrators"));
+ dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setUserId(simon.getId()).setGroupId(sonarUsers.getId()));
+ dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setUserId(fmallet.getId()).setGroupId(sonarUsers.getId()));
+ dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setUserId(fmallet.getId()).setGroupId(sonarAdministrators.getId()));
+
+ for (int i = 0; i < 3; i++) {
+ dbClient.userTokenDao().insert(dbSession, newUserToken().setLogin(simon.getLogin()));
+ }
+ dbClient.userTokenDao().insert(dbSession, newUserToken().setLogin(fmallet.getLogin()));
+ db.commit();
+ esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER, toUserDoc(fmallet), toUserDoc(simon));
+ loginAsAdmin();
+
+ String response = ws.newGetRequest("api/users", "search").execute().outputAsString();
+
+ assertJson(response).isSimilarTo(getClass().getResource("search-example.json"));
+ }
+
@Test
public void search_empty() throws Exception {
ws.newGetRequest("api/users", "search").execute().assertJson(getClass(), "empty.json");
injectUsers(5);
UserDto user = userDb.insertUser(
newUserDto("user-%_%-login", "user-name", "user@mail.com")
- .setScmAccounts("user1")
- );
+ .setScmAccounts("user1"));
esTester.putDocuments(UserIndexDefinition.INDEX, UserIndexDefinition.TYPE_USER,
new UserDoc()
.setActive(true)
String name = String.format("User %d", index);
List<String> scmAccounts = singletonList(String.format("user-%d", index));
- userDtos.add(dbClient.userDao().insert(dbSession, new UserDto()
+ UserDto userDto = dbClient.userDao().insert(dbSession, new UserDto()
.setActive(true)
.setCreatedAt(createdAt)
.setEmail(email)
.setName(name)
.setScmAccounts(scmAccounts)
.setLocal(true)
- .setUpdatedAt(createdAt)));
+ .setExternalIdentity(login)
+ .setExternalIdentityProvider("sonarqube")
+ .setUpdatedAt(createdAt));
+ userDtos.add(userDto);
- users[index] = new UserDoc()
- .setActive(true)
- .setCreatedAt(createdAt)
- .setEmail(email)
- .setLogin(login)
- .setName(name)
- .setScmAccounts(scmAccounts)
- .setUpdatedAt(createdAt);
+ users[index] = toUserDoc(userDto);
for (int tokenIndex = 0; tokenIndex < index; tokenIndex++) {
dbClient.userTokenDao().insert(dbSession, newUserToken()
return userDtos;
}
+ private static UserDoc toUserDoc(UserDto dto) {
+ return new UserDoc()
+ .setActive(dto.isActive())
+ .setCreatedAt(dto.getCreatedAt())
+ .setEmail(dto.getEmail())
+ .setLogin(dto.getLogin())
+ .setName(dto.getName())
+ .setScmAccounts(dto.getScmAccountsAsList())
+ .setUpdatedAt(dto.getUpdatedAt());
+ }
+
private void loginAsAdmin() {
userSession.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
}