]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7250 WS users/current - add details 737/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 27 Jan 2016 16:11:54 +0000 (17:11 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 28 Jan 2016 15:02:49 +0000 (16:02 +0100)
server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java
server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-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/UsersWsTest.java
server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/anonymous.json
server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/authenticated.json [deleted file]
sonar-db/src/test/java/org/sonar/db/user/GroupDbTester.java [new file with mode: 0644]

index 06226a23924a15525a1293164a99530a28cde121..287273cd5610f3e59b01e4b552f257fd009165f9 100644 (file)
  */
 package org.sonar.server.user.ws;
 
+import com.google.common.base.Optional;
+import java.util.Collection;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService.NewController;
 import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.user.UserDto;
 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;
+
 public class CurrentAction implements UsersWsAction {
   private final UserSession userSession;
+  private final DbClient dbClient;
 
-  public CurrentAction(UserSession userSession) {
+  public CurrentAction(UserSession userSession, DbClient dbClient) {
     this.userSession = userSession;
+    this.dbClient = dbClient;
   }
 
   @Override
@@ -44,27 +55,79 @@ public class CurrentAction implements UsersWsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    JsonWriter json = response.newJsonWriter().beginObject();
+    DbSession dbSession = dbClient.openSession(false);
+    try {
+      Optional<UserDto> user = Optional.absent();
+      Collection<String> groups = emptyList();
+      if (userSession.isLoggedIn()) {
+        user = selectCurrentUser(dbSession);
+        groups = selectGroups(dbSession);
+      }
+      writeResponse(response, user, groups);
+    } finally {
+      dbClient.closeSession(dbSession);
+    }
+  }
 
-    writeUserDetails(json, userSession);
+  private Optional<UserDto> selectCurrentUser(DbSession dbSession) {
+    return Optional.fromNullable(dbClient.userDao().selectActiveUserByLogin(dbSession, userSession.getLogin()));
+  }
 
+  private Collection<String> selectGroups(DbSession dbSession) {
+    return dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(userSession.getLogin()))
+      .get(userSession.getLogin());
+  }
+
+  private void writeResponse(Response response, Optional<UserDto> user, Collection<String> groups) {
+    JsonWriter json = response.newJsonWriter().beginObject();
+    writeUserDetails(json, user, groups);
     json.endObject().close();
   }
 
-  private void writeUserDetails(JsonWriter json, UserSession session) {
-    json.prop("isLoggedIn", session.isLoggedIn())
-      .prop("login", session.getLogin())
-      .prop("name", session.getName());
-    writePermissions(json, session);
+  private void writeUserDetails(JsonWriter json, Optional<UserDto> optionalUser, Collection<String> groups) {
+    json
+      .prop("isLoggedIn", userSession.isLoggedIn())
+      .prop("login", userSession.getLogin())
+      .prop("name", userSession.getName());
+    if (optionalUser.isPresent()) {
+      UserDto user = optionalUser.get();
+      if (!isNullOrEmpty(user.getEmail())) {
+        json.prop("email", user.getEmail());
+      }
+    }
+
+    writeScmAccounts(json, optionalUser);
+    writeGroups(json, groups);
+    writePermissions(json, userSession);
+  }
+
+  private static void writeScmAccounts(JsonWriter json, Optional<UserDto> optionalUser) {
+    json.name("scmAccounts");
+    json.beginArray();
+    if (optionalUser.isPresent()) {
+      for (String scmAccount : optionalUser.get().getScmAccountsAsList()) {
+        json.value(scmAccount);
+      }
+    }
+    json.endArray();
+  }
+
+  private static void writeGroups(JsonWriter json, Collection<String> groups) {
+    json.name("groups");
+    json.beginArray();
+    for (String group : groups) {
+      json.value(group);
+    }
+    json.endArray();
   }
 
-  private void writePermissions(JsonWriter json, UserSession session) {
+  private static void writePermissions(JsonWriter json, UserSession session) {
     json.name("permissions").beginObject();
     writeGlobalPermissions(json, session);
     json.endObject();
   }
 
-  private void writeGlobalPermissions(JsonWriter json, UserSession session) {
+  private static void writeGlobalPermissions(JsonWriter json, UserSession session) {
     json.name("global").beginArray();
     for (String permission : session.globalPermissions()) {
       json.value(permission);
index 9c3d477a7ea9942a45af9a178a22c26e28c67e5c..b68e4c6ea300c8f68850d9e1a6409684b24be8ca 100644 (file)
@@ -1,10 +1,24 @@
 {
   "isLoggedIn": true,
-  "login": "george.orwell",
-  "name": "George Orwell",
+  "login": "obiwan.kenobi",
+  "name": "Obiwan Kenobi",
+  "email": "obiwan.kenobi@starwars.com",
+  "scmAccounts": [
+    "obiwan:github",
+    "obiwan:bitbucket"
+  ],
+  "groups": [
+    "Jedi", "Rebel"
+  ],
   "permissions": {
     "global": [
-      "dryRunScan"
+      "admin",
+      "profileadmin",
+      "gateadmin",
+      "shareDashboard",
+      "scan",
+      "dryRunScan",
+      "provisioning"
     ]
   }
 }
index 5cbf31fc91edebccb0acfb4cb2ff3887609c5b37..50d73e96b03229ad7466be86fd91d956e8decdce 100644 (file)
@@ -22,30 +22,66 @@ package org.sonar.server.user.ws;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonar.api.utils.System2;
 import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.db.DbClient;
+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.db.user.UserGroupDto;
 import org.sonar.server.tester.UserSessionRule;
-import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.WsActionTester;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.db.user.GroupTesting.newGroupDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
+import static org.sonar.test.JsonAssert.assertJson;
 
 public class CurrentActionTest {
   @Rule
   public UserSessionRule userSessionRule = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create(System2.INSTANCE);
+  UserDbTester userDb = new UserDbTester(db);
+  GroupDbTester groupDb = new GroupDbTester(db);
+  DbClient dbClient = db.getDbClient();
 
-  private WsTester tester;
+  private WsActionTester ws;
 
   @Before
   public void before() {
-    tester = new WsTester(new UsersWs(new CurrentAction(userSessionRule)));
+    ws = new WsActionTester(new CurrentAction(userSessionRule, dbClient));
   }
 
   @Test
-  public void anonymous() throws Exception {
-    tester.newGetRequest("api/users", "current").execute().assertJson(getClass(), "anonymous.json");
+  public void json_example() throws Exception {
+    userSessionRule.login("obiwan.kenobi").setName("Obiwan Kenobi")
+      .setGlobalPermissions(GlobalPermissions.ALL.toArray(new String[0]));
+    UserDto obiwan = userDb.insertUser(
+      newUserDto("obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com")
+        .setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket")));
+    GroupDto jedi = groupDb.insertGroup(newGroupDto().setName("Jedi"));
+    GroupDto rebel = groupDb.insertGroup(newGroupDto().setName("Rebel"));
+    groupDb.insertGroup(newGroupDto().setName("Sith"));
+    dbClient.userGroupDao().insert(db.getSession(), new UserGroupDto()
+      .setUserId(obiwan.getId())
+      .setGroupId(jedi.getId()));
+    dbClient.userGroupDao().insert(db.getSession(), new UserGroupDto()
+      .setUserId(obiwan.getId())
+      .setGroupId(rebel.getId()));
+    db.commit();
+
+    String response = ws.newRequest().execute().getInput();
+
+    assertJson(response).isSimilarTo(getClass().getResource("current-example.json"));
   }
 
   @Test
-  public void authenticated() throws Exception {
-    userSessionRule.login("obiwan.kenobi").setName("Obiwan Kenobi")
-      .setGlobalPermissions(GlobalPermissions.ALL.toArray(new String[0]));
-    tester.newGetRequest("api/users", "current").execute().assertJson(getClass(), "authenticated.json");
+  public void anonymous() throws Exception {
+    String response = ws.newRequest().execute().getInput();
+
+    assertJson(response).isSimilarTo(getClass().getResource("CurrentActionTest/anonymous.json"));
   }
 }
index cb149d2e0f3b584409e5996adfe22390c51f48bb..3de0d9220159ec6820b5591aa5cfaa52a7c7e084 100644 (file)
@@ -43,7 +43,7 @@ public class UsersWsTest {
     WsTester tester = new WsTester(new UsersWs(
       new CreateAction(mock(UserIndex.class), mock(UserUpdater.class), mock(I18n.class), userSessionRule, mock(UserJsonWriter.class)),
       new UpdateAction(mock(UserIndex.class), mock(UserUpdater.class), userSessionRule, mock(UserJsonWriter.class), mock(DbClient.class)),
-      new CurrentAction(userSessionRule),
+      new CurrentAction(userSessionRule, mock(org.sonar.db.DbClient.class)),
       new DeactivateAction(mock(UserIndex.class), mock(UserUpdater.class), userSessionRule, mock(UserJsonWriter.class), mock(DbClient.class)),
       new ChangePasswordAction(mock(UserUpdater.class), userSessionRule),
       new SearchAction(mock(UserIndex.class), mock(DbClient.class), mock(UserJsonWriter.class))));
index ba25670ea7d3ce76ba3aaf1b11ac12a3acf5bea8..40a824848007e8272f61ac83164cfaed770b423c 100644 (file)
@@ -1,5 +1,7 @@
 {
   "isLoggedIn": false,
+  "scmAccounts": [],
+  "groups": [],
   "permissions": {
     "global": []
   }
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/authenticated.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/authenticated.json
deleted file mode 100644 (file)
index dbd6bd5..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-{
-  "isLoggedIn": true,
-  "login": "obiwan.kenobi",
-  "name": "Obiwan Kenobi",
-  "permissions": {
-    "global": ["admin", "profileadmin", "gateadmin", "shareDashboard", "scan", "dryRunScan", "provisioning"]
-  }
-}
diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupDbTester.java b/sonar-db/src/test/java/org/sonar/db/user/GroupDbTester.java
new file mode 100644 (file)
index 0000000..3ded0c4
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.db.user;
+
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+public class GroupDbTester {
+  private final DbTester db;
+  private final DbClient dbClient;
+  private final DbSession dbSession;
+
+  public GroupDbTester(DbTester db) {
+    this.db = db;
+    this.dbClient = db.getDbClient();
+    this.dbSession = db.getSession();
+  }
+
+  public GroupDto insertGroup(GroupDto groupDto) {
+    GroupDto updatedGroup = dbClient.groupDao().insert(dbSession, groupDto);
+    db.commit();
+
+    return updatedGroup;
+  }
+}