diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-03-27 16:43:48 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-04-03 13:23:20 +0300 |
commit | edc4a56d26792b5b5bac21f45948412675ad7ebb (patch) | |
tree | 5691320e58507e134a2f026f8f4e8054821edf50 /app/models | |
parent | 9157985cfce1391973673ea278dc7506a90f8f53 (diff) |
Allow admin to create public deploy keys that are accessible to any project.
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/deploy_key.rb | 7 | ||||
-rw-r--r-- | app/models/deploy_keys_project.rb | 6 | ||||
-rw-r--r-- | app/models/user.rb | 10 |
3 files changed, 21 insertions, 2 deletions
diff --git a/app/models/deploy_key.rb b/app/models/deploy_key.rb index 570f5e91c13..1770dde320f 100644 --- a/app/models/deploy_key.rb +++ b/app/models/deploy_key.rb @@ -7,6 +7,7 @@ # created_at :datetime # updated_at :datetime # key :text +# public :boolean default(FALSE) # title :string(255) # type :string(255) # fingerprint :string(255) @@ -17,4 +18,10 @@ class DeployKey < Key has_many :projects, through: :deploy_keys_projects scope :in_projects, ->(projects) { joins(:deploy_keys_projects).where('deploy_keys_projects.project_id in (?)', projects) } + scope :are_public, -> { where(public: true) } + scope :are_private, -> { where(public: false) } + + def private? + !public? + end end diff --git a/app/models/deploy_keys_project.rb b/app/models/deploy_keys_project.rb index 7e88903b9af..5f679f4b3ef 100644 --- a/app/models/deploy_keys_project.rb +++ b/app/models/deploy_keys_project.rb @@ -22,6 +22,10 @@ class DeployKeysProject < ActiveRecord::Base private def destroy_orphaned_deploy_key - self.deploy_key.destroy if self.deploy_key.deploy_keys_projects.length == 0 + # Public deploy keys are never automatically deleted + return if self.deploy_key.public? + return if self.deploy_key.deploy_keys_projects.length > 0 + + self.deploy_key.destroy end end diff --git a/app/models/user.rb b/app/models/user.rb index 515f29ea0ba..ea6b1367bbc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -414,8 +414,16 @@ class User < ActiveRecord::Base @ldap_identity ||= identities.find_by(["provider LIKE ?", "ldap%"]) end + def project_deploy_keys + DeployKey.in_projects(self.authorized_projects.pluck(:id)) + end + def accessible_deploy_keys - DeployKey.in_projects(self.authorized_projects.pluck(:id)).uniq + @accessible_deploy_keys ||= begin + key_ids = project_deploy_keys.pluck(:id) + key_ids.push(*DeployKey.are_public.pluck(:id)) + DeployKey.where(id: key_ids) + end end def created_by |