[refs #441] Refactring, fix errors

This commit is contained in:
Vladimir Sharshov 2012-07-30 23:07:49 +04:00
parent 844e81a73d
commit 89d67f9ea6
6 changed files with 61 additions and 38 deletions

View File

@ -10,7 +10,7 @@ class Platforms::KeyPairsController < ApplicationController
def create def create
@key_pair.user_id = current_user.id @key_pair.user_id = current_user.id
if @key_pair.key_create_call == true if @key_pair.save
flash[:notice] = t('flash.key_pairs.saved') flash[:notice] = t('flash.key_pairs.saved')
else else
flash[:error] = t('flash.key_pairs.save_error') flash[:error] = t('flash.key_pairs.save_error')
@ -21,7 +21,7 @@ class Platforms::KeyPairsController < ApplicationController
end end
def destroy def destroy
if @key_pair.rm_key_call if @key_pair.destroy
flash[:notice] = t('flash.key_pairs.destroyed') flash[:notice] = t('flash.key_pairs.destroyed')
else else
flash[:error] = t('flash.key_pairs.destroy_error') flash[:error] = t('flash.key_pairs.destroy_error')

View File

@ -5,25 +5,29 @@ class KeyPair < ActiveRecord::Base
attr_accessor :secret attr_accessor :secret
attr_accessible :public, :secret, :repository_id attr_accessible :public, :secret, :repository_id
after_create :key_create_call validates :repository_id, :public, :user_id, :presence => true
validates :secret, :presence => true, :on => :create
def key_create_call validates :repository_id, :uniqueness => {:message => I18n.t("activerecord.errors.key_pairs.repo_key_exists")}
if KeyPair.exists? :repository_id => self.repository_id
errors.add(:repository_id, I18n.t('flash.key_pairs.key_exists')) before_create :key_create_call
return false before_destroy :rm_key_call
protected
def key_create_call
result, self.key_id = BuildServer.import_gpg_key_pair(public, secret)
raise "Failed to create key_pairs for repository #{repository_id} with code #{result}." unless result == 4
if result != 0 || key_id.nil?
errors.add(:public, I18n.t("activerecord.errors.key_pairs.#{result}"))
return false
end
result = BuildServer.set_repository_key(repository_id, repository.platform_id, key_id)
raise "Failed to sign repository key #{repository_id} with code #{set_code}." unless result.zero?
end end
code, self.key_id = BuildServer.import_gpg_key_pair(public, secret) def rm_key_call
if code.zero? result = BuildServer.rm_repository_key(repository.platform_id, repository_id)
set_code = BuildServer.set_repository_key(repository_id, repository.platform_id, key_id) raise "Failed to desroy repository key #{repository_id} with code #{result}." unless result.zero?
set_code.zero? ? self.save : set_code
else
code
end end
end
def rm_key_call
return self.destroy if BuildServer.rm_repository_key(repository.platform_id, repository_id) == 0
false
end
end end

View File

@ -4,10 +4,10 @@
= form_for :key_pair, :url => platform_key_pairs_path(@platform), :method => :post, :html => { :class => :form } do |f| = 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 .leftlist= f.label :public, t("activerecord.attributes.key_pair.public"), :class => :label
.rightlist= f.text_field :public, :class => 'text_field' .rightlist= f.text_area :public, :class => 'text_field resizable', :cols => 80
.both .both
.leftlist= f.label :secret, t("activerecord.attributes.key_pair.secret"), :class => :label .leftlist= f.label :secret, t("activerecord.attributes.key_pair.secret"), :class => :label
.rightlist= f.text_field :secret, :class => 'text_field' .rightlist= f.text_area :secret, :class => 'text_field resizable', :cols => 80
.both .both
.leftlist= f.label :repository_id, t("activerecord.attributes.key_pair.repository_id"), :class => :label .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') .rightlist= f.select :repository_id, options_from_collection_for_select(@platform.repositories, 'id', 'name')

View File

@ -5,16 +5,21 @@ en:
user_id: User user_id: User
public: Public key public: Public key
secret: Secret key secret: Secret key
confirm_delete: Are you sure you want to delete this key pair? confirm_delete: Are you sure you want to delete this signature?
header: Key Pairs header: Signatures
flash: flash:
key_pairs: key_pairs:
saved: Key pair succefully created saved: Repository successfully signed
save_error: Key pair save error save_error: Signature save error
destroyed: Key pair succefully destroyed destroyed: Signature succefully destroyed
destroy_error: Key pair destroy error destroy_error: Signature destroy error
key_exists: has one key pair already!
activerecord: activerecord:
errors:
key_pair:
repo_key_exists: Repository has been signed already! Please remove old signature and try again
1: could not import public key
2: could not import secret key
3: keys are imported, but it is not a key pair (ids differ)
models: models:
key_pair: Key Pair key_pair: Key Pair
attributes: attributes:

View File

@ -1,22 +1,27 @@
en: ru:
layout: layout:
key_pairs: key_pairs:
repository_id: Репозиторий repository_id: Репозиторий
user_id: Пользователь user_id: Пользователь
public: Публичный ключ public: Публичный ключ
secret: Секретный ключ secret: Секретный ключ
confirm_delete: Вы уверены, что хотите удалить эту ключевую пару? confirm_delete: Вы уверены, что хотите удалить подпись?
header: Ключевые Пары header: Подписи
flash: flash:
key_pairs: key_pairs:
saved: Ключевая пара сохранена успешно saved: Репозиторий успешно подписан
save_error: Ошибка создания ключевой пары save_error: Ошибка создания подписи
destroyed: Ключевая пара удалена успешно destroyed: Подпись удалена успешно
destroy_error: Ошибка удаления ключевой пары destroy_error: Ошибка удаления подписи
key_exists: уже имеет одну ключевую пару!
activerecord: activerecord:
errors:
key_pair:
repo_key_exists: Репозиторий уже подписан! Пожалуйста, удалите старую подпись и попробуйте снова
1: Проблемы с импортром публичного ключа
2: Проблемы с импортром секретного ключа
3: Ключи импортированы, но не являются парой (идентификаторы не совпадают)
models: models:
key_pair: Ключевая пара key_pair: Подпись
attributes: attributes:
key_pair: key_pair:
id: Id id: Id
@ -25,4 +30,4 @@ en:
user_id: Пользователь user_id: Пользователь
repository_id: Репозиторий repository_id: Репозиторий
public: Публичный ключ public: Публичный ключ
secret: Секретный ключ secret: Секретный ключ

View File

@ -0,0 +1,9 @@
class SetStringTypeForKeyPairsKeyid < ActiveRecord::Migration
def up
change_column :key_pairs, :key_id, :string
end
def down
change_column :key_pairs, :key_id, :integer
end
end