diff options
author | Douwe Maan <douwe@selenight.nl> | 2018-01-31 10:51:05 -0600 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2018-02-06 12:06:05 -0600 |
commit | 8d69436c901a3eee674e72c67d91de3994d30e0c (patch) | |
tree | c8071b0c0c78f2f711d51999793a2b66d9a9aea9 /app | |
parent | 2d5f10b2a7f05b29ca5869b2d1acb53d5bcbf50b (diff) | |
download | gitlab-ce-8d69436c901a3eee674e72c67d91de3994d30e0c.tar.gz |
Validate user namespace before saving so that errors persist on modeldm-user-namespace-route-path-validation
Diffstat (limited to 'app')
-rw-r--r-- | app/models/namespace.rb | 3 | ||||
-rw-r--r-- | app/models/user.rb | 18 |
2 files changed, 9 insertions, 12 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 06655298950..44c5bb13a59 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -20,6 +20,9 @@ class Namespace < ActiveRecord::Base has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :project_statistics + + # This should _not_ be `inverse_of: :namespace`, because that would also set + # `user.namespace` when this user creates a group with themselves as `owner`. belongs_to :owner, class_name: "User" belongs_to :parent, class_name: "Namespace" diff --git a/app/models/user.rb b/app/models/user.rb index 4996cea718c..1df3772e63f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,7 +77,7 @@ class User < ActiveRecord::Base # # Namespace for personal projects - has_one :namespace, -> { where(type: nil) }, dependent: :destroy, foreign_key: :owner_id, autosave: true # rubocop:disable Cop/ActiveRecordDependent + has_one :namespace, -> { where(type: nil) }, dependent: :destroy, foreign_key: :owner_id, inverse_of: :owner, autosave: true # rubocop:disable Cop/ActiveRecordDependent # Profile has_many :keys, -> do @@ -171,7 +171,7 @@ class User < ActiveRecord::Base before_save :ensure_user_rights_and_limits, if: ->(user) { user.new_record? || user.external_changed? } before_save :skip_reconfirmation!, if: ->(user) { user.email_changed? && user.read_only_attribute?(:email) } before_save :check_for_verified_email, if: ->(user) { user.email_changed? && !user.new_record? } - after_save :ensure_namespace_correct + before_validation :ensure_namespace_correct after_update :username_changed_hook, if: :username_changed? after_destroy :post_destroy_hook after_destroy :remove_key_cache @@ -884,16 +884,10 @@ class User < ActiveRecord::Base end def ensure_namespace_correct - # Ensure user has namespace - create_namespace!(path: username, name: username) unless namespace - - if username_changed? - unless namespace.update_attributes(path: username, name: username) - namespace.errors.each do |attribute, message| - self.errors.add(:"namespace_#{attribute}", message) - end - raise ActiveRecord::RecordInvalid.new(namespace) - end + if namespace + namespace.path = namespace.name = username if username_changed? + else + build_namespace(path: username, name: username) end end |