Commit 48628d31d59f007fbf4b6958eb2a48adedaef8e4

Authored by Dmitriy Zaporozhets
1 parent 18fc0900

dont allow duplicates in ssh keys

Showing 1 changed file with 3 additions and 15 deletions   Show diff stats
app/models/key.rb
@@ -24,8 +24,8 @@ class Key < ActiveRecord::Base @@ -24,8 +24,8 @@ class Key < ActiveRecord::Base
24 before_save :set_identifier 24 before_save :set_identifier
25 25
26 validates :title, presence: true, length: { within: 0..255 } 26 validates :title, presence: true, length: { within: 0..255 }
27 - validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }  
28 - validate :unique_key, :fingerprintable_key 27 + validates :key, presence: true, length: { within: 0..5000 }, format: { :with => /ssh-.{3} / }, uniqueness: true
  28 + validate :fingerprintable_key
29 29
30 delegate :name, :email, to: :user, prefix: true 30 delegate :name, :email, to: :user, prefix: true
31 31
@@ -33,14 +33,6 @@ class Key < ActiveRecord::Base @@ -33,14 +33,6 @@ class Key < ActiveRecord::Base
33 self.key = self.key.strip unless self.key.blank? 33 self.key = self.key.strip unless self.key.blank?
34 end 34 end
35 35
36 - def unique_key  
37 - query = Key.where(key: key)  
38 - query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id  
39 - if (query.count > 0)  
40 - errors.add :key, 'already exist.'  
41 - end  
42 - end  
43 -  
44 def fingerprintable_key 36 def fingerprintable_key
45 return true unless key # Don't test if there is no key. 37 return true unless key # Don't test if there is no key.
46 # `ssh-keygen -lf /dev/stdin <<< "#{key}"` errors with: redirection unexpected 38 # `ssh-keygen -lf /dev/stdin <<< "#{key}"` errors with: redirection unexpected
@@ -65,7 +57,7 @@ class Key &lt; ActiveRecord::Base @@ -65,7 +57,7 @@ class Key &lt; ActiveRecord::Base
65 end 57 end
66 58
67 def is_deploy_key 59 def is_deploy_key
68 - true if project_id 60 + !!project_id
69 end 61 end
70 62
71 # projects that has this key 63 # projects that has this key
@@ -77,10 +69,6 @@ class Key &lt; ActiveRecord::Base @@ -77,10 +69,6 @@ class Key &lt; ActiveRecord::Base
77 end 69 end
78 end 70 end
79 71
80 - def last_deploy?  
81 - Key.where(identifier: identifier).count == 0  
82 - end  
83 -  
84 def shell_id 72 def shell_id
85 "key-#{self.id}" 73 "key-#{self.id}"
86 end 74 end