From: Teryk Bellahsene Date: Wed, 27 Jan 2016 16:11:54 +0000 (+0100) Subject: SONAR-7250 WS users/current - add details X-Git-Tag: 5.4-M10~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F737%2Fhead;p=sonarqube.git SONAR-7250 WS users/current - add details --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java index 06226a23924..287273cd561 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java @@ -19,17 +19,28 @@ */ 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 user = Optional.absent(); + Collection groups = emptyList(); + if (userSession.isLoggedIn()) { + user = selectCurrentUser(dbSession); + groups = selectGroups(dbSession); + } + writeResponse(response, user, groups); + } finally { + dbClient.closeSession(dbSession); + } + } - writeUserDetails(json, userSession); + private Optional selectCurrentUser(DbSession dbSession) { + return Optional.fromNullable(dbClient.userDao().selectActiveUserByLogin(dbSession, userSession.getLogin())); + } + private Collection selectGroups(DbSession dbSession) { + return dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(userSession.getLogin())) + .get(userSession.getLogin()); + } + + private void writeResponse(Response response, Optional user, Collection 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 optionalUser, Collection 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 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 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); diff --git a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json index 9c3d477a7ea..b68e4c6ea30 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json @@ -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" ] } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java index 5cbf31fc91e..50d73e96b03 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java @@ -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")); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java index cb149d2e0f3..3de0d922015 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/UsersWsTest.java @@ -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)))); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/anonymous.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/anonymous.json index ba25670ea7d..40a82484800 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/anonymous.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/anonymous.json @@ -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 index dbd6bd50c8f..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentActionTest/authenticated.json +++ /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 index 00000000000..3ded0c4175a --- /dev/null +++ b/sonar-db/src/test/java/org/sonar/db/user/GroupDbTester.java @@ -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; + } +}