Commit 4e5597b8 authored by Ariejan de Vroom's avatar Ariejan de Vroom

Merge branch 'miks-deploy_keys_nonunique'

parents bfe0906f b0ce61c4
require 'digest/md5'
class Key < ActiveRecord::Base class Key < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :project belongs_to :project
...@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base ...@@ -8,17 +10,30 @@ class Key < ActiveRecord::Base
validates :key, validates :key,
:presence => true, :presence => true,
:uniqueness => true,
:length => { :within => 0..5000 } :length => { :within => 0..5000 }
before_save :set_identifier before_save :set_identifier
before_validation :strip_white_space
after_save :update_repository after_save :update_repository
after_destroy :repository_delete_key after_destroy :repository_delete_key
delegate :name, :email, :to => :user, :prefix => true 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 def set_identifier
if is_deploy_key if is_deploy_key
self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" self.identifier = "deploy_" + Digest::MD5.hexdigest(key)
else else
self.identifier = "#{user.identifier}_#{Time.now.to_i}" self.identifier = "#{user.identifier}_#{Time.now.to_i}"
end end
...@@ -33,7 +48,10 @@ class Key < ActiveRecord::Base ...@@ -33,7 +48,10 @@ class Key < ActiveRecord::Base
def repository_delete_key def repository_delete_key
Gitlabhq::GitHost.system.new.configure do |c| Gitlabhq::GitHost.system.new.configure do |c|
#delete key file is there is no identically deploy keys
if !is_deploy_key || Key.where(:identifier => identifier).count() == 0
c.delete_key(identifier) c.delete_key(identifier)
end
c.update_projects(projects) c.update_projects(projects)
end end
end end
......
...@@ -163,6 +163,13 @@ ActiveRecord::Schema.define(:version => 20120228134252) do ...@@ -163,6 +163,13 @@ ActiveRecord::Schema.define(:version => 20120228134252) do
t.integer "project_access", :default => 0, :null => false t.integer "project_access", :default => 0, :null => false
end end
create_table "web_hook_urls", :force => true do |t|
t.string "url"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "web_hooks", :force => true do |t| create_table "web_hooks", :force => true do |t|
t.string "url" t.string "url"
t.integer "project_id" t.integer "project_id"
......
...@@ -10,8 +10,8 @@ class Factory ...@@ -10,8 +10,8 @@ class Factory
new(name, opts).tap(&:save!) new(name, opts).tap(&:save!)
end end
def new(name, opts) def new(name, opts = {})
factory = @factories[name] factory= @factories[name]
factory[0].new.tap do |obj| factory[0].new.tap do |obj|
factory[1].call(obj) factory[1].call(obj)
end.tap do |obj| end.tap do |obj|
......
...@@ -14,8 +14,40 @@ describe Key do ...@@ -14,8 +14,40 @@ describe Key do
it { should respond_to :projects } it { should respond_to :projects }
end end
it { Factory.create(:key, context "validation of uniqueness" do
:user => Factory(:user)).should be_valid }
context "as a deploy key" do
let(:project) { Factory.create(:project, path: 'alpha', code: 'alpha') }
let(:another_project) { Factory.create(:project, path: 'beta', code: 'beta') }
before do
deploy_key = Factory.create(:key, project: project)
end
it "does not accept the same key twice for a project" do
key = Factory.new(:key, project: project)
key.should_not be_valid
end
it "does accept the same key for another project" do
key = Factory.new(:key, project: another_project)
key.should be_valid
end
end
context "as a personal key" do
let(:user) { Factory.create(:user) }
it "accepts the key once" do
Factory.new(:key, user: user).should be_valid
end
it "does not accepts the key twice" do
Factory.create(:key, user: user)
Factory.new(:key, user: user).should_not be_valid
end
end
end
end end
# == Schema Information # == Schema Information
# #
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment