zuora_connect 0.0.5.6 → 0.0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d75f9ed98e63d0fc10ad01c42cab2ef742f1a999
4
- data.tar.gz: c0458d7d31cfc53e81e4cc0bf01d157f28709dd3
3
+ metadata.gz: c71fd050560a8874be39b40ffb67748135fb58a5
4
+ data.tar.gz: 64e34408cded45de75c19eeb10b0b0206e69bf84
5
5
  SHA512:
6
- metadata.gz: e65ce18868b73e80d407bf6bb64b97f3db4dd394cdb523f4fa87b6c4e2947e1635b3d4bb94d2b56b21eff63b278c8f7d885d6b19fedb2cb62cf6a705b249e674
7
- data.tar.gz: 44c17338a21b285a94bc0fb839f83b8e84e81cf20097780ae8f40f46efe6a660c88f96241e8a6acc4cba7862e37ddda20a19817dbf6e8d3cfdd99cbe0144eb05
6
+ metadata.gz: ff693b4d0094d05711f83bc5a6002664fa7166c3e7238e4e49377611c75c9bdf81b783d71a3642d66df88ba3612e47e27bdf04a8a7fbcc1bf5b295b9278c236d
7
+ data.tar.gz: 75426091080e23acf1b0c176d027b6caccbb5bf941794754375e28efccea47618c4973e1e6bb4856b251bf6251ee8acadce966349303e4bd3d119a100c4589c6
@@ -1,4 +1,5 @@
1
1
  module ZuoraConnect
2
2
  module ApplicationHelper
3
+
3
4
  end
4
5
  end
@@ -1,22 +1,24 @@
1
1
  module ZuoraConnect
2
2
  class AppInstance < ActiveRecord::Base
3
- attr_accessor :user, :pass, :options, :mode, :logins, :valid
3
+ after_initialize :init
4
+ attr_accessor :user, :pass, :options, :mode, :logins, :valid, :task_data, :last_refresh
4
5
 
5
- def new_session(user, password)
6
- @user = user
7
- @password = password
8
- response = JSON.parse(HTTParty.get(ZuoraConnect.configuration.url + "/api/v1/tools/tasks/#{self.id}.json",:basic_auth => auth = {:username => user, :password => password}).body)
9
- @mode = response["mode"]
6
+ def init
10
7
  @options = Hash.new
11
8
  @logins = Hash.new
12
- response.each do |k,v|
13
- if k.match(/^(.*)_login$/)
14
- @logins[k] = v
15
- elsif k == "options"
16
- v.each do |opt|
17
- @options[opt["config_name"]] = opt
18
- end
19
- end
9
+ Apartment::Migrator.migrate(self.id)
10
+ Apartment::Tenant.switch!(self.id)
11
+ end
12
+
13
+ def new_session(user, password, session = nil)
14
+ @user = user
15
+ @pass = password
16
+ if session.nil? || (!session.nil? && self.id != session["appInstance"].to_i) || session["#{self.id}::task_data"].blank? || ( session["#{self.id}::last_refresh"].blank? || session["#{self.id}::last_refresh"].to_i < ZuoraConnect.configuration.timeout.ago.to_i )
17
+ Rails.logger.debug("REFRESHING")
18
+ self.refresh(session)
19
+ else
20
+ Rails.logger.debug("REBUILDING")
21
+ build_task(session["#{self.id}::task_data"], session)
20
22
  end
21
23
  @valid = true
22
24
  end
@@ -25,11 +27,37 @@ module ZuoraConnect
25
27
  if @user && @pass
26
28
  return HTTParty.get(ZuoraConnect.configuration.url + "/api/v1/tools/application_options/#{optionId}/edit?value=#{value}",:basic_auth => auth = {:username => user, :password => password})
27
29
  else
28
- puts "Username or password is nil"
29
30
  return false
30
31
  end
31
32
  end
32
33
 
34
+ def refresh(session = nil)
35
+ response = HTTParty.get(ZuoraConnect.configuration.url + "/api/v1/tools/tasks/#{self.id}.json",:basic_auth => auth = {:username => self.user, :password => self.pass})
36
+ if response.code == 200
37
+ @last_refresh = Time.now.to_i
38
+ build_task(JSON.parse(response.body), session)
39
+ else
40
+ raise "Unauthorized"
41
+ end
42
+ end
43
+
44
+ def build_task(task_data, session)
45
+ @task_data = task_data
46
+ @mode = @task_data["mode"]
47
+ @task_data.each do |k,v|
48
+ if k.match(/^(.*)_login$/)
49
+ tmp = ZuoraConnect::Login.new(v)
50
+ tmp.client.current_session = session["#{self.id}::#{k}:session"] if !session.nil? && v["tenant_type"] == "Zuora" && session["#{self.id}::#{k}:session"]
51
+ @logins[k] = tmp
52
+ self.attr_builder(k, @logins[k])
53
+ elsif k == "options"
54
+ v.each do |opt|
55
+ @options[opt["config_name"]] = opt
56
+ end
57
+ end
58
+ end
59
+ end
60
+
33
61
  def send_email
