import org.sonar.server.plugins.privileged.PrivilegedPluginsBootstraper;
import org.sonar.server.plugins.privileged.PrivilegedPluginsStopper;
import org.sonar.server.property.InternalPropertiesImpl;
-import org.sonar.server.qualityprofile.BuiltInProfiles;
import org.sonar.server.qualityprofile.QProfileComparison;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileProjectLookup;
QProfileProjectOperations.class,
QProfileProjectLookup.class,
QProfileComparison.class,
- BuiltInProfiles.class,
-
+
// rule
RuleIndexer.class,
AnnotationRuleParser.class,
assertThat(picoContainer.getComponentAdapters())
.hasSize(
CONTAINER_ITSELF
- + 77 // level 4
+ + 76 // level 4
+ 4 // content of CeConfigurationModule
+ 3 // content of CeHttpModule
+ 5 // content of CeQueueModule
import org.sonar.server.projectlink.ws.ProjectLinksModule;
import org.sonar.server.property.InternalPropertiesImpl;
import org.sonar.server.qualitygate.QualityGateModule;
-import org.sonar.server.qualityprofile.BuiltInProfiles;
import org.sonar.server.qualityprofile.QProfileBackuper;
import org.sonar.server.qualityprofile.QProfileComparison;
import org.sonar.server.qualityprofile.QProfileCopier;
QProfileProjectOperations.class,
QProfileProjectLookup.class,
QProfileComparison.class,
- BuiltInProfiles.class,
SearchDataLoader.class,
ProfilesWs.class,
OldRestoreAction.class,
+++ /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.server.qualityprofile;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.Multimap;
-import java.util.Collection;
-import org.sonar.api.ce.ComputeEngineSide;
-import org.sonar.api.server.ServerSide;
-
-/**
- * Used to list default profile names.
- *
- * It should be removed as soon as a new API to define quality profiles is created. Currently loading all definitions
- * just to get the profile names is too slow (see {@link org.sonar.api.profiles.ProfileDefinition}).
- */
-@ServerSide
-@ComputeEngineSide
-public class BuiltInProfiles {
-
- // built-in profile names grouped by language
- private final Multimap<String, String> namesByLang;
-
- public BuiltInProfiles() {
- this.namesByLang = ArrayListMultimap.create();
- }
-
- Collection<String> byLanguage(String language) {
- return namesByLang.get(language);
- }
-
- BuiltInProfiles put(String language, String name) {
- namesByLang.put(language, name);
- return this;
- }
-
-}
private final QProfileFactory factory;
private final RuleActivator activator;
private final ActiveRuleIndexer activeRuleIndexer;
- private final BuiltInProfiles builtInProfiles;
private final ProfileDefinition[] definitions;
- public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, BuiltInProfiles builtInProfiles, QProfileFactory factory,
+ public QProfileReset(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory,
ProfileDefinition[] definitions) {
this.db = db;
this.activator = activator;
this.activeRuleIndexer = activeRuleIndexer;
- this.builtInProfiles = builtInProfiles;
this.factory = factory;
this.definitions = definitions;
}
- public QProfileReset(DbClient db, RuleActivator activator, BuiltInProfiles builtInProfiles, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
- this(db, activator, activeRuleIndexer, builtInProfiles, factory, new ProfileDefinition[0]);
- }
-
- public Collection<String> builtInProfileNamesForLanguage(String language) {
- return builtInProfiles.byLanguage(language);
+ public QProfileReset(DbClient db, RuleActivator activator, QProfileFactory factory, ActiveRuleIndexer activeRuleIndexer) {
+ this(db, activator, activeRuleIndexer, factory, new ProfileDefinition[0]);
}
/**
* Reset built-in profiles for the given language. Missing profiles are created and
* existing ones are updated.
*/
- void resetLanguage(String language) {
+ public void resetLanguage(String language) {
DbSession dbSession = db.openSession(false);
try {
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
*/
package org.sonar.server.qualityprofile;
-import java.io.Reader;
-import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
private final RuleActivator ruleActivator;
private final QProfileFactory factory;
private final QProfileBackuper backuper;
- private final QProfileReset reset;
private final UserSession userSession;
public QProfileService(DbClient db, ActiveRuleIndexer activeRuleIndexer, RuleActivator ruleActivator, QProfileFactory factory,
- QProfileBackuper backuper, QProfileReset reset, UserSession userSession) {
+ QProfileBackuper backuper, UserSession userSession) {
this.db = db;
this.activeRuleIndexer = activeRuleIndexer;
this.ruleActivator = ruleActivator;
this.factory = factory;
this.backuper = backuper;
- this.reset = reset;
this.userSession = userSession;
}
return output.toString();
}
- public void restore(Reader backup) {
- verifyAdminPermission();
- backuper.restore(backup, null);
- }
-
- /**
- * @deprecated used only by Ruby on Rails. Use {@link #restore(java.io.Reader)}
- */
- @Deprecated
- public void restore(String backup) {
- restore(new StringReader(backup));
- }
-
- public void restoreBuiltInProfilesForLanguage(String lang) {
- verifyAdminPermission();
- reset.resetLanguage(lang);
- }
-
- public void delete(String key) {
- verifyAdminPermission();
- DbSession session = db.openSession(false);
- try {
- List<ActiveRuleChange> changes = factory.delete(session, key, false);
- session.commit();
- activeRuleIndexer.index(changes);
- } finally {
- db.closeSession(session);
- }
- }
-
- public void rename(String key, String newName) {
- verifyAdminPermission();
- factory.rename(key, newName);
- }
-
- /**
- * Set the given quality profile as default for the related language
- */
- public void setDefault(String key) {
- verifyAdminPermission();
- factory.setDefault(key);
- }
-
/**
* Used in /api/profiles and in /profiles/export
*/
private static final String DEFAULT_PROFILE_NAME = "Sonar way";
private final List<ProfileDefinition> definitions;
- private final BuiltInProfiles builtInProfiles;
private final DbClient dbClient;
private final QProfileFactory profileFactory;
private final RuleActivator ruleActivator;
/**
* To be kept when no ProfileDefinition are injected
*/
- public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
- DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
- this(builtInProfiles, dbClient, profileFactory, ruleActivator, Collections.<ProfileDefinition>emptyList(), languages, activeRuleIndexer);
+ public RegisterQualityProfiles(DbClient dbClient,
+ QProfileFactory profileFactory, RuleActivator ruleActivator, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
+ this(dbClient, profileFactory, ruleActivator, Collections.emptyList(), languages, activeRuleIndexer);
}
- public RegisterQualityProfiles(BuiltInProfiles builtInProfiles,
- DbClient dbClient, QProfileFactory profileFactory, RuleActivator ruleActivator,
+ public RegisterQualityProfiles(DbClient dbClient,
+ QProfileFactory profileFactory, RuleActivator ruleActivator,
List<ProfileDefinition> definitions, Languages languages, ActiveRuleIndexer activeRuleIndexer) {
- this.builtInProfiles = builtInProfiles;
this.dbClient = dbClient;
this.profileFactory = profileFactory;
this.ruleActivator = ruleActivator;
if (shouldRegister(profileName, session)) {
changes.addAll(register(profileName, entry.getValue(), session));
}
- builtInProfiles.put(language, name);
}
setDefault(language, defs, session);
session.commit();
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.qualityprofile.QProfileService;
+import org.sonar.core.permission.GlobalPermissions;
+import org.sonar.server.qualityprofile.QProfileReset;
+import org.sonar.server.user.UserSession;
import static org.sonar.server.util.LanguageParamUtils.getExampleValue;
import static org.sonar.server.util.LanguageParamUtils.getLanguageKeys;
public class RestoreBuiltInAction implements QProfileWsAction {
- private final QProfileService service;
+ private final QProfileReset reset;
private final Languages languages;
+ private final UserSession userSession;
- public RestoreBuiltInAction(QProfileService service, Languages languages) {
- this.service = service;
+ public RestoreBuiltInAction(QProfileReset reset, Languages languages, UserSession userSession) {
+ this.reset = reset;
this.languages = languages;
+ this.userSession = userSession;
}
@Override
@Override
public void handle(Request request, Response response) {
+ verifyAdminPermission();
+
String language = request.mandatoryParam("language");
- service.restoreBuiltInProfilesForLanguage(language);
+ reset.resetLanguage(language);
response.noContent();
}
+ private void verifyAdminPermission() {
+ userSession.checkLoggedIn();
+ userSession.checkPermission(GlobalPermissions.QUALITY_PROFILE_ADMIN);
+ }
}
+++ /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.server.qualityprofile;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-
-public class BuiltInProfilesTest {
-
- BuiltInProfiles cache;
-
- @Before
- public void setUp() {
- cache = new BuiltInProfiles();
- }
-
- @Test
- public void add_profiles() {
- cache.put("java", "Default");
- cache.put("java", "Sonar Way");
- cache.put("js", "Default");
-
- assertThat(cache.byLanguage("java")).containsOnly("Default", "Sonar Way");
- assertThat(cache.byLanguage("js")).containsOnly("Default");
- }
-
- @Test
- public void not_add_same_profile_name() {
- cache.put("java", "Default");
- cache.put("java", "Default");
-
- assertThat(cache.byLanguage("java")).containsOnly("Default");
- }
-}
new RemoveProjectAction(projectAssociationParameters, null, null, null),
new CreateAction(null, null, null, languages, importers, userSessionRule, null),
new ImportersAction(importers),
- new RestoreBuiltInAction(null, languages),
+ new RestoreBuiltInAction(null, languages, userSessionRule),
new SearchAction(null, languages),
new SetDefaultAction(languages, null, null, userSessionRule),
new ProjectsAction(null, userSessionRule),
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Languages;
+import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.language.LanguageTesting;
-import org.sonar.server.qualityprofile.QProfileService;
+import org.sonar.server.qualityprofile.QProfileReset;
+import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestResponse;
import org.sonar.server.ws.WsActionTester;
import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
public class RestoreBuiltInActionTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
- QProfileService profileService = mock(QProfileService.class);
- Languages languages = LanguageTesting.newLanguages("xoo");
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone();
+
+ private QProfileReset reset = mock(QProfileReset.class);
+ private Languages languages = LanguageTesting.newLanguages("xoo");
- WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(profileService, languages));
+ private WsActionTester tester = new WsActionTester(new RestoreBuiltInAction(reset, languages, userSession));
@Test
public void return_empty_result_when_no_info_or_warning() {
+ userSession.login("himself").setGlobalPermissions(QUALITY_PROFILE_ADMIN);
TestResponse response = tester.newRequest().setParam("language", "xoo").execute();
- verify(profileService).restoreBuiltInProfilesForLanguage("xoo");
+ verify(reset).resetLanguage("xoo");
assertThat(response.getStatus()).isEqualTo(204);
}
@Test
public void fail_on_unknown_language() throws Exception {
+ userSession.login("himself").setGlobalPermissions(QUALITY_PROFILE_ADMIN);
expectedException.expect(IllegalArgumentException.class);
tester.newRequest().setParam("language", "unknown").execute();
}
+
+ @Test
+ public void fail_if_not_profile_administrator() throws Exception {
+ expectedException.expect(UnauthorizedException.class);
+ tester.newRequest().setParam("language", "xoo").execute();
+ }
}