summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2015-12-23 12:07:37 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2015-12-23 12:07:37 +0000
commite1e39b61e52146a3796efa4c7a0a099b80e5c932 (patch)
tree9625720c196f69266a8be4864b8801418920afd7 /app
parentc53742b14d1abb4557f037d16b282ee7d26d4677 (diff)
parent3e6950481a90a83f183397f11b8f2a5d21233cfb (diff)
downloadgitlab-ce-e1e39b61e52146a3796efa4c7a0a099b80e5c932.tar.gz
Merge branch 'fix/ci-runners-token-persistence' into 'master'
Fix method that ensures authentication token Until now, `ensure_#{token_filed_name}!` method didn't persist new token in database. This closes #4235. See merge request !2185
Diffstat (limited to 'app')
-rw-r--r--app/models/application_setting.rb4
-rw-r--r--app/models/concerns/token_authenticatable.rb20
-rw-r--r--app/views/admin/runners/index.html.haml2
3 files changed, 18 insertions, 8 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
index 1f4e8b3ef24..724429e7558 100644
--- a/app/models/application_setting.rb
+++ b/app/models/application_setting.rb
@@ -134,4 +134,8 @@ class ApplicationSetting < ActiveRecord::Base
/x)
self.restricted_signup_domains.reject! { |d| d.empty? }
end
+
+ def runners_registration_token
+ ensure_runners_registration_token!
+ end
end
diff --git a/app/models/concerns/token_authenticatable.rb b/app/models/concerns/token_authenticatable.rb
index 488ff8c31b7..885deaf78d2 100644
--- a/app/models/concerns/token_authenticatable.rb
+++ b/app/models/concerns/token_authenticatable.rb
@@ -18,15 +18,16 @@ module TokenAuthenticatable
define_method("ensure_#{token_field}") do
current_token = read_attribute(token_field)
- if current_token.blank?
- write_attribute(token_field, generate_token_for(token_field))
- else
- current_token
- end
+ current_token.blank? ? write_new_token(token_field) : current_token
+ end
+
+ define_method("ensure_#{token_field}!") do
+ send("reset_#{token_field}!") if read_attribute(token_field).blank?
+ read_attribute(token_field)
end
define_method("reset_#{token_field}!") do
- write_attribute(token_field, generate_token_for(token_field))
+ write_new_token(token_field)
save!
end
end
@@ -34,7 +35,12 @@ module TokenAuthenticatable
private
- def generate_token_for(token_field)
+ def write_new_token(token_field)
+ new_token = generate_token(token_field)
+ write_attribute(token_field, new_token)
+ end
+
+ def generate_token(token_field)
loop do
token = Devise.friendly_token
break token unless self.class.unscoped.find_by(token_field => token)
diff --git a/app/views/admin/runners/index.html.haml b/app/views/admin/runners/index.html.haml
index c5fb3c95506..c407972cd08 100644
--- a/app/views/admin/runners/index.html.haml
+++ b/app/views/admin/runners/index.html.haml
@@ -3,7 +3,7 @@
To register a new runner you should enter the following registration token.
With this token the runner will request a unique runner token and use that for future communication.
Registration token is
- %code{ id: 'runners-token' } #{current_application_settings.ensure_runners_registration_token}
+ %code{ id: 'runners-token' } #{current_application_settings.runners_registration_token}
.bs-callout.clearfix
.pull-left