Allow non-unique deploy keys
This commit is contained in:
parent
3d77183c16
commit
e3e9db9509
|
@ -1,3 +1,5 @@
|
||||||
|
require 'digest/md5'
|
||||||
|
|
||||||
class Key < ActiveRecord::Base
|
class Key < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
|
@ -8,16 +10,24 @@ class Key < ActiveRecord::Base
|
||||||
|
|
||||||
validates :key,
|
validates :key,
|
||||||
:presence => true,
|
:presence => true,
|
||||||
:uniqueness => true,
|
|
||||||
:length => { :within => 0..5000 }
|
:length => { :within => 0..5000 }
|
||||||
|
|
||||||
before_save :set_identifier
|
before_save :set_identifier
|
||||||
after_save :update_repository
|
after_save :update_repository
|
||||||
after_destroy :repository_delete_key
|
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
|
def set_identifier
|
||||||
if is_deploy_key
|
if is_deploy_key
|
||||||
self.identifier = "deploy_#{project.code}_#{Time.now.to_i}"
|
self.identifier = "deploy_" + Digest::MD5.hexdigest(key)
|
||||||
else
|
else
|
||||||
self.identifier = "#{user.identifier}_#{Time.now.to_i}"
|
self.identifier = "#{user.identifier}_#{Time.now.to_i}"
|
||||||
end
|
end
|
||||||
|
@ -32,7 +42,10 @@ class Key < ActiveRecord::Base
|
||||||
|
|
||||||
def repository_delete_key
|
def repository_delete_key
|
||||||
Gitlabhq::GitHost.system.new.configure do |c|
|
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)
|
c.update_projects(projects)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue