Merge branch 'deploy_keys_nonunique' of https://github.com/miks/gitlabhq into miks-deploy_keys_nonunique

Added/fixed specs
Update spec/factory to allow Factory#new without opts

Conflicts:
	app/models/key.rb
This commit is contained in:
Ariejan de Vroom 2012-03-01 16:00:14 +01:00
commit b0ce61c4f2
4 changed files with 65 additions and 8 deletions

View file

@ -1,3 +1,5 @@
require 'digest/md5'
class Key < ActiveRecord::Base
belongs_to :user
belongs_to :project
@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
validates :key,
:presence => true,
:uniqueness => true,
:length => { :within => 0..5000 }
before_save :set_identifier
before_validation :strip_white_space
after_save :update_repository
after_destroy :repository_delete_key
delegate :name, :email, :to => :user, :prefix => true
validate :unique_key
def strip_white_space
self.key = self.key.strip unless self.key.blank?
end
def unique_key
query = Key.where('key = ?', key)
query = query.where('(project_id IS NULL OR project_id = ?)', project_id) if project_id
if (query.count > 0)
errors.add :key, 'already exist.'
end
end
def set_identifier
if is_deploy_key
self.identifier = "deploy_#{project.code}_#{Time.now.to_i}"
self.identifier = "deploy_" + Digest::MD5.hexdigest(key)
else
self.identifier = "#{user.identifier}_#{Time.now.to_i}"
end
@ -33,11 +48,14 @@ class Key < ActiveRecord::Base
def repository_delete_key
Gitlabhq::GitHost.system.new.configure do |c|
c.delete_key(identifier)
#delete key file is there is no identically deploy keys
if !is_deploy_key || Key.where(:identifier => identifier).count() == 0
c.delete_key(identifier)
end
c.update_projects(projects)
end
end
def is_deploy_key
true if project_id
end