]> source.dussan.org Git - redmine.git/commitdiff
Be more conservative when fetching constants in Repository.factory (#23758).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 4 Sep 2016 17:10:12 +0000 (17:10 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 4 Sep 2016 17:10:12 +0000 (17:10 +0000)
Patch by Holger Just.

git-svn-id: http://svn.redmine.org/redmine/trunk@15816 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/repository.rb
test/unit/repository_test.rb

index 5f2f118b7257eb615de0959582c251720ea851ac..6da088541ec6c9a79894aa19369eeeb690f5e8c6 100644 (file)
@@ -363,10 +363,14 @@ class Repository < ActiveRecord::Base
   end
 
   def self.factory(klass_name, *args)
-    klass = "Repository::#{klass_name}".constantize
-    klass.new(*args)
-  rescue
-    nil
+    repository_class(klass_name).new(*args) rescue nil
+  end
+
+  def self.repository_class(class_name)
+    class_name = class_name.to_s.classify
+    if Redmine::Scm::Base.all.include?(class_name)
+      "Repository::#{class_name}".constantize
+    end
   end
 
   def self.scm_adapter_class
index 238c29d6469e234bd0025810d75ac07e5d33ed0e..da3273ead10e75fb94e3b269f570a0a93fe2176a 100644 (file)
@@ -496,4 +496,18 @@ class RepositoryTest < ActiveSupport::TestCase
     Repository::Subversion.any_instance.expects(:fetch_changesets).twice.returns(true)
     Repository.fetch_changesets
   end
+
+  def test_repository_class
+    assert_equal Repository::Subversion, Repository.repository_class('Subversion')
+    assert_equal Repository::Git, Repository.repository_class('Git')
+    assert_nil Repository.factory('Serializer')
+    assert_nil Repository.factory('Query')
+  end
+
+  def test_factory
+    assert_instance_of Repository::Subversion, Repository.factory('Subversion')
+    assert_instance_of Repository::Git, Repository.factory('Git')
+    assert_nil Repository.factory('Serializer')
+    assert_nil Repository.factory('Query')
+  end
 end