summaryrefslogtreecommitdiff
path: root/app/models/namespace.rb
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-02-06 17:16:50 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2017-02-08 11:59:01 +0200
commitf642a4608d99b1344d862134900c2e078eda9cfc (patch)
tree96e10f2af7a6279afa3498136eb5f728d9f5ab2b /app/models/namespace.rb
parent99fceff4f9e98cc7cb725882abec6e5b7c9b0170 (diff)
downloadgitlab-ce-f642a4608d99b1344d862134900c2e078eda9cfc.tar.gz
Limit level of nesting for groupsdz-limit-nested-groups
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/models/namespace.rb')
-rw-r--r--app/models/namespace.rb15
1 files changed, 14 insertions, 1 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 2fb2eb44aaa..0d2600fe3bc 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -7,6 +7,11 @@ class Namespace < ActiveRecord::Base
include Gitlab::CurrentSettings
include Routable
+ # Prevent users from creating unreasonably deep level of nesting.
+ # The number 20 was taken based on maximum nesting level of
+ # Android repo (15) + some extra backup.
+ NUMBER_OF_ANCESTORS_ALLOWED = 20
+
cache_markdown_field :description, pipeline: :description
has_many :projects, dependent: :destroy
@@ -29,6 +34,8 @@ class Namespace < ActiveRecord::Base
length: { maximum: 255 },
namespace: true
+ validate :nesting_level_allowed
+
delegate :name, to: :owner, allow_nil: true, prefix: true
after_update :move_dir, if: :path_changed?
@@ -194,7 +201,7 @@ class Namespace < ActiveRecord::Base
# Scopes the model on ancestors of the record
def ancestors
if parent_id
- path = route.path
+ path = route ? route.path : full_path
paths = []
until path.blank?
@@ -270,4 +277,10 @@ class Namespace < ActiveRecord::Base
path_was
end
end
+
+ def nesting_level_allowed
+ if ancestors.count > Group::NUMBER_OF_ANCESTORS_ALLOWED
+ errors.add(:parent_id, "has too deep level of nesting")
+ end
+ end
end