*/
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
@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);
{
"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"
]
}
}
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"));
}
}
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))));
{
"isLoggedIn": false,
+ "scmAccounts": [],
+ "groups": [],
"permissions": {
"global": []
}
+++ /dev/null
-{
- "isLoggedIn": true,
- "login": "obiwan.kenobi",
- "name": "Obiwan Kenobi",
- "permissions": {
- "global": ["admin", "profileadmin", "gateadmin", "shareDashboard", "scan", "dryRunScan", "provisioning"]
- }
-}
--- /dev/null
+/*
+ * 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;
+ }
+}