34
62
 
35
63
  end
@@ -37,5 +65,10 @@ module ZuoraConnect
37
65
  def self.decrypt_response(resp)
38
66
  OpenSSL::PKey::RSA.new(ZuoraConnect.configuration.private_key).private_decrypt(resp)
39
67
  end
68
+
69
+ def attr_builder(field,val)
70
+ singleton_class.class_eval { attr_accessor "#{field}" }
71
+ send("#{field}=", val)
72
+ end
40
73
  end
41
74
  end
@@ -0,0 +1,17 @@
1
+ module ZuoraConnect
2
+ class Login
3
+
4
+ def initialize (fields)
5
+ self.attr_builder("client",::ZuoraAPI::Login.new(fields.map{|k,v| [k.to_sym, v]}.to_h)) if fields["tenant_type"] == "Zuora"
6
+ fields.each do |k,v|
7
+ self.attr_builder(k,v)
8
+ end
9
+ end
10
+
11
+ def attr_builder(field,val)
12
+ singleton_class.class_eval { attr_accessor "#{field}" }
13
+ send("#{field}=", val)
14
+ end
15
+
16
+ end
17
+ end
@@ -47,7 +47,7 @@ Apartment.configure do |config|
47
47
  # end
48
48
  # end
49
49
  #
50
- config.tenant_names = lambda { ZuoraConnect::AppInstance.pluck :database }
50
+ config.tenant_names = lambda { ZuoraConnect::AppInstance.pluck :id }
51
51
  config.excluded_models = ["ZuoraConnect::AppInstance"]
52
52
 
53
53
  #
@@ -56,7 +56,7 @@ Apartment.configure do |config|
56
56
  # Specifies whether to use PostgreSQL schemas or create a new database per Tenant.
57
57
  # The default behaviour is true.
58
58
  #
59
- # config.use_schemas = true
59
+ config.use_schemas = true
60
60
 
61
61
  # Apartment can be forced to use raw SQL dumps instead of schema.rb for creating new schemas.
62
62
  # Use this when you are using some extra features in PostgreSQL that can't be respresented in
data/lib/zuora_connect.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'zuora_connect/configuration'
2
2
  require "zuora_connect/engine"
3
3
  require 'zuora_connect/controllers/helpers'
4
-
4
+ require 'zuora_connect/views/helpers'
5
5
  module ZuoraConnect
6
6
  class << self
7
7
  attr_accessor :configuration
@@ -10,6 +10,10 @@ module ZuoraConnect
10
10
  autoload :Helpers, 'zuora_connect/controllers/helpers'
11
11
  end
12
12
 
13
+ module Views
14
+ ActionView::Base.send(:include, Helpers)
15
+ end
16
+
13
17
  def self.configuration
14
18
  @configuration ||= Configuration.new
15
19
  end
@@ -1,9 +1,10 @@
1
1
  module ZuoraConnect
2
2
  class Configuration
3
- attr_accessor :url, :private_key, :dev_mode_logins,:dev_mode_mode, :dev_mode_options, :mode
3
+ attr_accessor :url, :private_key, :dev_mode_logins,:dev_mode_mode, :dev_mode_options, :mode, :timeout
4
4
 
5
5
  def initialize
6
- @url = "https://connect.zuora.com"
6
+ @timeout = 5.minutes
7
+ @url = "http://localhost:3000"
7
8
  @mode = "Production"
8
9
  @dev_mode_logins = { "target_login" => {"tenant_type" => "Zuora", "username" => "user", "password" => "pass", "url" => "url"} }
9
10
  @dev_mode_options = {"name" => {"config_name" => "name", "datatype" => "type", "value" => "value"}}
@@ -4,20 +4,28 @@ module ZuoraConnect
4
4
  module Helpers
5
5
  extend ActiveSupport::Concern
6
6
 
7
- def authenticate_request
7
+
8
+ def authenticate_connect_app_request
8
9
  if ZuoraConnect.configuration.mode == "Production"
9
- data_present = set_session
10
- if !session["valid"] && data_present
11
- res = HTTParty.get(ZuoraConnect.configuration.url + "/api/v1/tools/tasks/#{session["appInstance"]}.json",:basic_auth => auth = {:username => session["user"], :password => session["key"]})
12
- if res.code == 200
13
- session["valid"] = true
10
+ instance_id = process_encrypted_response
11
+
12
+
13
+ if session["appInstance"]
14
+ if ZuoraConnect::AppInstance.where(:id => instance_id).size == 0
15
+ ZuoraConnect::AppInstance.create!(:id => instance_id )
16
+ Apartment::Tenant.create(instance_id)
14
17
  end
