diff --git a/app/controllers/platforms/key_pairs_controller.rb b/app/controllers/platforms/key_pairs_controller.rb index b7fecef5f..c45f53dcb 100644 --- a/app/controllers/platforms/key_pairs_controller.rb +++ b/app/controllers/platforms/key_pairs_controller.rb @@ -10,7 +10,7 @@ class Platforms::KeyPairsController < ApplicationController def create @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') else flash[:error] = t('flash.key_pairs.save_error') @@ -21,7 +21,7 @@ class Platforms::KeyPairsController < ApplicationController end def destroy - if @key_pair.rm_key_call + if @key_pair.destroy flash[:notice] = t('flash.key_pairs.destroyed') else flash[:error] = t('flash.key_pairs.destroy_error') diff --git a/app/models/key_pair.rb b/app/models/key_pair.rb index ec85e53fc..5b752c513 100644 --- a/app/models/key_pair.rb +++ b/app/models/key_pair.rb @@ -5,25 +5,29 @@ class KeyPair < ActiveRecord::Base attr_accessor :secret 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 - if KeyPair.exists? :repository_id => self.repository_id - errors.add(:repository_id, I18n.t('flash.key_pairs.key_exists')) - return false + validates :repository_id, :uniqueness => {:message => I18n.t("activerecord.errors.key_pairs.repo_key_exists")} + + before_create :key_create_call + 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 - 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) - set_code.zero? ? self.save : set_code - else - code + def rm_key_call + result = BuildServer.rm_repository_key(repository.platform_id, repository_id) + raise "Failed to desroy repository key #{repository_id} with code #{result}." unless result.zero? end - end - - def rm_key_call - return self.destroy if BuildServer.rm_repository_key(repository.platform_id, repository_id) == 0 - false - end end diff --git a/app/views/platforms/key_pairs/_new.html.haml b/app/views/platforms/key_pairs/_new.html.haml index c6cc8f0bf..b740fe218 100644 --- a/app/views/platforms/key_pairs/_new.html.haml +++ b/app/views/platforms/key_pairs/_new.html.haml @@ -4,10 +4,10 @@ = 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' + .rightlist= f.text_area :public, :class => 'text_field resizable', :cols => 80 .both .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 .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') diff --git a/config/locales/models/key_pair.en.yml b/config/locales/models/key_pair.en.yml index b7ad9532f..0d55e41d9 100644 --- a/config/locales/models/key_pair.en.yml +++ b/config/locales/models/key_pair.en.yml @@ -5,16 +5,21 @@ en: user_id: User public: Public key secret: Secret key - confirm_delete: Are you sure you want to delete this key pair? - header: Key Pairs + confirm_delete: Are you sure you want to delete this signature? + header: Signatures 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 - key_exists: has one key pair already! + saved: Repository successfully signed + save_error: Signature save error + destroyed: Signature succefully destroyed + destroy_error: Signature destroy error 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: key_pair: Key Pair attributes: diff --git a/config/locales/models/key_pair.ru.yml b/config/locales/models/key_pair.ru.yml index 32f93f356..451fc8b9d 100644 --- a/config/locales/models/key_pair.ru.yml +++ b/config/locales/models/key_pair.ru.yml @@ -1,22 +1,27 @@ -en: +ru: layout: key_pairs: repository_id: Репозиторий user_id: Пользователь public: Публичный ключ secret: Секретный ключ - confirm_delete: Вы уверены, что хотите удалить эту ключевую пару? - header: Ключевые Пары + confirm_delete: Вы уверены, что хотите удалить подпись? + header: Подписи flash: key_pairs: - saved: Ключевая пара сохранена успешно - save_error: Ошибка создания ключевой пары - destroyed: Ключевая пара удалена успешно - destroy_error: Ошибка удаления ключевой пары - key_exists: уже имеет одну ключевую пару! + saved: Репозиторий успешно подписан + save_error: Ошибка создания подписи + destroyed: Подпись удалена успешно + destroy_error: Ошибка удаления подписи activerecord: + errors: + key_pair: + repo_key_exists: Репозиторий уже подписан! Пожалуйста, удалите старую подпись и попробуйте снова + 1: Проблемы с импортром публичного ключа + 2: Проблемы с импортром секретного ключа + 3: Ключи импортированы, но не являются парой (идентификаторы не совпадают) models: - key_pair: Ключевая пара + key_pair: Подпись attributes: key_pair: id: Id @@ -25,4 +30,4 @@ en: user_id: Пользователь repository_id: Репозиторий public: Публичный ключ - secret: Секретный ключ + secret: Секретный ключ \ No newline at end of file diff --git a/db/migrate/20120730185119_set_string_type_for_key_pairs_keyid.rb b/db/migrate/20120730185119_set_string_type_for_key_pairs_keyid.rb new file mode 100644 index 000000000..da6337e16 --- /dev/null +++ b/db/migrate/20120730185119_set_string_type_for_key_pairs_keyid.rb @@ -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