]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4464 Fixed permission properties migration to better support dev cockpit permis...
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Wed, 17 Jul 2013 12:27:28 +0000 (14:27 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Wed, 17 Jul 2013 12:27:28 +0000 (14:27 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/permission_templates_controller.rb
sonar-server/src/main/webapp/WEB-INF/db/migrate/418_migrate_default_permissions.rb

index 1d9aa4a1bbbf7f6b18da1a9d45c9d9445c5526af..0e1017e1907bd3688f6fc951d25a6dbe60e3c28d 100644 (file)
@@ -182,6 +182,9 @@ class PermissionTemplatesController < ApplicationController
     verify_post_request
     get_root_qualifiers.each do |qualifier|
       Property.set("sonar.permission.template.#{qualifier}.default", params["default_template_#{qualifier}"])
+      if 'TRK' == qualifier
+        Property.set("sonar.permission.template.default", params["default_template_#{qualifier}"])
+      end
     end
     redirect_to :action => 'index'
   end
index 0e2c6239ee282a75f314d664ec7d5a1dbb06622b..c337013e57e6b2f026f25cbc09181d0644f36618 100644 (file)
@@ -77,61 +77,70 @@ class MigrateDefaultPermissions < ActiveRecord::Migration
   def self.migrate_existing_default_permissions
 
     ROOT_QUALIFIERS.keys.each do |qualifier|
-      existing_properties = []
-      existing_properties << Property.find_by_prop_key("sonar.role.admin.#{qualifier}.defaultGroups")
-      existing_properties << Property.find_by_prop_key("sonar.role.user.#{qualifier}.defaultGroups")
-      existing_properties << Property.find_by_prop_key("sonar.role.codeviewer.#{qualifier}.defaultGroups")
-      existing_properties << Property.find_by_prop_key("sonar.role.admin.#{qualifier}.defaultUsers")
-      existing_properties << Property.find_by_prop_key("sonar.role.user.#{qualifier}.defaultUsers")
-      existing_properties << Property.find_by_prop_key("sonar.role.codeviewer.#{qualifier}.defaultUsers")
+      existing_properties = {}
+      existing_properties["sonar.role.admin.#{qualifier}.defaultGroups"] = Property.find_by_prop_key("sonar.role.admin.#{qualifier}.defaultGroups")
+      existing_properties["sonar.role.user.#{qualifier}.defaultGroups"] = Property.find_by_prop_key("sonar.role.user.#{qualifier}.defaultGroups")
+      existing_properties["sonar.role.codeviewer.#{qualifier}.defaultGroups"] = Property.find_by_prop_key("sonar.role.codeviewer.#{qualifier}.defaultGroups")
+      existing_properties["sonar.role.admin.#{qualifier}.defaultUsers"] = Property.find_by_prop_key("sonar.role.admin.#{qualifier}.defaultUsers")
+      existing_properties["sonar.role.user.#{qualifier}.defaultUsers"] = Property.find_by_prop_key("sonar.role.user.#{qualifier}.defaultUsers")
+      existing_properties["sonar.role.codeviewer.#{qualifier}.defaultUsers"] = Property.find_by_prop_key("sonar.role.codeviewer.#{qualifier}.defaultUsers")
 
-      existing_properties.reject! {|prop| prop.nil?}
+      configured_values = existing_properties.values.reject {|prop| prop.nil?}
 
       # Existing properties are migrated only when upgrading an existing SonarQube instance
       # Subviews permissions are not migrated since they are not used
-      if !@is_fresh_install && existing_properties.length > 0 && qualifier != :SVW
-        migrate_existing_permissions(qualifier, existing_properties)
+      if !@is_fresh_install && qualifier != :SVW && !configured_values.empty?
+
+        template_id = migrate_existing_permissions(qualifier, configured_values)
+
+        # If some of the default properties are missing in DB then fallback to SonarQube defaults
+        # This can happen with DevCockpit as the default permissions are not persistent settings
+        if configured_values.length < 6
+          existing_properties.each_pair do |key, value|
+            if value.nil?
+              add_default_setting_for_key(key, template_id)
+            end
+          end
+        end
       end
 
-      delete_existing_default_permissions(existing_properties)
+      delete_existing_default_permissions(configured_values)
+
     end
 
   end
 
   def self.migrate_existing_permissions(qualifier, properties)
 
-    unless properties.empty?
-
-      qualifier_template = PermissionTemplate.create(
-        :name => "Default template for #{ROOT_QUALIFIERS[qualifier]}",
-        :kee => "default_template_for_#{ROOT_QUALIFIERS[qualifier].downcase}",
-        :description => "This template has been automatically created using the previously configured default permissions for #{ROOT_QUALIFIERS[qualifier]}")
-
-      properties.each do |property|
-        key_fields = property.prop_key.split('.')
-        value_fields = property.text_value.split(',')
-        role = key_fields[2]
-        if 'defaultGroups'.eql?(key_fields[4])
-          value_fields.each do |group_name|
-            if 'Anyone'.eql?(group_name) || !Group.find_by_name(group_name).nil?
-              group_id = 'Anyone'.eql?(group_name) ? nil : Group.find_by_name(group_name).id
-              PermissionTemplateGroup.create(:group_id => group_id, :permission_reference => role, :template_id => qualifier_template.id)
-            end
+    qualifier_template = PermissionTemplate.create(
+      :name => "Default template for #{ROOT_QUALIFIERS[qualifier]}",
+      :kee => "default_template_for_#{ROOT_QUALIFIERS[qualifier].downcase}",
+      :description => "This template has been automatically created using the previously configured default permissions for #{ROOT_QUALIFIERS[qualifier]}")
+
+    properties.each do |property|
+      key_fields = property.prop_key.split('.')
+      value_fields = property.text_value.split(',')
+      role = key_fields[2]
+      if 'defaultGroups'.eql?(key_fields[4])
+        value_fields.each do |group_name|
+          if 'Anyone'.eql?(group_name) || !Group.find_by_name(group_name).nil?
+            group_id = 'Anyone'.eql?(group_name) ? nil : Group.find_by_name(group_name).id
+            PermissionTemplateGroup.create(:group_id => group_id, :permission_reference => role, :template_id => qualifier_template.id)
           end
-        else
-          value_fields.each do |user_login|
-            user = User.find_by_login(user_login)
-            unless user.nil?
-              PermissionTemplateUser.create(:user_id => user.id, :permission_reference => role, :template_id => qualifier_template.id)
-            end
+        end
+      else
+        value_fields.each do |user_login|
+          user = User.find_by_login(user_login)
+          unless user.nil?
+            PermissionTemplateUser.create(:user_id => user.id, :permission_reference => role, :template_id => qualifier_template.id)
           end
         end
       end
-
-      Property.create(:prop_key => "sonar.permission.template.#{qualifier}.default", :text_value => qualifier_template.kee)
-
     end
 
+    Property.create(:prop_key => "sonar.permission.template.#{qualifier}.default", :text_value => qualifier_template.kee)
+
+    qualifier_template.id
   end
 
   def self.delete_existing_default_permissions(properties)
@@ -140,4 +149,29 @@ class MigrateDefaultPermissions < ActiveRecord::Migration
     end
   end
 
+  def self.add_default_setting_for_key(key, template_id)
+
+    if key.include?('admin')
+      if(key.include?('defaultGroups'))
+        # admin -> sonar-administrators
+        group_id = Group.find_by_name('sonar-administrators').id
+        PermissionTemplateGroup.create(:group_id => group_id, :permission_reference => 'admin', :template_id => template_id)
+      end
+    elsif key.include?('user')
+      if(key.include?('defaultGroups'))
+        # user -> sonar-users, Anyone
+        group_id = Group.find_by_name('sonar-users').id
+        PermissionTemplateGroup.create(:group_id => group_id, :permission_reference => 'user', :template_id => template_id)
+        PermissionTemplateGroup.create(:group_id => nil, :permission_reference => 'user', :template_id => template_id)
+      end
+    elsif key.include?('codeviewer')
+      if(key.include?('defaultGroups'))
+        # codeviewer -> sonar-users, Anyone
+        group_id = Group.find_by_name('sonar-users').id
+        PermissionTemplateGroup.create(:group_id => group_id, :permission_reference => 'codeviewer', :template_id => template_id)
+        PermissionTemplateGroup.create(:group_id => nil, :permission_reference => 'codeviewer', :template_id => template_id)
+      end
+    end
+  end
+
 end