15
- end
16
- if !session["valid"]
18
+ @appinstance = ZuoraConnect::AppInstance.find(instance_id)
19
+ begin
20
+ @appinstance.new_session(session["#{instance_id}::user"], session["#{instance_id}::key"], session)
21
+ rescue
22
+ puts "redirect"
23
+ redirect_to "/500.html"
24
+ end
25
+ else
26
+ puts "redirect2"
17
27
  redirect_to "/500.html"
18
28
  end
19
- set_app_instance(session["appInstance"].to_i) if session["appInstance"] || ( session["appInstance"] && Apartment::Tenant.current != session["appInstance"])
20
- @appinstance.new_session(session["user"], session["key"]) if @appinstance && !@appinstance.valid
21
29
  else
22
30
  dev_mode
23
31
  set_app_instance(session["appInstance"].to_i)
@@ -27,7 +35,22 @@ module ZuoraConnect
27
35
  end
28
36
  end
29
37
 
30
- def set_session
38
+ def persist_connect_app_session
39
+ @appinstance.logins.each do |key, login|
40
+ if login.tenant_type == "Zuora"
41
+ session["#{@appinstance.id}::#{key}:session"] = login.client.current_session
42
+ end
43
+ end
44
+ session["#{@appinstance.id}::task_data"] = @appinstance.task_data
45
+ session["#{@appinstance.id}::last_refresh"] = @appinstance.last_refresh
46
+ end
47
+
48
+ def check_connect_admin
49
+ redirect_to "/500.html" if !session["#{@appinstance.id}::admin"]
50
+ end
51
+
52
+ private
53
+ def process_encrypted_response
31
54
  session["valid"] |= false
32
55
  if request["data"]
33
56
  values = JSON.parse(ZuoraConnect::AppInstance.decrypt_response(Base64.urlsafe_decode64(request["data"])))
@@ -36,36 +59,22 @@ module ZuoraConnect
36
59
  params[k] = v
37
60
  end
38
61
  end
39
- session["user"] = values["user"]
40
- session["key"] = values["key"]
41
- session["valid"] = false
62
+ session["#{values["appInstance"]}::user"] = values["user"]
63
+ session["#{values["appInstance"]}::key"] = values["key"]
64
+ session["#{values["appInstance"]}::valid"] = false
42
65
  session["appInstance"] = values["appInstance"]
43
- session["admin"] = values["admin"] ? values["admin"] : false
44
- return true
66
+ session["#{values["appInstance"]}::admin"] = values["admin"] ? values["admin"] : false
45
67
  end
46
- return false
68
+ return session["appInstance"]
47
69
  end
48
70
 
49
71
  def dev_mode
50
72
  session["appInstance"] = "1"
51
- session["user"] = "test"
52
- session["key"] = "test"
53
- session["valid"] = true
73
+ session["1::user"] = "test"
74
+ session["1::key"] = "test"
75
+ session["1::valid"] = true
54
76
  end
55
77
 
56
- def set_app_instance(id)
57
- if ZuoraConnect::AppInstance.where(:id => id).size == 0
58
- ZuoraConnect::AppInstance.create!(:id => id )
59
- Apartment::Tenant.create(id)
60
- end
61
- @appinstance = ZuoraConnect::AppInstance.find(id)
62
- Apartment::Migrator.migrate(@appinstance.id)
63
- Apartment::Tenant.switch!(@appinstance.id)
64
- end
65
-
66
- def check_admin
67
- redirect_to "/500.html" if session["admin"] != "true"
68
- end
69
78
  end
70
79
  end
71
80
  end
@@ -1,3 +1,3 @@
1
1
  module ZuoraConnect
2
- VERSION = "0.0.5.6"
2
+ VERSION = "0.0.6.0"
3
3
  end
@@ -0,0 +1,9 @@
1
+ module ZuoraConnect
2
+ module Views
3
+ module Helpers
4
+ def is_app_admin?
5
+ return session["#{@appinstance.id}::admin"]
6
+ end
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zuora_connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5.6
4
+ version: 0.0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Connect Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-19 00:00:00.000000000 Z
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: apartment
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: zuora_api
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: httparty
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -105,6 +119,7 @@ files:
105
119
  - app/helpers/zuora_connect/api/v1/app_instance_helper.rb
106
120
  - app/helpers/zuora_connect/application_helper.rb
107
121
  - app/models/zuora_connect/app_instance.rb
122
+ - app/models/zuora_connect/login.rb
108
123
  - app/views/layouts/zuora_connect/application.html.erb
109
124
  - config/initializers/apartment.rb
110
125
  - config/routes.rb
@@ -115,6 +130,7 @@ files:
115
130
  - lib/zuora_connect/controllers/helpers.rb
116
131
  - lib/zuora_connect/engine.rb
117
132
  - lib/zuora_connect/version.rb
133
+ - lib/zuora_connect/views/helpers.rb
118
134
  - test/controllers/zuora_connect/api/v1/app_instance_controller_test.rb
119
135
  - test/dummy/README.rdoc
120
136
  - test/dummy/Rakefile