]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7478 External identity information in users WS
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 21 Mar 2016 16:20:40 +0000 (17:20 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 22 Mar 2016 14:37:45 +0000 (15:37 +0100)
server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/UserJsonWriter.java
server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json
server/sonar-server/src/main/resources/org/sonar/server/user/ws/search-example.json
server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java

index d05399763e05cd3153ef2a8c95890310d9b8f872..dee0ad0fdc919685ad1e6e0dab03440a7d2f44c5 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.server.user.UserSession;
 import static com.google.common.base.Strings.isNullOrEmpty;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.singletonList;
+import static org.sonar.server.user.ws.UserJsonWriter.FIELD_EXTERNAL_IDENTITY;
+import static org.sonar.server.user.ws.UserJsonWriter.FIELD_EXTERNAL_PROVIDER;
 
 public class CurrentAction implements UsersWsAction {
   private final UserSession userSession;
@@ -95,6 +97,8 @@ public class CurrentAction implements UsersWsAction {
         json.prop("email", user.getEmail());
       }
       json.prop("local", user.isLocal());
+      json.prop(FIELD_EXTERNAL_IDENTITY, user.getExternalIdentity());
+      json.prop(FIELD_EXTERNAL_PROVIDER, user.getExternalIdentityProvider());
     }
 
     writeScmAccounts(json, optionalUser);
index 88abcd1c8c3e8782337936c7164ccbda1df36d7c..f77a2fb985fba7dc150318081bcbab45061b81e6 100644 (file)
@@ -34,16 +34,19 @@ import static org.sonar.server.ws.JsonWriterUtils.writeIfNeeded;
 
 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;
@@ -69,6 +72,8 @@ public class UserJsonWriter {
     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);
index 4a6ce26ec20cb3cf2ed7c674b2c91f9750cd2696..57d6511b5f8545af51342dcdeaec1fe200ee2934 100644 (file)
@@ -4,6 +4,8 @@
   "name": "Obiwan Kenobi",
   "email": "obiwan.kenobi@starwars.com",
   "local": true,
+  "externalIdentity": "obiwan.kenobi",
+  "externalProvider": "sonarqube",
   "scmAccounts": [
     "obiwan:github",
     "obiwan:bitbucket"
index 21b96a064caa22460aaa615f065c3c7d828f847a..1746fe7b4c698a47cf34ecd4b15e788b82656908 100644 (file)
@@ -11,7 +11,9 @@
         "sonar-administrators"
       ],
       "tokensCount": 1,
-      "local": true
+      "local": true,
+      "externalIdentity": "fmallet",
+      "externalProvider": "sonarqube"
     },
     {
       "login": "sbrandhof",
@@ -26,7 +28,9 @@
         "sonar-users"
       ],
       "tokensCount": 3,
-      "local": false
+      "local": false,
+      "externalIdentity": "sbrandhof@ldap.com",
+      "externalProvider": "LDAP"
     }
   ]
 }
index 50d73e96b03229ad7466be86fd91d956e8decdce..0bf0cb397da93772be0b8bbe8e6e92e3b6f93bdb 100644 (file)
@@ -61,6 +61,9 @@ public class CurrentActionTest {
       .setGlobalPermissions(GlobalPermissions.ALL.toArray(new String[0]));
     UserDto obiwan = userDb.insertUser(
       newUserDto("obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com")
+        .setLocal(true)
+        .setExternalIdentity("obiwan.kenobi")
+        .setExternalIdentityProvider("sonarqube")
         .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket")));
     GroupDto jedi = groupDb.insertGroup(newGroupDto().setName("Jedi"));
     GroupDto rebel = groupDb.insertGroup(newGroupDto().setName("Rebel"));
index 86c377eb49f84066c8dfe91c7fb89666e2936455..ee48a84a72ece43b50670c1d5e2cbb359734aa48 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.core.permission.GlobalPermissions;
 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;
@@ -43,11 +44,14 @@ import org.sonar.server.user.index.UserIndex;
 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 {
 
@@ -58,6 +62,7 @@ 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();
 
@@ -71,6 +76,37 @@ public class SearchActionTest {
     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");
@@ -88,8 +124,7 @@ public class SearchActionTest {
     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)
@@ -184,7 +219,7 @@ public class SearchActionTest {
       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)
@@ -192,16 +227,12 @@ public class SearchActionTest {
         .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()
@@ -214,6 +245,17 @@ public class SearchActionTest {
     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);
   }