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.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
+import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Strings.isNullOrEmpty;
public class CurrentAction implements UsersWsAction {
private final UserSession userSession;
private final DbClient dbClient;
+ private final DefaultOrganizationProvider defaultOrganizationProvider;
- public CurrentAction(UserSession userSession, DbClient dbClient) {
+ public CurrentAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider) {
this.userSession = userSession;
this.dbClient = dbClient;
+ this.defaultOrganizationProvider = defaultOrganizationProvider;
}
@Override
writeScmAccounts(json, optionalUser);
writeGroups(json, groups);
- writePermissions(json, userSession);
+ writePermissions(json);
}
private static void writeScmAccounts(JsonWriter json, Optional<UserDto> optionalUser) {
json.endArray();
}
- private static void writePermissions(JsonWriter json, UserSession session) {
+ private void writePermissions(JsonWriter json) {
json.name("permissions").beginObject();
- writeGlobalPermissions(json, session);
+ writeGlobalPermissions(json);
json.endObject();
}
- private static void writeGlobalPermissions(JsonWriter json, UserSession session) {
+ private void writeGlobalPermissions(JsonWriter json) {
json.name("global").beginArray();
- for (String permission : session.globalPermissions()) {
- json.value(permission);
- }
+
+ String defaultOrganizationUuid = defaultOrganizationProvider.get().getUuid();
+ GlobalPermissions.ALL.stream()
+ .filter(permission -> userSession.hasOrganizationPermission(defaultOrganizationUuid, permission))
+ .forEach(permission -> json.value(permission));
+
json.endArray();
}
],
"permissions": {
"global": [
- "admin",
"profileadmin",
- "gateadmin",
- "scan",
- "provisioning"
+ "scan"
]
}
}
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserGroupDto;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
public UserSessionRule userSessionRule = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
+ private DbClient dbClient = db.getDbClient();
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private WsActionTester ws;
@Before
public void before() {
- ws = new WsActionTester(new CurrentAction(userSessionRule, dbClient));
+ ws = new WsActionTester(new CurrentAction(userSessionRule, dbClient, defaultOrganizationProvider));
}
@Test
- public void json_example() throws Exception {
- userSessionRule.logIn("obiwan.kenobi").setName("Obiwan Kenobi")
- .setGlobalPermissions(GlobalPermissions.ALL.toArray(new String[0]));
+ public void json_example() {
+ userSessionRule.logIn("obiwan.kenobi").setName("Obiwan Kenobi");
+
+ // permissions on default organization
+ userSessionRule
+ .addOrganizationPermission(db.getDefaultOrganization(), GlobalPermissions.SCAN_EXECUTION)
+ .addOrganizationPermission(db.getDefaultOrganization(), GlobalPermissions.QUALITY_PROFILE_ADMIN);
+
+ // permissions on other organizations are ignored
+ userSessionRule.addOrganizationPermission(db.organizations().insert(), GlobalPermissions.SYSTEM_ADMIN);
+
UserDto obiwan = db.users().insertUser(
newUserDto("obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com")
.setLocal(true)
}
@Test
- public void anonymous() throws Exception {
+ public void anonymous() {
+ userSessionRule.anonymous();
+
String response = ws.newRequest().execute().getInput();
assertJson(response).isSimilarTo(getClass().getResource("CurrentActionTest/anonymous.json"));
import org.junit.Test;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
+import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.user.UserUpdater;
import org.sonar.server.user.index.UserIndex;
WsTester tester = new WsTester(new UsersWs(
new CreateAction(mock(UserUpdater.class), userSessionRule),
new UpdateAction(mock(UserUpdater.class), userSessionRule, mock(UserJsonWriter.class), mock(DbClient.class)),
- new CurrentAction(userSessionRule, mock(org.sonar.db.DbClient.class)),
+ new CurrentAction(userSessionRule, mock(DbClient.class), mock(DefaultOrganizationProvider.class)),
new ChangePasswordAction(mock(DbClient.class), mock(UserUpdater.class), userSessionRule),
new SearchAction(mock(UserIndex.class), mock(DbClient.class), mock(UserJsonWriter.class))));
controller = tester.controller("api/users");