Skip to content
Snippets Groups Projects
Commit 5b4382e1 authored by miks's avatar miks
Browse files

Validate key uniqueness across Key and DeployKey tables

parent c973fce6
No related branches found
No related tags found
1 merge request!251Deploy keys for project
require 'unique_public_key_validator'
class DeployKey < ActiveRecord::Base
belongs_to :project
 
Loading
Loading
@@ -10,6 +12,8 @@ class DeployKey < ActiveRecord::Base
:uniqueness => true,
:length => { :within => 0..5000 }
 
validates_with UniquePublicKeyValidator
before_save :set_identifier
after_save :update_repository
after_destroy :repository_delete_key
Loading
Loading
require 'unique_public_key_validator'
class Key < ActiveRecord::Base
belongs_to :user
 
Loading
Loading
@@ -10,6 +12,8 @@ class Key < ActiveRecord::Base
:uniqueness => true,
:length => { :within => 0..5000 }
 
validates_with UniquePublicKeyValidator
before_save :set_identifier
after_save :update_repository
after_destroy :repository_delete_key
Loading
Loading
class UniquePublicKeyValidator < ActiveModel::Validator
def validate(record)
if (DeployKey.where('key = ? AND id !=?', record.key , record.id).count > 0 || Key.where('key = ? AND id !=?', record.key , record.id).count > 0)
record.errors.add :key, 'already exist.'
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment