@@ -62,6 +62,7 @@ class User < ActiveRecord::Base
6262 validate :private_namespace_and_team_available , on : :create
6363 validate :portus_user_validation , on : :update
6464 after_create :create_personal_namespace!
65+ after_create :no_skip_validation!
6566
6667 # Actions performed before destroy
6768 before_destroy :update_tags!
@@ -78,6 +79,21 @@ class User < ActiveRecord::Base
7879 scope :enabled , -> { not_portus . where enabled : true }
7980 scope :admins , -> { not_portus . where enabled : true , admin : true }
8081
82+ class <<self
83+ attr_accessor :skip_portus_validation
84+ end
85+
86+ # Creates the Portus hidden user.
87+ def self . create_portus_user!
88+ User . skip_portus_validation = true
89+ User . create! (
90+ username : "portus" ,
91+ password : Rails . application . secrets . portus_password ,
92+ 93+ admin : true
94+ )
95+ end
96+
8197 # Special method used by Devise to require an email on signup. This is always
8298 # true except for LDAP.
8399 def email_required?
@@ -87,6 +103,11 @@ def email_required?
87103 # Adds an error if the user to be updated is the portus one. This is a
88104 # validation on update, so it can be skipped when strictly required.
89105 def portus_user_validation
106+ if User . skip_portus_validation
107+ User . skip_portus_validation = nil
108+ return
109+ end
110+
90111 return unless portus? || portus? ( username_was )
91112 errors . add ( :username , "cannot be updated" )
92113 end
@@ -138,7 +159,11 @@ def create_personal_namespace!
138159 description : default_description ,
139160 registry : Registry . get # TODO: fix once we handle more registries
140161 )
141- update_attributes ( namespace : namespace )
162+
163+ # Skipping validation on purpose, so after creating the portus hidden user,
164+ # a namespace can be assigned to it even if updates are forbidden
165+ # afterwards.
166+ update_attribute ( "namespace" , namespace )
142167 end
143168
144169 # Find the user that can be guessed from the given push event.
@@ -256,6 +281,11 @@ def suggest_username(data)
256281
257282 protected
258283
284+ # Validations can no longer be skipped after calling this method.
285+ def no_skip_validation!
286+ User . skip_portus_validation = nil
287+ end
288+
259289 # Get username from provider's data.
260290 def extract_username ( data )
261291 data [ "nickname" ] || data [ "username" ] || data [ "email" ] . match ( /^[^@]*/ ) . to_s
0 commit comments