[refs #441] Add key pairs for platform

This commit is contained in:
konstantin.grabar 2012-07-13 15:18:12 +04:00
parent c45e599eb0
commit b4059c67fc
20 changed files with 234 additions and 2 deletions

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Platforms::KeyPairs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -0,0 +1,31 @@
class Platforms::KeyPairsController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource :platform
load_and_authorize_resource
skip_load_and_authorize_resource :only => [:index]
skip_authorize_resource :platform, :only => [:create, :destroy]
def create
@key_pair.user_id = current_user.id
if @key_pair.key_create_call == true
flash[:notice] = t('flash.key_pairs.saved')
else
flash[:error] = t('flash.key_pairs.save_error')
end
redirect_to platform_key_pairs_path(@platform)
end
def destroy
if @key_pair.rm_key_call
flash[:notice] = t('flash.key_pairs.destroyed')
else
flash[:error] = t('flash.key_pairs.destroy_error')
end
redirect_to platform_key_pairs_path(@platform)
end
end

View File

@ -0,0 +1,2 @@
module Platforms::KeyPairsHelper
end

View File

@ -97,6 +97,8 @@ class Ability
can(:clear, Platform) {|platform| local_admin?(platform) && platform.personal?}
can([:change_visibility, :settings, :destroy], Repository) {|repository| owner? repository.platform}
can([:create, :destroy], KeyPair) {|key_pair| owner?(key_pair.repository.platform) || local_admin?(key_pair.repository.platform)}
can :read, Product, :platform => {:visibility => 'open'}
can :read, Product, :platform => {:owner_type => 'User', :owner_id => user.id, :platform_type => 'main'}
can :read, Product, :platform => {:owner_type => 'Group', :owner_id => user.group_ids, :platform_type => 'main'}

32
app/models/key_pair.rb Normal file
View File

@ -0,0 +1,32 @@
class KeyPair < ActiveRecord::Base
belongs_to :repository
belongs_to :user
attr_accessor :secret
attr_accessible :public, :secret, :repository_id
after_create :key_create_call
def key_create_call
code, self.key_id = BuildServer.import_gpg_key_pair(public, secret)
if code.zero?
set_code = BuildServer.set_repository_key(repository_id, repository.platform_id, key_id)
if set_code.zero?
self.save
else
set_code
end
else
code
end
end
def rm_key_call
if BuildServer.rm_repository_key(repository.platform_id, repository_id) == 0
self.destroy
return true
end
false
end
end

View File

@ -4,6 +4,7 @@ class Repository < ActiveRecord::Base
has_many :project_to_repositories, :dependent => :destroy, :validate => true
has_many :projects, :through => :project_to_repositories
has_many :key_pairs
validates :description, :presence => true
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => {:with => /^[a-z0-9_\-]+$/}

View File

@ -25,6 +25,9 @@
- if can? :members, @platform
%li{:class => (act == :members && contr == :platforms) ? 'active' : nil}
= link_to t("layout.platforms.members"), members_platform_path(@platform)
- if can? :edit, @platform
%li{:class => (act == :index && contr == :key_pairs) ? 'active' : ''}
= link_to t("layout.key_pairs.header"), platform_key_pairs_path(@platform)
-#- if current_user.owner_of? @platform or current_user.admin?
%li{:class => (act == :index && contr == :private_users) ? 'active' : ''}
= link_to t("layout.platforms.private_users"), platform_private_users_path(@platform)

View File

@ -0,0 +1,21 @@
%table#myTable.tablesorter.platform-repos{:cellspacing => "0", :cellpadding => "0"}
%thead
%tr
%th.th1= t("activerecord.attributes.key_pair.repository")
%th.th2= t("activerecord.attributes.key_pair.public")
%th.th3= t("activerecord.attributes.key_pair.user_id")
%th= t("layout.delete")
%tbody
- @platform.repositories.each do |repository|
- repository.key_pairs.each do |key_pair|
%tr{:class => cycle("odd", "even")}
%td
= key_pair.repository.name
%td
= key_pair.public
%td
= key_pair.user.name
%td.buttons
- if can? :destroy, key_pair
= link_to platform_key_pair_path(@platform, key_pair), :method => :delete, :confirm => t("layout.key_pairs.confirm_delete") do
%span.delete &nbsp;

View File

@ -0,0 +1,17 @@
= render 'platforms/base/sidebar'
%h3= t("layout.key_pairs.header")
= form_for :key_pair, :url => platform_key_pairs_path(@platform), :method => :post, :html => { :class => :form } do |f|
.leftlist= f.label :public, t("activerecord.attributes.key_pair.public"), :class => :label
.rightlist= f.text_field :public, :class => 'text_field'
.both
.leftlist= f.label :secret, t("activerecord.attributes.key_pair.secret"), :class => :label
.rightlist= f.text_field :secret, :class => 'text_field'
.both
.leftlist= f.label :repository_id, t("activerecord.attributes.key_pair.repository_id"), :class => :label
.rightlist= f.select :repository_id, options_from_collection_for_select(@platform.repositories, 'id', 'name')
.both
.button_block
= submit_tag t("layout.save")

View File

@ -0,0 +1,4 @@
= render 'new' if can? :edit, @platform
= render 'list'#, :object => @key_pairs
=# will_paginate @key_pairs

View File

@ -0,0 +1,27 @@
en:
layout:
key_pairs:
repository_id: Repository
user_id: User
public: Public key
secret: Secret key
confirm_delete: Are you sure you want to delete this key pair?
header: Key Pairs
flash:
key_pairs:
saved: Key pair succefully created
save_error: Key pair save error
destroyed: Key pair succefully destroyed
destroy_error: Key pair destroy error
activerecord:
models:
key_pair: Key Pair
attributes:
key_pair:
id: Id
created_at: Created
updated_at: Updated
user_id: User
repository_id: Repository
public: Public key
secret: Secret key

View File

@ -0,0 +1,27 @@
en:
layout:
key_pairs:
repository_id: Репозиторий
user_id: Пользователь
public: Публичный ключ
secret: Секретный ключ
confirm_delete: Вы уверены, что хотите удалить эту ключевую пару?
header: Ключевые Пары
flash:
key_pairs:
saved: Ключевая пара сохранена успешно
save_error: Ошибка создания ключевой пары
destroyed: Ключевая пара удалена успешно
destroy_error: Ошибка удаления ключевой пары
activerecord:
models:
key_pair: Ключевая пара
attributes:
key_pair:
id: Id
created_at: Создано
updated_at: Обновлено
user_id: Пользователь
repository_id: Репозиторий
public: Публичный ключ
secret: Секретный ключ

View File

@ -71,6 +71,7 @@ Rosa::Application.routes.draw do
get :projects_list
end
end
resources :key_pairs, :only => [:create, :index, :destroy]
resources :products do
resources :product_build_lists, :only => [:create, :destroy]
end

View File

@ -0,0 +1,11 @@
class CreateKeyPairs < ActiveRecord::Migration
def change
create_table :key_pairs do |t|
t.integer :repository_id
t.integer :user_id
t.integer :key_id
t.string :public
t.timestamps
end
end
end

View File

@ -184,6 +184,15 @@ ActiveRecord::Schema.define(:version => 20120703101719) do
add_index "issues", ["project_id", "serial_id"], :name => "index_issues_on_project_id_and_serial_id", :unique => true
create_table "key_pairs", :force => true do |t|
t.integer "repository_id"
t.integer "user_id"
t.integer "key_id"
t.string "public"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "labelings", :force => true do |t|
t.integer "label_id", :null => false
t.integer "issue_id"

View File

@ -99,4 +99,17 @@ class BuildServer
def self.freeze platform_name
self.client.call('freeze_platform', platform_name)
end
# Repository key pair calls
def self.import_gpg_key_pair key_pub, key_secret
self.client.call('import_gpg_key_pair', key_pub, key_secret)
end
def self.set_repository_key platform, repository, key_id
self.client.call('set_repository_key', platform, repository, key_id)
end
def self.rm_repository_key platform, repository
self.client.call('rm_repository_key', platform, repository)
end
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe Platforms::KeyPairsController do
end

View File

@ -0,0 +1,15 @@
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the Platforms::KeyPairsHelper. For example:
#
# describe Platforms::KeyPairsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe Platforms::KeyPairsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,5 @@
require 'spec_helper'
describe KeyPair do
pending "add some examples to (or delete) #{__FILE__}"
end