ref: refs/heads/master commit 32d31d94bed756b2d69e464e18de99695c7d10ab Author: J. Daniel Schmidt <jdsn@suse.de> Date: Tue Oct 20 18:21:38 2009 +0200 make model more rubyish --- .../registration/configuration_controller.rb | 13 ++-- .../registration/registration_controller.rb | 16 ++-- plugins/registration/app/models/register.rb | 94 ++++++++------------ 3 files changed, 51 insertions(+), 72 deletions(-) diff --git a/plugins/registration/app/controllers/registration/configuration_controller.rb b/plugins/registration/app/controllers/registration/configuration_controller.rb index 178570b..7262e53 100644 --- a/plugins/registration/app/controllers/registration/configuration_controller.rb +++ b/plugins/registration/app/controllers/registration/configuration_controller.rb @@ -16,8 +16,8 @@ class Registration::ConfigurationController < ApplicationController req = Hash.new end - newurl = '' - newca = '' + newurl = nil + newca = nil # read registration server url if req['registrationconfig'] && @@ -34,16 +34,17 @@ class Registration::ConfigurationController < ApplicationController end - @register = Register.new( { } ) - @register.set_config newurl, newca + @register = Register.new({}) + @register.registrationserver = newurl if newurl + @register.certificate = newca if newca + @register.save render :show end def show permission_check("org.opensuse.yast.modules.ysr.getregistrationconfig") # do not run registration, only get the config - @register = Register.new( { } ) - @register.get_config + @register = Register.new({}) end def index diff --git a/plugins/registration/app/controllers/registration/registration_controller.rb b/plugins/registration/app/controllers/registration/registration_controller.rb index e5b5a96..e68787b 100644 --- a/plugins/registration/app/controllers/registration/registration_controller.rb +++ b/plugins/registration/app/controllers/registration/registration_controller.rb @@ -21,7 +21,7 @@ class Registration::RegistrationController < ApplicationController req = Hash.new end - valid_context_keys = %w[forcereg nooptional nohwdata yastcall norefresh logfile] + valid_context_keys = %w[forcereg nooptional nohwdata yastcall norefresh logfile].freeze context = Hash.new #puts req.inspect @@ -32,20 +32,17 @@ class Registration::RegistrationController < ApplicationController req['registration']['options'].each do |k, v| case k when 'debug' - context['debugMode'] = v + @register.context['debugMode'] = v when 'restorerepos' - context['restoreRepos'] = v + @register.context['restoreRepos'] = v else - context[k] = v if valid_context_keys.include? k + @register.context[k] = v if valid_context_keys.include? k end end end logger.debug "set context to #{context.inspect}" - raise InvalidParameters.new :context => "Missing" - - # overwrite context data - @register.set_context( context ) + # raise InvalidParameters.new :context => "Missing" # TODO: parse post data and set the arguments # @register.set_arguments( { } ) @@ -59,8 +56,7 @@ class Registration::RegistrationController < ApplicationController def show permission_check("org.opensuse.yast.modules.ysr.getregistrationconfig") # get registration status - @register = Register.new( { } ) - @register.get_config + @register = Register.new({}) render :status end diff --git a/plugins/registration/app/models/register.rb b/plugins/registration/app/models/register.rb index 782534e..1fc1b97 100644 --- a/plugins/registration/app/models/register.rb +++ b/plugins/registration/app/models/register.rb @@ -7,86 +7,68 @@ class Register require 'yast_service' - @context = { } - @arguments = { } - @config = { } + attr_accessor :registrationserver + attr_accessor :certificate + attr_accessor :context + attr_accessor_with_default :arguments, Hash.new + attr_reader :guid + @reg = '' def initialize(hash) # cleanup arguments - @arguments = { } + #self.arguments = Hash.new # initialize context init_context hash + # read the configuration + find end def init_context(hash) # set context defaults - @context = { 'yastcall' => [ 'i', 1 ], - 'norefresh' => [ 'i', 1 ], - 'restoreRepos' => [ 'i', 1 ], - 'forcereg' => [ 'i', 0 ], - 'nohwdata' => [ 'i', 1 ], - 'nooptional' => [ 'i', 1 ], - 'debugMode' => [ 'i', 2 ], - 'logfile' => [ 's', '/root/.suse_register.log' ] } - - # when hash is nil, ignore it - return if hash.nil? - - # merge custom context data - raise "Invalid or missing registration initialization context data." unless hash.is_a?(Hash) - - hash.each do |k, v| - @context.merge!( { k.to_s => ['s', v.to_s] } ) - end + @context = { 'yastcall' => '1', + 'norefresh' => '1', + 'restoreRepos' => '1', + 'forcereg' => '0', + 'nohwdata' => '1', + 'nooptional' => '1', + 'debugMode' => '2', + 'logfile' => '/root/.suse_register.log' } + @context.merge! hash if hash.class == Hash end def find begin - @config = YastService.Call("YSR::getregistrationconfig") + config = YastService.Call("YSR::getregistrationconfig") + @registrationserver = config['regserverurl'] + @certificate = config['regserverca'] + @guid = config['guid'] rescue Exception => e Rails.logger.error "YastService.Call('YSR::getregistrationconfig') failed" raise end - @config - end - - def set_context(hash) - self.init_context hash - end - - def add_argument(key, value) - @arguments.merge!( { key.to_s => [ 'a{ss}',{ 'value' => value.to_s } ] } ) - end - - def add_arguments(hash) - hash.each {|k, v| self.add_argument k, v } - end - - def set_arguments(hash) - @arguments = {} - self.add_arguments hash + config end - def register # don't know how to pass only one hash, so split it into two. FIXME change later if possible! - # @reg = YastService.Call("YSR::statelessregister", { 'ctx' => @context, 'arguments' => @arguments } ) + # @reg = YastService.Call("YSR::statelessregister", { 'ctx' => ctx, 'arguments' => args } ) - @reg = YastService.Call("YSR::statelessregister", @context, @arguments ) - return @reg['exitcode'] || 99 - end + ctx = Hash.new + self.context.each { |k, v| ctx[k] = [ 's', v.to_s ] } + args = Hash.new + self.arguments.each { |k, v| args[k] = [ 'a{ss}', { 'value' => v.to_s } ] } - def get_config - find + @reg = YastService.Call("YSR::statelessregister", ctx, args ) + return @reg['exitcode'] || 99 end - def set_config(url, ca) - newconfig = { 'regserverurl' => url, - 'regserverca' => ca } + def save + newconfig = { 'regserverurl' => registrationserver, + 'regserverca' => certificate } ret = YastService.Call("YSR::setregistrationconfig", newconfig) - puts "YastService.Call returned: =#{ret}=" - self.get_config +# puts "YastService.Call returned: =#{ret}=" + self.find return ret end @@ -95,7 +77,7 @@ class Register xml.instruct! unless options[:skip_instruct] xml.registration do - xml.tag!(:guid, @config['guid']) if @config['guid'] && @config['guid'].size > 0 + xml.guid @guid if @guid && @guid.size > 0 end end @@ -105,11 +87,11 @@ class Register xml.registrationconfig do xml.server do - xml.tag!(:url, @config['regserverurl'] ) + xml.url self.registrationserver if self.registrationserver end xml.certificate do xml.data do - xml.cdata!(@config['regserverca']) if @config['regserverca'].size > 0 + xml.cdata!(self.certificate) if self.certificate && self.certificate.size > 0 end end end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org