import java.util.Collection;
import java.util.List;
-import java.util.Locale;
import java.util.Set;
import org.sonar.server.user.UserSession;
return notImplementedBooleanMethod();
}
- @Override
- public Locale locale() {
- return notImplemented();
- }
-
@Override
public boolean isRoot() {
return notImplementedBooleanMethod();
private final transient Errors errors;
+ /**
+ * @deprecated Bundle key should be replaced by real message as only english is returned on server side errors
+ */
+ @Deprecated
public BadRequestException(String l10nKey, Object... l10nParams) {
super(HTTP_BAD_REQUEST);
this.errors = new Errors().add(Message.of(l10nKey, l10nParams));
}
+ /**
+ * @deprecated Bundle key should be replaced by real message as only english is returned on server side errors
+ */
+ @Deprecated
public BadRequestException(List<Message> messages) {
super(HTTP_BAD_REQUEST);
this.errors = new Errors().add(messages);
import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.i18n.I18n;
import org.sonar.api.utils.text.JsonWriter;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Locale;
+import static java.util.Locale.ENGLISH;
public class Errors {
return expression;
}
- public void writeJson(JsonWriter json, I18n i18n, Locale locale) {
- writeJson(json, i18n, locale, "errors");
- }
-
- public void writeJsonAsWarnings(JsonWriter json, I18n i18n, Locale locale) {
- writeJson(json, i18n, locale, "warnings");
- }
-
- private void writeJson(JsonWriter json, I18n i18n, Locale locale, String name) {
+ public void writeJson(JsonWriter json, I18n i18n) {
if (!messages.isEmpty()) {
- json.name(name).beginArray();
+ json.name("errors").beginArray();
for (Message message : messages) {
json.beginObject();
- String text = StringUtils.defaultString(i18n.message(locale, message.getKey(), message.getKey(), message.getParams()), message.getKey());
+ String text = StringUtils.defaultString(i18n.message(ENGLISH, message.getKey(), message.getKey(), message.getParams()), message.getKey());
json.prop("msg", text);
json.endObject();
}
package org.sonar.server.exceptions;
import com.google.common.base.MoreObjects;
-import org.apache.commons.lang.StringUtils;
-
+import java.util.Arrays;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+import org.apache.commons.lang.StringUtils;
-import java.util.Arrays;
-
+/**
+ * @deprecated Bundle key should be replaced by real message as only english is returned on server side messages
+ */
+@Deprecated
public class Message {
private final String key;
import org.sonar.server.qualityprofile.BulkChangeResult;
import org.sonar.server.qualityprofile.QProfileService;
import org.sonar.server.rule.ws.RuleQueryFactory;
-import org.sonar.server.user.UserSession;
import static org.sonar.server.rule.ws.SearchAction.defineRuleSearchParameters;
private final QProfileService profileService;
private final RuleQueryFactory ruleQueryFactory;
private final I18n i18n;
- private final UserSession userSession;
- public BulkRuleActivationActions(QProfileService profileService, RuleQueryFactory ruleQueryFactory, I18n i18n, UserSession userSession) {
+ public BulkRuleActivationActions(QProfileService profileService, RuleQueryFactory ruleQueryFactory, I18n i18n) {
this.profileService = profileService;
this.ruleQueryFactory = ruleQueryFactory;
this.i18n = i18n;
- this.userSession = userSession;
}
void define(WebService.NewController controller) {
defineProfileKeyParameter(deactivate);
}
- private void defineProfileKeyParameter(WebService.NewAction action) {
+ private static void defineProfileKeyParameter(WebService.NewAction action) {
action.createParam(PROFILE_KEY)
.setDescription("Quality Profile Key. To retrieve a profile key for a given language please see the api/qprofiles documentation")
.setRequired(true)
JsonWriter json = response.newJsonWriter().beginObject();
json.prop("succeeded", result.countSucceeded());
json.prop("failed", result.countFailed());
- result.getErrors().writeJsonAsWarnings(json, i18n, userSession.locale());
+ result.getErrors().writeJson(json, i18n);
json.endObject().close();
}
}
import java.util.Collections;
import java.util.List;
-import java.util.Locale;
import java.util.Set;
import org.sonar.api.security.DefaultGroups;
import org.sonar.core.permission.GlobalPermissions;
return false;
}
- @Override
- public Locale locale() {
- return Locale.getDefault();
- }
-
@Override
public boolean hasPermission(String globalPermission) {
return true;
oldUserSession = threadLocalUserSession.hasSession() ? threadLocalUserSession.get() : null;
threadLocalUserSession.set(new PrivilegedUserSession());
}
-
+
private void stop() {
threadLocalUserSession.unload();
if (oldUserSession != null) {
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
return userDto != null;
}
- @Override
- public Locale locale() {
- return Locale.ENGLISH;
- }
-
@Override
public boolean isRoot() {
return userDto != null && userDto.isRoot();
permissionsByOrganizationUuid.putAll(organizationUuid, permissions);
}
return permissions.contains(permission);
- }
+ }
private Set<String> loadOrganizationPermissions(String organizationUuid) {
try (DbSession dbSession = dbClient.openSession(false)) {
import java.util.Collection;
import java.util.List;
-import java.util.Locale;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.server.exceptions.UnauthorizedException;
return get().isLoggedIn();
}
- @Override
- public Locale locale() {
- return get().locale();
- }
-
@Override
public boolean isRoot() {
return get().isRoot();
import java.util.Collection;
import java.util.List;
-import java.util.Locale;
import java.util.Set;
import javax.annotation.CheckForNull;
boolean isLoggedIn();
- /**
- * @deprecated Always returning {@link Locale#ENGLISH}
- */
- @Deprecated
- Locale locale();
-
boolean isRoot();
/**
/**
* Does the user have the given project permission for a component uuid ?
-
+
* First, check if the user has the global permission (even if the component doesn't exist)
* If not, check is the user has the permission on the project of the component
* If the component doesn't exist, return false
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.List;
-import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.server.user.UserUpdater;
import org.sonarqube.ws.client.user.CreateRequest;
+import static java.lang.String.format;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_EMAIL;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_LOGIN;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_NAME;
private final DbClient dbClient;
private final UserUpdater userUpdater;
- private final I18n i18n;
private final UserSession userSession;
private final UserJsonWriter userWriter;
- public CreateAction(DbClient dbClient, UserUpdater userUpdater, I18n i18n, UserSession userSession, UserJsonWriter userWriter) {
+ public CreateAction(DbClient dbClient, UserUpdater userUpdater, UserSession userSession, UserJsonWriter userWriter) {
this.dbClient = dbClient;
this.userUpdater = userUpdater;
- this.i18n = i18n;
this.userSession = userSession;
this.userWriter = userWriter;
}
userWriter.write(json, user, ImmutableSet.of(), UserJsonWriter.FIELDS);
}
- private void writeReactivationMessage(JsonWriter json, String login) {
+ private static void writeReactivationMessage(JsonWriter json, String login) {
json.name("infos").beginArray();
json.beginObject();
- String text = i18n.message(userSession.locale(), "user.reactivated", "user.reactivated", login);
+ String text = format("The user '%s' has been reactivated", login);
json.prop("msg", text);
json.endObject();
json.endArray();
try {
json.beginObject();
- errors.writeJson(json, i18n, userSession.locale());
+ errors.writeJson(json, i18n);
json.endObject();
} finally {
// TODO if close() fails, the runtime exception should not hide the
package org.sonar.server.component.ws;
import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbTester;
import org.sonar.server.ws.WsTester;
import static org.mockito.Mockito.mock;
+import static org.sonar.api.measures.CoreMetrics.COVERAGE_KEY;
+import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY;
+import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
+import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
+import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
+import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
+import static org.sonar.api.measures.CoreMetrics.getMetric;
public class AppActionTest {
@Test
public void file_with_measures() throws Exception {
insertComponentsAndAnalysis();
- insertFileMeasure(metricsByKey.get(CoreMetrics.LINES_KEY).getId(), 200d, null);
- insertFileMeasure(metricsByKey.get(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY).getId(), 7.4, null);
- insertFileMeasure(metricsByKey.get(CoreMetrics.SQALE_RATING_KEY).getId(), null, "C");
- insertFileMeasure(metricsByKey.get(CoreMetrics.TECHNICAL_DEBT_KEY).getId(), 182d, null);
- insertFileMeasure(metricsByKey.get(CoreMetrics.SQALE_DEBT_RATIO_KEY).getId(), 35d, null);
- insertFileMeasure(metricsByKey.get(CoreMetrics.COVERAGE_KEY).getId(), 95.4d, null);
+ insertFileMeasure(metricsByKey.get(LINES_KEY).getId(), 200d, null);
+ insertFileMeasure(metricsByKey.get(DUPLICATED_LINES_DENSITY_KEY).getId(), 7.4, null);
+ insertFileMeasure(metricsByKey.get(SQALE_RATING_KEY).getId(), null, "C");
+ insertFileMeasure(metricsByKey.get(TECHNICAL_DEBT_KEY).getId(), 182d, null);
+ insertFileMeasure(metricsByKey.get(SQALE_DEBT_RATIO_KEY).getId(), 35d, null);
+ insertFileMeasure(metricsByKey.get(COVERAGE_KEY).getId(), 95.4d, null);
dbTester.commit();
userSessionRule
.login("john")
- .setLocale(Locale.ENGLISH)
.addComponentUuidPermission(UserRole.USER, PROJECT_UUID, FILE_UUID);
WsTester.TestRequest request = wsTester.newGetRequest("api/components", "app").setParam("uuid", FILE_UUID);
request.execute().assertJson(getClass(), "app_with_measures.json");
@Test
public void file_with_coverage() throws Exception {
insertComponentsAndAnalysis();
- insertFileMeasure(metricsByKey.get(CoreMetrics.COVERAGE_KEY).getId(), 95.4, null);
+ insertFileMeasure(metricsByKey.get(COVERAGE_KEY).getId(), 95.4, null);
dbTester.commit();
userSessionRule.login("john").addComponentUuidPermission(UserRole.USER, PROJECT_UUID, FILE_UUID);
private void insertMetrics() {
metricsByKey = new HashMap<>();
for (String metricKey : AppAction.METRIC_KEYS) {
- MetricDto dto = RegisterMetrics.MetricToDto.INSTANCE.apply(CoreMetrics.getMetric(metricKey));
+ MetricDto dto = RegisterMetrics.MetricToDto.INSTANCE.apply(getMetric(metricKey));
dbTester.getDbClient().metricDao().insert(dbTester.getSession(), dto);
metricsByKey.put(metricKey, dto);
}
controller = new WsTester(new QProfilesWs(
new RuleActivationActions(profileService),
- new BulkRuleActivationActions(profileService, null, i18n, userSessionRule),
+ new BulkRuleActivationActions(profileService, null, i18n),
new AddProjectAction(projectAssociationParameters, null, null, dbClient),
new RemoveProjectAction(projectAssociationParameters, null, null, dbClient),
new CreateAction(null, null, null, languages, importers, userSessionRule, null),
assertThat(restore.params()).hasSize(1);
}
- public void define_bulk_activate_rule_action() {
- WebService.Action restoreProfiles = controller.action(BulkRuleActivationActions.BULK_ACTIVATE_ACTION);
- assertThat(restoreProfiles).isNotNull();
- assertThat(restoreProfiles.isPost()).isTrue();
- assertThat(restoreProfiles.params()).hasSize(20);
- }
-
@Test
public void define_bulk_deactivate_rule_action() {
WebService.Action restoreProfiles = controller.action(BulkRuleActivationActions.BULK_DEACTIVATE_ACTION);
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import java.util.Locale;
import java.util.Map;
-import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.security.DefaultGroups;
public abstract class AbstractMockUserSession<T extends AbstractMockUserSession> extends AbstractUserSession {
private final Class<T> clazz;
- private Locale locale = Locale.ENGLISH;
private Set<String> userGroups = Sets.newHashSet(DefaultGroups.ANYONE);
private List<String> globalPermissions = Collections.emptyList();
private HashMultimap<String, String> projectKeyByPermission = HashMultimap.create();
return clazz.cast(this);
}
- @Override
- public Locale locale() {
- return this.locale;
- }
-
- public T setLocale(Locale locale) {
- this.locale = Objects.requireNonNull(locale);
- return clazz.cast(this);
- }
-
/**
* Deprecated, please use {@link #addProjectUuidPermissions}
*/
import com.google.common.base.Preconditions;
import java.util.Collection;
import java.util.List;
-import java.util.Locale;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
return this;
}
- public UserSessionRule setLocale(@Nullable Locale l) {
- ensureAbstractMockUserSession().setLocale(l);
- return this;
- }
-
public UserSessionRule addComponentUuidPermission(String projectPermission, String projectUuid, String componentUuid) {
ensureAbstractMockUserSession().addComponentUuidPermission(projectPermission, projectUuid, componentUuid);
return this;
return currentUserSession.isLoggedIn();
}
- @Override
- public Locale locale() {
- return currentUserSession.locale();
- }
-
@Override
public boolean isRoot() {
return currentUserSession.isRoot();
*/
package org.sonar.server.user;
-import java.util.Locale;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@Test
public void get_session_for_user() {
- AbstractMockUserSession expected = new MockUserSession("karadoc").setUserId(123).setLocale(Locale.FRENCH);
+ AbstractMockUserSession expected = new MockUserSession("karadoc").setUserId(123);
threadLocalUserSession.set(expected);
UserSession session = threadLocalUserSession.get();
assertThat(threadLocalUserSession.getUserId()).isEqualTo(123);
assertThat(threadLocalUserSession.getLogin()).isEqualTo("karadoc");
assertThat(threadLocalUserSession.isLoggedIn()).isTrue();
- assertThat(threadLocalUserSession.locale()).isEqualTo(Locale.FRENCH);
}
@Test
assertThat(threadLocalUserSession.getLogin()).isNull();
assertThat(threadLocalUserSession.getUserId()).isNull();
assertThat(threadLocalUserSession.isLoggedIn()).isFalse();
- // default locale
- assertThat(threadLocalUserSession.locale()).isEqualTo(Locale.ENGLISH);
}
@Test
*/
package org.sonar.server.user.ws;
-import java.util.Locale;
import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import static org.sonar.db.user.UserTesting.newUserDto;
public class CreateActionTest {
userIndexer = new UserIndexer(system2, db.getDbClient(), esTester.client());
index = new UserIndex(esTester.client());
DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- tester = new WsTester(new UsersWs(new CreateAction(db.getDbClient(),
- new UserUpdater(mock(NewUserNotifier.class), settings, db.getDbClient(), userIndexer, system2, defaultOrganizationProvider),
- i18n, userSessionRule, new UserJsonWriter(userSessionRule))));
+ tester = new WsTester(new UsersWs(
+ new CreateAction(db.getDbClient(),
+ new UserUpdater(mock(NewUserNotifier.class), settings, db.getDbClient(), userIndexer, system2, defaultOrganizationProvider),
+ userSessionRule, new UserJsonWriter(userSessionRule))));
}
@Test
@Test
public void reactivate_user() throws Exception {
- userSessionRule.login("admin").setLocale(Locale.FRENCH).setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ userSessionRule.login("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
db.users().insertUser(newUserDto("john", "John", "john@email.com"));
db.getDbClient().userDao().deactivateUserByLogin(db.getSession(), "john");
db.commit();
userIndexer.index();
- when(i18n.message(Locale.FRENCH, "user.reactivated", "user.reactivated", "john")).thenReturn("The user 'john' has been reactivated.");
tester.newPostRequest("api/users", "create")
.setParam("login", "john")
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.api.i18n.I18n;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.server.tester.UserSessionRule;
@Before
public void setUp() {
WsTester tester = new WsTester(new UsersWs(
- new CreateAction(mock(DbClient.class), mock(UserUpdater.class), mock(I18n.class), userSessionRule, mock(UserJsonWriter.class)),
+ new CreateAction(mock(DbClient.class), mock(UserUpdater.class), userSessionRule, mock(UserJsonWriter.class)),
new UpdateAction(mock(UserUpdater.class), userSessionRule, mock(UserJsonWriter.class), mock(DbClient.class)),
new CurrentAction(userSessionRule, mock(org.sonar.db.DbClient.class)),
new ChangePasswordAction(mock(UserUpdater.class), userSessionRule),
@Test
public void bad_request_with_i18n_message() {
- userSessionRule.setLocale(Locale.ENGLISH);
ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail_with_i18n_message").setParam("count", "3");
DumbResponse response = new DumbResponse();
when(i18n.message(Locale.ENGLISH, "bad.request.reason", "bad.request.reason", 0)).thenReturn("reason #0");
@Test
public void bad_request_with_multiple_i18n_messages() {
- userSessionRule.setLocale(Locale.ENGLISH);
-
ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail_with_multiple_i18n_messages").setParam("count", "3");
DumbResponse response = new DumbResponse();
when(i18n.message(Locale.ENGLISH, "bad.request.reason", "bad.request.reason", 0)).thenReturn("reason #0");
},
"infos": [
{
- "msg": "The user 'john' has been reactivated."
+ "msg": "The user 'john' has been reactivated"
}
]
}