diff --git a/lib/bugzilla/user.rb b/lib/bugzilla/user.rb index b131a21..6f104a6 100644 --- a/lib/bugzilla/user.rb +++ b/lib/bugzilla/user.rb @@ -43,6 +43,7 @@ class User < APITemplate =end def session(user, password) + # Using cookies prior to 4.4.3 fname = File.join(ENV['HOME'], '.ruby-bugzilla-cookie.yml') if File.exist?(fname) && File.lstat(fname).mode & 0600 == 0600 then conf = YAML.load(File.open(fname).read) @@ -50,21 +51,46 @@ def session(user, password) cookie = conf[host] unless cookie.nil? then @iface.cookie = cookie - print "Using cookie\n" yield - conf[host] = @iface.cookie - File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)} return end end + + # Using tokens for > 4.4.3 + fname = File.join(ENV['HOME'], '.ruby-bugzilla-token.yml') + if File.exist?(fname) && File.lstat(fname).mode & 0600 == 0600 then + conf = YAML.load(File.open(fname).read) + host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host) + token = conf[host] + unless token.nil? then + @iface.token = token + yield + return + end + end + if user.nil? || password.nil? then yield else login({'login'=>user, 'password'=>password, 'remember'=>true}) yield - logout + + # Using cookies prior to 4.4.3 + unless @iface.cookie.nil? + fname = File.join(ENV['HOME'], '.ruby-bugzilla-cookie.yml') + host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host) + conf = { host => @iface.cookie } + File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)} + end + + # Using tokens for > 4.4.3 + unless @iface.token.nil? + fname = File.join(ENV['HOME'], '.ruby-bugzilla-token.yml') + host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host) + conf = { host => @iface.token } + File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)} + end end - end # def session =begin rdoc @@ -92,7 +118,13 @@ def session(user, password) def _login(cmd, *args) raise ArgumentError, "Invalid parameters" unless args[0].kind_of?(Hash) - @iface.call(cmd,args[0]) + res = @iface.call(cmd,args[0]) + unless res['token'].nil? + @iface.token = res['token'] + end + + return res + end # def _login def _logout(cmd, *args) diff --git a/lib/bugzilla/version.rb b/lib/bugzilla/version.rb index eee6879..c7908ea 100644 --- a/lib/bugzilla/version.rb +++ b/lib/bugzilla/version.rb @@ -26,6 +26,6 @@ module Bugzilla - VERSION = "0.5.1" + VERSION = "0.5.3" end # module Bugzilla diff --git a/lib/bugzilla/xmlrpc.rb b/lib/bugzilla/xmlrpc.rb index 8968c09..ba87076 100644 --- a/lib/bugzilla/xmlrpc.rb +++ b/lib/bugzilla/xmlrpc.rb @@ -51,6 +51,7 @@ def call(cmd, params = {}, user = nil, password = nil) params = {} if params.nil? params['Bugzilla_login'] = user unless user.nil? || password.nil? params['Bugzilla_password'] = password unless user.nil? || password.nil? + params['Bugzilla_token'] = @token unless @token.nil? @xmlrpc.call(cmd, params) end # def call @@ -74,6 +75,26 @@ def cookie=(val) @xmlrpc.cookie = val end # def cookie +=begin rdoc + +==== Bugzilla::XMLRPC#token + +=end + + def token + @token + end # def token + +=begin rdoc + +==== Bugzilla::XMLRPC#token=(val) + +=end + + def token=(val) + @token = val + end # def token + end # class XMLRPC end # module Bugzilla