Allow non-unique deploy keys

This commit is contained in:
miks 2012-02-07 23:56:53 +02:00
parent 3d77183c16
commit e3e9db9509

View file

@ -1,3 +1,5 @@
require 'digest/md5'
class Key < ActiveRecord::Base
belongs_to :user
belongs_to :project
@ -8,16 +10,24 @@ class Key < ActiveRecord::Base
validates :key,
:presence => true,
:uniqueness => true,
:length => { :within => 0..5000 }
before_save :set_identifier
after_save :update_repository
after_destroy :repository_delete_key
validate :unique_key
def unique_key
query = 'key = ?'
query << ' AND project_id IS NULL' unless user_id
if (Key.where(query, key.strip).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
@ -32,7 +42,10 @@ 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