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 | class Key < ActiveRecord::Base | 3 | class Key < ActiveRecord::Base |
2 | belongs_to :user | 4 | belongs_to :user |
3 | belongs_to :project | 5 | belongs_to :project |
@@ -8,16 +10,24 @@ class Key < ActiveRecord::Base | @@ -8,16 +10,24 @@ class Key < ActiveRecord::Base | ||
8 | 10 | ||
9 | validates :key, | 11 | validates :key, |
10 | :presence => true, | 12 | :presence => true, |
11 | - :uniqueness => true, | ||
12 | :length => { :within => 0..5000 } | 13 | :length => { :within => 0..5000 } |
13 | 14 | ||
14 | before_save :set_identifier | 15 | before_save :set_identifier |
15 | after_save :update_repository | 16 | after_save :update_repository |
16 | after_destroy :repository_delete_key | 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 | def set_identifier | 28 | def set_identifier |
19 | if is_deploy_key | 29 | if is_deploy_key |
20 | - self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" | 30 | + self.identifier = "deploy_" + Digest::MD5.hexdigest(key) |
21 | else | 31 | else |
22 | self.identifier = "#{user.identifier}_#{Time.now.to_i}" | 32 | self.identifier = "#{user.identifier}_#{Time.now.to_i}" |
23 | end | 33 | end |
@@ -32,7 +42,10 @@ class Key < ActiveRecord::Base | @@ -32,7 +42,10 @@ class Key < ActiveRecord::Base | ||
32 | 42 | ||
33 | def repository_delete_key | 43 | def repository_delete_key |
34 | Gitlabhq::GitHost.system.new.configure do |c| | 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 | c.update_projects(projects) | 49 | c.update_projects(projects) |
37 | end | 50 | end |
38 | end | 51 | end |