Commit e3e9db95 authored by miks's avatar miks

Allow non-unique deploy keys

parent 3d77183c
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment