diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-10 12:05:37 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-13 18:02:19 +0200 |
commit | 7b9c0c06b2a803f3cfad7baceccd83f361ed8eaf (patch) | |
tree | 2f05ee324d10690e39c4c65722ec13e6b84a3d6b /server | |
parent | d953ac33b97fcfb55042b3b812585cc95ee1759d (diff) | |
download | sonarqube-7b9c0c06b2a803f3cfad7baceccd83f361ed8eaf.tar.gz sonarqube-7b9c0c06b2a803f3cfad7baceccd83f361ed8eaf.zip |
SONAR-6405 WS to get the details of the current user
Diffstat (limited to 'server')
7 files changed, 151 insertions, 10 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 202b1558515..43f1691fb9f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -497,6 +497,7 @@ class ServerComponents { pico.addSingleton(UsersWs.class); pico.addSingleton(org.sonar.server.user.ws.CreateAction.class); pico.addSingleton(org.sonar.server.user.ws.UpdateAction.class); + pico.addSingleton(org.sonar.server.user.ws.CurrentUserAction.class); pico.addSingleton(org.sonar.server.issue.ws.AuthorsAction.class); pico.addSingleton(FavoritesWs.class); pico.addSingleton(UserPropertiesWs.class); diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserSession.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserSession.java index c7e3c95f658..1186c16141e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/UserSession.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserSession.java @@ -36,13 +36,7 @@ import org.sonar.server.platform.Platform; import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; +import java.util.*; import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Maps.newHashMap; @@ -160,7 +154,7 @@ public class UserSession { return globalPermissions().contains(globalPermission); } - List<String> globalPermissions() { + public List<String> globalPermissions() { if (globalPermissions == null) { List<String> permissionKeys = authorizationDao().selectGlobalPermissions(login); globalPermissions = new ArrayList<String>(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentUserAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentUserAction.java new file mode 100644 index 00000000000..f17dbf099c6 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentUserAction.java @@ -0,0 +1,71 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.user.ws; + +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.server.user.UserSession; + +public class CurrentUserAction implements BaseUsersWsAction { + + @Override + public void define(NewController context) { + context.createAction("current") + .setDescription("Get the details of the current authenticated user.") + .setHandler(this) + .setInternal(true) + .setResponseExample(getClass().getResource("example-current.json")) + .setSince("5.2"); + } + + @Override + public void handle(Request request, Response response) throws Exception { + UserSession session = UserSession.get(); + JsonWriter json = response.newJsonWriter().beginObject(); + + writeUserDetails(json, session); + + json.endObject().close(); + } + + private void writeUserDetails(JsonWriter json, UserSession session) { + json.prop("isLoggedIn", session.isLoggedIn()) + .prop("login", session.login()) + .prop("name", session.name()); + writePermissions(json, session); + } + + private void writePermissions(JsonWriter json, UserSession session) { + json.name("permissions").beginObject(); + writeGlobalPermissions(json, session); + json.endObject(); + } + + private void writeGlobalPermissions(JsonWriter json, UserSession session) { + json.name("global").beginArray(); + for (String permission : session.globalPermissions()) { + json.value(permission); + } + json.endArray(); + } + +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentUserActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentUserActionTest.java new file mode 100644 index 00000000000..82b91f197ad --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentUserActionTest.java @@ -0,0 +1,49 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.user.ws; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.permission.GlobalPermissions; +import org.sonar.server.user.MockUserSession; +import org.sonar.server.ws.WsTester; + +public class CurrentUserActionTest { + + private WsTester tester; + + @Before + public void before() { + tester = new WsTester(new UsersWs(new CurrentUserAction())); + } + + @Test + public void anonymous() throws Exception { + MockUserSession.set(); + tester.newGetRequest("api/users", "current").execute().assertJson(getClass(), "anonymous.json"); + } + + @Test + public void authenticated() throws Exception { + MockUserSession.set().setLogin("obiwan.kenobi").setName("Obiwan Kenobi") + .setGlobalPermissions(GlobalPermissions.ALL.toArray(new String[0])); + tester.newGetRequest("api/users", "current").execute().assertJson(getClass(), "authenticated.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 259d7473904..f3551635ac6 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 @@ -37,7 +37,10 @@ public class UsersWsTest { @Before public void setUp() throws Exception { - WsTester tester = new WsTester(new UsersWs(new CreateAction(mock(UserService.class), mock(I18n.class)), new UpdateAction(mock(UserService.class)))); + WsTester tester = new WsTester(new UsersWs( + new CreateAction(mock(UserService.class), mock(I18n.class)), + new UpdateAction(mock(UserService.class)), + new CurrentUserAction())); controller = tester.controller("api/users"); } @@ -46,7 +49,7 @@ public class UsersWsTest { assertThat(controller).isNotNull(); assertThat(controller.description()).isNotEmpty(); assertThat(controller.since()).isEqualTo("3.6"); - assertThat(controller.actions()).hasSize(4); + assertThat(controller.actions()).hasSize(5); } @Test @@ -83,4 +86,13 @@ public class UsersWsTest { assertThat(action.handler()).isInstanceOf(RailsHandler.class); assertThat(action.params()).hasSize(2); } + + @Test + public void define_current_action() throws Exception { + WebService.Action action = controller.action("current"); + assertThat(action).isNotNull(); + assertThat(action.isPost()).isFalse(); + assertThat(action.isInternal()).isTrue(); + assertThat(action.params()).isEmpty(); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/anonymous.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/anonymous.json new file mode 100644 index 00000000000..ba25670ea7d --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/anonymous.json @@ -0,0 +1,6 @@ +{ + "isLoggedIn": false, + "permissions": { + "global": [] + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/authenticated.json b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/authenticated.json new file mode 100644 index 00000000000..ae2de7f3f69 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/user/ws/CurrentUserActionTest/authenticated.json @@ -0,0 +1,8 @@ +{ + "isLoggedIn": true, + "login": "obiwan.kenobi", + "name": "Obiwan Kenobi", + "permissions": { + "global": ["admin", "profileadmin", "shareDashboard", "scan", "dryRunScan", "provisioning"] + } +} |