Commit 5b4382e12e060528c42bd3e19f61df88d6316785
1 parent
c973fce6
Exists in
master
and in
4 other branches
Validate key uniqueness across Key and DeployKey tables
Showing
3 changed files
with
15 additions
and
0 deletions
Show diff stats
app/models/deploy_key.rb
1 | +require 'unique_public_key_validator' | |
2 | + | |
1 | 3 | class DeployKey < ActiveRecord::Base |
2 | 4 | belongs_to :project |
3 | 5 | |
... | ... | @@ -10,6 +12,8 @@ class DeployKey < ActiveRecord::Base |
10 | 12 | :uniqueness => true, |
11 | 13 | :length => { :within => 0..5000 } |
12 | 14 | |
15 | + validates_with UniquePublicKeyValidator | |
16 | + | |
13 | 17 | before_save :set_identifier |
14 | 18 | after_save :update_repository |
15 | 19 | after_destroy :repository_delete_key | ... | ... |
app/models/key.rb
1 | +require 'unique_public_key_validator' | |
2 | + | |
1 | 3 | class Key < ActiveRecord::Base |
2 | 4 | belongs_to :user |
3 | 5 | |
... | ... | @@ -10,6 +12,8 @@ class Key < ActiveRecord::Base |
10 | 12 | :uniqueness => true, |
11 | 13 | :length => { :within => 0..5000 } |
12 | 14 | |
15 | + validates_with UniquePublicKeyValidator | |
16 | + | |
13 | 17 | before_save :set_identifier |
14 | 18 | after_save :update_repository |
15 | 19 | after_destroy :repository_delete_key | ... | ... |
... | ... | @@ -0,0 +1,7 @@ |
1 | +class UniquePublicKeyValidator < ActiveModel::Validator | |
2 | + def validate(record) | |
3 | + if (DeployKey.where('key = ? AND id !=?', record.key , record.id).count > 0 || Key.where('key = ? AND id !=?', record.key , record.id).count > 0) | |
4 | + record.errors.add :key, 'already exist.' | |
5 | + end | |
6 | + end | |
7 | +end | ... | ... |