Commit e3e9db9509a14032b692597b5134446a5adc137f
1 parent
3d77183c
Exists in
master
and in
4 other branches
Allow non-unique deploy keys
Showing
1 changed file
with
16 additions
and
3 deletions
Show diff stats
app/models/key.rb
1 | +require 'digest/md5' | |
2 | + | |
1 | 3 | class Key < ActiveRecord::Base |
2 | 4 | belongs_to :user |
3 | 5 | belongs_to :project |
... | ... | @@ -8,16 +10,24 @@ class Key < ActiveRecord::Base |
8 | 10 | |
9 | 11 | validates :key, |
10 | 12 | :presence => true, |
11 | - :uniqueness => true, | |
12 | 13 | :length => { :within => 0..5000 } |
13 | 14 | |
14 | 15 | before_save :set_identifier |
15 | 16 | after_save :update_repository |
16 | 17 | after_destroy :repository_delete_key |
18 | + validate :unique_key | |
19 | + | |
20 | + def unique_key | |
21 | + query = 'key = ?' | |
22 | + query << ' AND project_id IS NULL' unless user_id | |
23 | + if (Key.where(query, key.strip).count > 0) | |
24 | + errors.add :key, 'already exist.' | |
25 | + end | |
26 | + end | |
17 | 27 | |
18 | 28 | def set_identifier |
19 | 29 | if is_deploy_key |
20 | - self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" | |
30 | + self.identifier = "deploy_" + Digest::MD5.hexdigest(key) | |
21 | 31 | else |
22 | 32 | self.identifier = "#{user.identifier}_#{Time.now.to_i}" |
23 | 33 | end |
... | ... | @@ -32,7 +42,10 @@ class Key < ActiveRecord::Base |
32 | 42 | |
33 | 43 | def repository_delete_key |
34 | 44 | Gitlabhq::GitHost.system.new.configure do |c| |
35 | - c.delete_key(identifier) | |
45 | + #delete key file is there is no identically deploy keys | |
46 | + if !is_deploy_key || Key.where(:identifier => identifier).count() == 0 | |
47 | + c.delete_key(identifier) | |
48 | + end | |
36 | 49 | c.update_projects(projects) |
37 | 50 | end |
38 | 51 | end | ... | ... |