summaryrefslogtreecommitdiff
path: root/app/models/deploy_token.rb
diff options
context:
space:
mode:
authorJames Lopez <james@gitlab.com>2018-04-12 08:57:27 +0000
committerJames Lopez <james@gitlab.com>2018-04-12 08:57:27 +0000
commit63623dc7741a4680af17dd348319e1104f71a8c6 (patch)
tree24586af26d80f56d06d8b432a22fa6bfa7cae8c0 /app/models/deploy_token.rb
parentc393a44f2fa5e161c8055bc589f10d4ee2e1d8a0 (diff)
parent8e26fbfeff7fbd32879bfc530fb3eb5bcbd73a00 (diff)
downloadgitlab-ce-63623dc7741a4680af17dd348319e1104f71a8c6.tar.gz
Merge branch '10-7-stable-prepare-rc5' into '10-7-stable'
Prepare 10.7 RC5 release See merge request gitlab-org/gitlab-ce!18308
Diffstat (limited to 'app/models/deploy_token.rb')
-rw-r--r--app/models/deploy_token.rb61
1 files changed, 61 insertions, 0 deletions
diff --git a/app/models/deploy_token.rb b/app/models/deploy_token.rb
new file mode 100644
index 00000000000..8dae821a10e
--- /dev/null
+++ b/app/models/deploy_token.rb
@@ -0,0 +1,61 @@
+class DeployToken < ActiveRecord::Base
+ include Expirable
+ include TokenAuthenticatable
+ add_authentication_token_field :token
+
+ AVAILABLE_SCOPES = %i(read_repository read_registry).freeze
+
+ default_value_for(:expires_at) { Forever.date }
+
+ has_many :project_deploy_tokens, inverse_of: :deploy_token
+ has_many :projects, -> { auto_include(false) }, through: :project_deploy_tokens
+
+ validate :ensure_at_least_one_scope
+ before_save :ensure_token
+
+ accepts_nested_attributes_for :project_deploy_tokens
+
+ scope :active, -> { where("revoked = false AND expires_at >= NOW()") }
+
+ def revoke!
+ update!(revoked: true)
+ end
+
+ def active?
+ !revoked
+ end
+
+ def scopes
+ AVAILABLE_SCOPES.select { |token_scope| read_attribute(token_scope) }
+ end
+
+ def username
+ "gitlab+deploy-token-#{id}"
+ end
+
+ def has_access_to?(requested_project)
+ active? && project == requested_project
+ end
+
+ # This is temporal. Currently we limit DeployToken
+ # to a single project, later we're going to extend
+ # that to be for multiple projects and namespaces.
+ def project
+ projects.first
+ end
+
+ def expires_at
+ expires_at = read_attribute(:expires_at)
+ expires_at != Forever.date ? expires_at : nil
+ end
+
+ def expires_at=(value)
+ write_attribute(:expires_at, value.presence || Forever.date)
+ end
+
+ private
+
+ def ensure_at_least_one_scope
+ errors.add(:base, "Scopes can't be blank") unless read_repository || read_registry
+ end
+end