summaryrefslogtreecommitdiff
path: root/app/models/key.rb
diff options
context:
space:
mode:
authorAriejan de Vroom <ariejan@ariejan.net>2012-03-01 16:00:14 +0100
committerAriejan de Vroom <ariejan@ariejan.net>2012-03-01 16:00:14 +0100
commitb0ce61c4f20abe1ca5c99631517b49c99f8b23ef (patch)
treef43439eba70d0815da2528d8110c456440a3ff12 /app/models/key.rb
parentbfe0906f2ff7a79064c1866c0278cd0c9440b246 (diff)
parentf5a16663f0b038aeb397bda19ebdefa6ad873955 (diff)
downloadgitlab-ce-b0ce61c4f20abe1ca5c99631517b49c99f8b23ef.tar.gz
Merge branch 'deploy_keys_nonunique' of https://github.com/miks/gitlabhq into miks-deploy_keys_nonunique
Added/fixed specs Update spec/factory to allow Factory#new without opts Conflicts: app/models/key.rb
Diffstat (limited to 'app/models/key.rb')
-rw-r--r--app/models/key.rb26
1 files changed, 22 insertions, 4 deletions
diff --git a/app/models/key.rb b/app/models/key.rb
index 4114e5261dc..779d985d98b 100644
--- a/app/models/key.rb
+++ b/app/models/key.rb
@@ -1,3 +1,5 @@
+require 'digest/md5'
+
class Key < ActiveRecord::Base
belongs_to :user
belongs_to :project
@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
validates :key,
:presence => true,
- :uniqueness => true,
:length => { :within => 0..5000 }
before_save :set_identifier
+ before_validation :strip_white_space
after_save :update_repository
after_destroy :repository_delete_key
delegate :name, :email, :to => :user, :prefix => true
+ validate :unique_key
+
+ def strip_white_space
+ self.key = self.key.strip unless self.key.blank?
+ end
+
+ def unique_key
+ query = Key.where('key = ?', key)
+ query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id
+ if (query.count > 0)
+ errors.add :key, 'already exist.'
+ end
+ end
def set_identifier
if is_deploy_key
- self.identifier = "deploy_#{project.code}_#{Time.now.to_i}"
+ self.identifier = "deploy_" + Digest::MD5.hexdigest(key)
else
self.identifier = "#{user.identifier}_#{Time.now.to_i}"
end
@@ -33,11 +48,14 @@ class Key < ActiveRecord::Base
def repository_delete_key
Gitlabhq::GitHost.system.new.configure do |c|
- c.delete_key(identifier)
+ #delete key file is there is no identically deploy keys
+ if !is_deploy_key || Key.where(:identifier => identifier).count() == 0
+ c.delete_key(identifier)
+ end
c.update_projects(projects)
end
end
-
+
def is_deploy_key
true if project_id
end