From: Simon Brandhof Date: Wed, 12 Sep 2012 15:29:47 +0000 (+0200) Subject: SONAR-3773 keep the existing default roles of views during migration X-Git-Tag: 3.3~303 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6fcdb3a6c635e6baf06489687a20c95b6c49795c;p=sonarqube.git SONAR-3773 keep the existing default roles of views during migration --- diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql index 913e1ff6d9c..1dc3d34d0e2 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/rows-h2.sql @@ -10,6 +10,18 @@ ALTER TABLE GROUP_ROLES ALTER COLUMN ID RESTART WITH 2; INSERT INTO GROUPS_USERS(USER_ID, GROUP_ID) VALUES (1, 1); INSERT INTO GROUPS_USERS(USER_ID, GROUP_ID) VALUES (1, 2); +-- see migration 320 +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (1, 'sonar.role.admin.TRK.defaultGroups', NULL, 'sonar-administrators', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (2, 'sonar.role.user.TRK.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (3, 'sonar.role.codeviewer.TRK.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (4, 'sonar.role.admin.VW.defaultGroups', NULL, 'sonar-administrators', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (5, 'sonar.role.user.VW.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (6, 'sonar.role.codeviewer.VW.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (7, 'sonar.role.admin.SVW.defaultGroups', NULL, 'sonar-administrators', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (8, 'sonar.role.user.SVW.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +INSERT INTO PROPERTIES(ID, prop_key, resource_id, text_value, user_id) VALUES (9, 'sonar.role.codeviewer.SVW.defaultGroups', NULL, 'Anyone,sonar-users', NULL); +ALTER TABLE PROPERTIES ALTER COLUMN ID RESTART WITH 10; + INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('2'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('10'); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index a995d5b8685..ed12de72e04 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -19,11 +19,10 @@ */ package org.sonar.server.platform; -import org.sonar.api.config.EmailSettings; - import org.apache.commons.configuration.BaseConfiguration; import org.slf4j.LoggerFactory; import org.sonar.api.Plugins; +import org.sonar.api.config.EmailSettings; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.Server; import org.sonar.api.profiles.AnnotationProfileParser; @@ -45,11 +44,7 @@ import org.sonar.core.i18n.I18nManager; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.core.metric.DefaultMetricFinder; import org.sonar.core.notification.DefaultNotificationManager; -import org.sonar.core.persistence.DaoUtils; -import org.sonar.core.persistence.DatabaseMigrator; -import org.sonar.core.persistence.DatabaseVersion; -import org.sonar.core.persistence.DefaultDatabase; -import org.sonar.core.persistence.MyBatis; +import org.sonar.core.persistence.*; import org.sonar.core.qualitymodel.DefaultModelFinder; import org.sonar.core.rule.DefaultRuleFinder; import org.sonar.core.user.DefaultUserFinder; @@ -69,13 +64,7 @@ import org.sonar.server.database.EmbeddedDatabaseFactory; import org.sonar.server.filters.FilterExecutor; import org.sonar.server.notifications.NotificationService; import org.sonar.server.notifications.reviews.ReviewsNotificationManager; -import org.sonar.server.plugins.ApplicationDeployer; -import org.sonar.server.plugins.DefaultServerPluginRepository; -import org.sonar.server.plugins.PluginDeployer; -import org.sonar.server.plugins.PluginDownloader; -import org.sonar.server.plugins.ServerExtensionInstaller; -import org.sonar.server.plugins.UpdateCenterClient; -import org.sonar.server.plugins.UpdateCenterMatrixFactory; +import org.sonar.server.plugins.*; import org.sonar.server.qualitymodel.DefaultModelManager; import org.sonar.server.rules.ProfilesConsole; import org.sonar.server.rules.RulesConsole; @@ -257,7 +246,6 @@ public final class Platform { startupContainer.addSingleton(GeneratePluginIndex.class); startupContainer.addSingleton(RegisterNewFilters.class); startupContainer.addSingleton(RegisterNewDashboards.class); - startupContainer.addSingleton(SetDefaultProjectPermissions.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java b/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java deleted file mode 100644 index ce589a79bf9..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.server.startup; - -import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; -import org.sonar.server.platform.PersistentSettings; - -import java.util.Map; - -/** - * @since 3.2 - */ -public class SetDefaultProjectPermissions { - private static final String SONAR_ADMINISTRATORS = "sonar-administrators"; - private static final String ANYONE_AND_USERS = "Anyone,sonar-users"; - private static final String SUFFIX = ".defaultGroups"; - - private final PersistentSettings persistentSettings; - - public SetDefaultProjectPermissions(PersistentSettings persistentSettings) { - this.persistentSettings = persistentSettings; - } - - public void start() { - Map props = Maps.newHashMap(); - props.putAll(missingProperties("TRK")); - - // Support old versions of Views plugin - props.putAll(missingProperties("VW")); - props.putAll(missingProperties("SVW")); - - if (!props.isEmpty()) { - LoggerFactory.getLogger(SetDefaultProjectPermissions.class).info("Set default project roles"); - persistentSettings.saveProperties(props); - } - } - - private Map missingProperties(String qualifier) { - Map props = Maps.newHashMap(); - if (StringUtils.isBlank(persistentSettings.getSettings().getString("sonar.role.user." + qualifier + SUFFIX))) { - completeDefaultRoles(qualifier, props); - } - return props; - } - - private void completeDefaultRoles(String qualifier, Map props) { - props.put("sonar.role.admin." + qualifier + SUFFIX, SONAR_ADMINISTRATORS); - props.put("sonar.role.user." + qualifier + SUFFIX, ANYONE_AND_USERS); - props.put("sonar.role.codeviewer." + qualifier + SUFFIX, ANYONE_AND_USERS); - } -} diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/320_move_default_roles.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/320_move_default_roles.rb index 8635e4d3890..c3a48292f15 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/320_move_default_roles.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/320_move_default_roles.rb @@ -50,6 +50,11 @@ class MoveDefaultRoles < ActiveRecord::Migration # upgrade from version < 3.2. move_groups move_users + else + # fresh install + create_default_roles('TRK') + create_default_roles('VW') + create_default_roles('SVW') end end @@ -76,6 +81,8 @@ class MoveDefaultRoles < ActiveRecord::Migration groups_per_role.each_pair do |role, groups| Property.create(:prop_key => "sonar.role.#{role}.TRK.defaultGroups", :text_value => groups.join(',')) + Property.create(:prop_key => "sonar.role.#{role}.VW.defaultGroups", :text_value => groups.join(',')) + Property.create(:prop_key => "sonar.role.#{role}.SVW.defaultGroups", :text_value => groups.join(',')) end GroupRole.delete_all ['role like ?', 'default-%'] @@ -96,8 +103,17 @@ class MoveDefaultRoles < ActiveRecord::Migration users_per_role.each_pair do |role, users| Property.create(:prop_key => "sonar.role.#{role}.TRK.defaultUsers", :text_value => users.join(',')) + Property.create(:prop_key => "sonar.role.#{role}.VW.defaultUsers", :text_value => users.join(',')) + Property.create(:prop_key => "sonar.role.#{role}.SVW.defaultUsers", :text_value => users.join(',')) end UserRole.delete_all ['role like ?', 'default-%'] end + + def self.create_default_roles(qualifier) + Property.create(:prop_key => "sonar.role.admin.#{qualifier}.defaultGroups", :text_value => 'sonar-administrators') + Property.create(:prop_key => "sonar.role.user.#{qualifier}.defaultGroups", :text_value => 'Anyone,sonar-users') + Property.create(:prop_key => "sonar.role.codeviewer.#{qualifier}.defaultGroups", :text_value => 'Anyone,sonar-users') + end + end diff --git a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java deleted file mode 100644 index 3139bb6b192..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar 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. - * - * Sonar 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 Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.server.startup; - -import org.junit.Test; -import org.mockito.ArgumentMatcher; -import org.sonar.api.config.Settings; -import org.sonar.server.platform.PersistentSettings; - -import java.util.Map; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class SetDefaultProjectPermissionsTest { - @Test - public void set_default_permissions_if_none() { - PersistentSettings persistentSettings = mock(PersistentSettings.class); - Settings settings = new Settings(); - when(persistentSettings.getSettings()).thenReturn(settings); - - new SetDefaultProjectPermissions(persistentSettings).start(); - - verify(persistentSettings).saveProperties(argThat(new ArgumentMatcher>() { - @Override - public boolean matches(Object o) { - Map map = (Map) o; - return map.size()>1 && map.get("sonar.role.admin.TRK.defaultGroups").equals("sonar-administrators"); - } - })); - } - - @Test - public void do_not_set_default_permissions_if_exist() { - PersistentSettings persistentSettings = mock(PersistentSettings.class); - Settings settings = new Settings() - .setProperty("sonar.role.user.TRK.defaultGroups", "custom-group") - .setProperty("sonar.role.user.VW.defaultGroups", "custom-group") - .setProperty("sonar.role.user.SVW.defaultGroups", "custom-group"); - when(persistentSettings.getSettings()).thenReturn(settings); - - new SetDefaultProjectPermissions(persistentSettings).start(); - - verify(persistentSettings, never()).saveProperties(any(Map.class)); - } -}