Commit 99b8b577e92831d9cfe4f98fca4c6b8017c95a7a
1 parent
5b4382e1
Exists in
master
and in
4 other branches
DeployKey moved to Key model
Showing
8 changed files
with
35 additions
and
93 deletions
Show diff stats
app/controllers/deploy_keys_controller.rb
| ... | ... | @@ -12,28 +12,28 @@ class DeployKeysController < ApplicationController |
| 12 | 12 | end |
| 13 | 13 | |
| 14 | 14 | def index |
| 15 | - @keys = @project.deploy_keys.all | |
| 15 | + @keys = @project.keys.all | |
| 16 | 16 | end |
| 17 | 17 | |
| 18 | 18 | def show |
| 19 | - @key = @project.deploy_keys.find(params[:id]) | |
| 19 | + @key = @project.keys.find(params[:id]) | |
| 20 | 20 | end |
| 21 | 21 | |
| 22 | 22 | def new |
| 23 | - @key = @project.deploy_keys.new | |
| 23 | + @key = @project.keys.new | |
| 24 | 24 | |
| 25 | 25 | respond_with(@key) |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | 28 | def create |
| 29 | - @key = @project.deploy_keys.new(params[:deploy_key]) | |
| 29 | + @key = @project.keys.new(params[:key]) | |
| 30 | 30 | @key.save |
| 31 | 31 | |
| 32 | 32 | respond_with(@key) |
| 33 | 33 | end |
| 34 | 34 | |
| 35 | 35 | def destroy |
| 36 | - @key = @project.deploy_keys.find(params[:id]) | |
| 36 | + @key = @project.keys.find(params[:id]) | |
| 37 | 37 | @key.destroy |
| 38 | 38 | |
| 39 | 39 | respond_to do |format| | ... | ... |
app/models/deploy_key.rb
| ... | ... | @@ -1,53 +0,0 @@ |
| 1 | -require 'unique_public_key_validator' | |
| 2 | - | |
| 3 | -class DeployKey < ActiveRecord::Base | |
| 4 | - belongs_to :project | |
| 5 | - | |
| 6 | - validates :title, | |
| 7 | - :presence => true, | |
| 8 | - :length => { :within => 0..255 } | |
| 9 | - | |
| 10 | - validates :key, | |
| 11 | - :presence => true, | |
| 12 | - :uniqueness => true, | |
| 13 | - :length => { :within => 0..5000 } | |
| 14 | - | |
| 15 | - validates_with UniquePublicKeyValidator | |
| 16 | - | |
| 17 | - before_save :set_identifier | |
| 18 | - after_save :update_repository | |
| 19 | - after_destroy :repository_delete_key | |
| 20 | - | |
| 21 | - def set_identifier | |
| 22 | - self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" | |
| 23 | - end | |
| 24 | - | |
| 25 | - def update_repository | |
| 26 | - Gitlabhq::GitHost.system.new.configure do |c| | |
| 27 | - c.update_keys(identifier, key) | |
| 28 | - c.update_project(project.path, project) | |
| 29 | - end | |
| 30 | - end | |
| 31 | - | |
| 32 | - def repository_delete_key | |
| 33 | - Gitlabhq::GitHost.system.new.configure do |c| | |
| 34 | - c.delete_key(identifier) | |
| 35 | - c.update_project(project.path, project) | |
| 36 | - end | |
| 37 | - end | |
| 38 | - | |
| 39 | -end | |
| 40 | -# == Schema Information | |
| 41 | -# | |
| 42 | -# Table name: keys | |
| 43 | -# | |
| 44 | -# id :integer not null, primary key | |
| 45 | -# project_id :integer not null | |
| 46 | -# created_at :datetime | |
| 47 | -# updated_at :datetime | |
| 48 | -# key :text | |
| 49 | -# title :string(255) | |
| 50 | -# identifier :string(255) | |
| 51 | -# | |
| 52 | - | |
| 53 | - |
app/models/key.rb
| 1 | -require 'unique_public_key_validator' | |
| 2 | - | |
| 3 | 1 | class Key < ActiveRecord::Base |
| 4 | 2 | belongs_to :user |
| 3 | + belongs_to :project | |
| 5 | 4 | |
| 6 | 5 | validates :title, |
| 7 | 6 | :presence => true, |
| ... | ... | @@ -12,14 +11,16 @@ class Key < ActiveRecord::Base |
| 12 | 11 | :uniqueness => true, |
| 13 | 12 | :length => { :within => 0..5000 } |
| 14 | 13 | |
| 15 | - validates_with UniquePublicKeyValidator | |
| 16 | - | |
| 17 | 14 | before_save :set_identifier |
| 18 | 15 | after_save :update_repository |
| 19 | 16 | after_destroy :repository_delete_key |
| 20 | 17 | |
| 21 | 18 | def set_identifier |
| 22 | - self.identifier = "#{user.identifier}_#{Time.now.to_i}" | |
| 19 | + if is_deploy_key | |
| 20 | + self.identifier = "deploy_#{project.code}_#{Time.now.to_i}" | |
| 21 | + else | |
| 22 | + self.identifier = "#{user.identifier}_#{Time.now.to_i}" | |
| 23 | + end | |
| 23 | 24 | end |
| 24 | 25 | |
| 25 | 26 | def update_repository |
| ... | ... | @@ -35,10 +36,18 @@ class Key < ActiveRecord::Base |
| 35 | 36 | c.update_projects(projects) |
| 36 | 37 | end |
| 37 | 38 | end |
| 39 | + | |
| 40 | + def is_deploy_key | |
| 41 | + true if project_id | |
| 42 | + end | |
| 38 | 43 | |
| 39 | 44 | #projects that has this key |
| 40 | 45 | def projects |
| 41 | - user.projects | |
| 46 | + if is_deploy_key | |
| 47 | + [project] | |
| 48 | + else | |
| 49 | + user.projects | |
| 50 | + end | |
| 42 | 51 | end |
| 43 | 52 | end |
| 44 | 53 | # == Schema Information | ... | ... |
app/models/project.rb
| ... | ... | @@ -14,7 +14,7 @@ class Project < ActiveRecord::Base |
| 14 | 14 | has_many :users, :through => :users_projects |
| 15 | 15 | has_many :notes, :dependent => :destroy |
| 16 | 16 | has_many :snippets, :dependent => :destroy |
| 17 | - has_many :deploy_keys, :dependent => :destroy | |
| 17 | + has_many :keys, :dependent => :destroy | |
| 18 | 18 | has_many :web_hooks, :dependent => :destroy |
| 19 | 19 | |
| 20 | 20 | acts_as_taggable |
| ... | ... | @@ -189,15 +189,15 @@ class Project < ActiveRecord::Base |
| 189 | 189 | end |
| 190 | 190 | |
| 191 | 191 | def repository_readers |
| 192 | - keys = Key.joins({:user => :users_projects}). | |
| 192 | + read_keys = Key.joins({:user => :users_projects}). | |
| 193 | 193 | where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_R) |
| 194 | - keys.map(&:identifier) + deploy_keys.map(&:identifier) | |
| 194 | + read_keys.map(&:identifier) + keys.map(&:identifier) | |
| 195 | 195 | end |
| 196 | 196 | |
| 197 | 197 | def repository_writers |
| 198 | - keys = Key.joins({:user => :users_projects}). | |
| 198 | + write_keys = Key.joins({:user => :users_projects}). | |
| 199 | 199 | where("users_projects.project_id = ? AND users_projects.repo_access = ?", id, Repository::REPO_RW) |
| 200 | - keys.map(&:identifier) | |
| 200 | + write_keys.map(&:identifier) | |
| 201 | 201 | end |
| 202 | 202 | |
| 203 | 203 | def readers | ... | ... |
app/views/deploy_keys/_form.html.haml
db/migrate/20111225202855_create_deploy_keys.rb
| ... | ... | @@ -1,12 +0,0 @@ |
| 1 | -class CreateDeployKeys < ActiveRecord::Migration | |
| 2 | - def change | |
| 3 | - create_table :deploy_keys do |t| | |
| 4 | - t.integer "project_id", :null => false | |
| 5 | - t.datetime "created_at" | |
| 6 | - t.datetime "updated_at" | |
| 7 | - t.text "key" | |
| 8 | - t.string "title" | |
| 9 | - t.string "identifier" | |
| 10 | - end | |
| 11 | - end | |
| 12 | -end |
db/schema.rb
| ... | ... | @@ -11,16 +11,7 @@ |
| 11 | 11 | # |
| 12 | 12 | # It's strongly recommended to check this file into your version control system. |
| 13 | 13 | |
| 14 | -ActiveRecord::Schema.define(:version => 20111225202855) do | |
| 15 | - | |
| 16 | - create_table "deploy_keys", :force => true do |t| | |
| 17 | - t.integer "project_id", :null => false | |
| 18 | - t.datetime "created_at" | |
| 19 | - t.datetime "updated_at" | |
| 20 | - t.text "key" | |
| 21 | - t.string "title" | |
| 22 | - t.string "identifier" | |
| 23 | - end | |
| 14 | +ActiveRecord::Schema.define(:version => 20111231111825) do | |
| 24 | 15 | |
| 25 | 16 | create_table "issues", :force => true do |t| |
| 26 | 17 | t.string "title" |
| ... | ... | @@ -36,12 +27,13 @@ ActiveRecord::Schema.define(:version => 20111225202855) do |
| 36 | 27 | end |
| 37 | 28 | |
| 38 | 29 | create_table "keys", :force => true do |t| |
| 39 | - t.integer "user_id", :null => false | |
| 30 | + t.integer "user_id" | |
| 40 | 31 | t.datetime "created_at" |
| 41 | 32 | t.datetime "updated_at" |
| 42 | 33 | t.text "key" |
| 43 | 34 | t.string "title" |
| 44 | 35 | t.string "identifier" |
| 36 | + t.integer "project_id" | |
| 45 | 37 | end |
| 46 | 38 | |
| 47 | 39 | create_table "merge_requests", :force => true do |t| | ... | ... |