Skip to content
Snippets Groups Projects
Commit e3e9db95 authored by miks's avatar miks
Browse files

Allow non-unique deploy keys

parent 3d77183c
No related branches found
No related tags found
1 merge request!379Single deploy key across multiple projects. Fixes #370
require 'digest/md5'
class Key < ActiveRecord::Base
belongs_to :user
belongs_to :project
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment