kaui 2.1.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +8 -7
  3. data/Rakefile +4 -2
  4. data/app/assets/config/kaui_manifest.js +45 -0
  5. data/app/assets/javascripts/jquery.spin.js +76 -0
  6. data/app/assets/javascripts/kaui/kaui.js +76 -0
  7. data/app/assets/javascripts/kaui_application.js +25 -0
  8. data/app/assets/javascripts/spin.js +76 -0
  9. data/app/assets/stylesheets/bootstrap_and_overrides.scss +240 -0
  10. data/app/assets/stylesheets/kaui/{account.less → account.scss} +4 -0
  11. data/app/assets/stylesheets/kaui/{audit.less → audit.scss} +5 -1
  12. data/app/assets/stylesheets/kaui/{common.less → common.scss} +17 -15
  13. data/app/assets/stylesheets/kaui/{datatable.less → datatable.scss} +6 -3
  14. data/app/assets/stylesheets/kaui/{header.less → header.scss} +16 -12
  15. data/app/assets/stylesheets/kaui/{home.less → home.scss} +7 -4
  16. data/app/assets/stylesheets/kaui/{invoice.less → invoice.scss} +7 -3
  17. data/app/assets/stylesheets/kaui/kaui.scss +265 -0
  18. data/app/assets/stylesheets/kaui/kaui_bootstrap.scss +3 -0
  19. data/app/assets/stylesheets/kaui/{overdue.less → overdue.scss} +3 -1
  20. data/app/assets/stylesheets/kaui/{payment.less → payment.scss} +3 -0
  21. data/app/assets/stylesheets/kaui/{subscription.less → subscription.scss} +4 -1
  22. data/app/assets/stylesheets/kaui/{tags.less → tags.scss} +9 -5
  23. data/app/assets/stylesheets/kaui/timeline.scss +7 -0
  24. data/app/assets/stylesheets/kaui/tooltip.scss +15 -0
  25. data/app/controllers/kaui/account_children_controller.rb +25 -28
  26. data/app/controllers/kaui/account_custom_fields_controller.rb +16 -15
  27. data/app/controllers/kaui/account_emails_controller.rb +28 -25
  28. data/app/controllers/kaui/account_tags_controller.rb +37 -34
  29. data/app/controllers/kaui/account_timelines_controller.rb +38 -33
  30. data/app/controllers/kaui/accounts_controller.rb +268 -250
  31. data/app/controllers/kaui/admin_allowed_users_controller.rb +105 -99
  32. data/app/controllers/kaui/admin_controller.rb +32 -31
  33. data/app/controllers/kaui/admin_tenants_controller.rb +434 -388
  34. data/app/controllers/kaui/audit_logs_controller.rb +86 -83
  35. data/app/controllers/kaui/bundle_tags_controller.rb +24 -20
  36. data/app/controllers/kaui/bundles_controller.rb +96 -82
  37. data/app/controllers/kaui/chargebacks_controller.rb +36 -33
  38. data/app/controllers/kaui/charges_controller.rb +23 -20
  39. data/app/controllers/kaui/credits_controller.rb +28 -25
  40. data/app/controllers/kaui/custom_fields_controller.rb +172 -38
  41. data/app/controllers/kaui/engine_controller.rb +2 -1
  42. data/app/controllers/kaui/engine_controller_util.rb +128 -117
  43. data/app/controllers/kaui/home_controller.rb +230 -217
  44. data/app/controllers/kaui/invoice_items_controller.rb +43 -39
  45. data/app/controllers/kaui/invoice_tags_controller.rb +24 -20
  46. data/app/controllers/kaui/invoices_controller.rb +132 -117
  47. data/app/controllers/kaui/login_proxy_controller.rb +2 -1
  48. data/app/controllers/kaui/payment_methods_controller.rb +102 -94
  49. data/app/controllers/kaui/payments_controller.rb +95 -92
  50. data/app/controllers/kaui/queues_controller.rb +47 -14
  51. data/app/controllers/kaui/refunds_controller.rb +36 -33
  52. data/app/controllers/kaui/registrations_controller.rb +7 -7
  53. data/app/controllers/kaui/role_definitions_controller.rb +17 -14
  54. data/app/controllers/kaui/sessions_controller.rb +7 -4
  55. data/app/controllers/kaui/subscriptions_controller.rb +197 -187
  56. data/app/controllers/kaui/tag_definitions_controller.rb +28 -27
  57. data/app/controllers/kaui/tags_controller.rb +25 -22
  58. data/app/controllers/kaui/tenants_controller.rb +30 -28
  59. data/app/controllers/kaui/transactions_controller.rb +43 -38
  60. data/app/helpers/kaui/account_helper.rb +16 -14
  61. data/app/helpers/kaui/application_helper.rb +8 -3
  62. data/app/helpers/kaui/date_helper.rb +4 -3
  63. data/app/helpers/kaui/home_helper.rb +2 -0
  64. data/app/helpers/kaui/locale_helper.rb +7 -6
  65. data/app/helpers/kaui/money_helper.rb +2 -1
  66. data/app/helpers/kaui/object_helper.rb +11 -10
  67. data/app/helpers/kaui/payment_helper.rb +7 -6
  68. data/app/helpers/kaui/payment_method_helper.rb +3 -2
  69. data/app/helpers/kaui/permissions_helper.rb +9 -9
  70. data/app/helpers/kaui/plugin_helper.rb +25 -23
  71. data/app/helpers/kaui/subscription_helper.rb +27 -29
  72. data/app/helpers/kaui/us_states_helper.rb +7 -6
  73. data/app/helpers/kaui/uuid_helper.rb +8 -6
  74. data/app/models/kaui/ability.rb +19 -12
  75. data/app/models/kaui/account.rb +45 -28
  76. data/app/models/kaui/account_email.rb +23 -20
  77. data/app/models/kaui/account_timeline.rb +5 -1
  78. data/app/models/kaui/admin.rb +65 -64
  79. data/app/models/kaui/admin_tenant.rb +81 -82
  80. data/app/models/kaui/allowed_user.rb +13 -9
  81. data/app/models/kaui/allowed_user_tenant.rb +4 -2
  82. data/app/models/kaui/application_record.rb +2 -0
  83. data/app/models/kaui/audit_log.rb +6 -3
  84. data/app/models/kaui/base.rb +13 -7
  85. data/app/models/kaui/bundle.rb +41 -24
  86. data/app/models/kaui/catalog.rb +118 -113
  87. data/app/models/kaui/chargeback.rb +5 -1
  88. data/app/models/kaui/credit.rb +4 -1
  89. data/app/models/kaui/custom_field.rb +9 -6
  90. data/app/models/kaui/invoice.rb +25 -22
  91. data/app/models/kaui/invoice_item.rb +9 -6
  92. data/app/models/kaui/invoice_payment.rb +49 -54
  93. data/app/models/kaui/killbill_authenticatable.rb +14 -8
  94. data/app/models/kaui/killbill_registerable.rb +7 -0
  95. data/app/models/kaui/overdue.rb +64 -65
  96. data/app/models/kaui/payment.rb +22 -19
  97. data/app/models/kaui/payment_method.rb +48 -39
  98. data/app/models/kaui/payment_state.rb +41 -38
  99. data/app/models/kaui/rails_methods.rb +9 -9
  100. data/app/models/kaui/refund.rb +7 -1
  101. data/app/models/kaui/role_definition.rb +5 -1
  102. data/app/models/kaui/simple_plan.rb +5 -3
  103. data/app/models/kaui/subscription.rb +10 -7
  104. data/app/models/kaui/tag.rb +25 -22
  105. data/app/models/kaui/tag_definition.rb +50 -45
  106. data/app/models/kaui/tenant.rb +6 -5
  107. data/app/models/kaui/transaction.rb +43 -31
  108. data/app/models/kaui/user.rb +10 -16
  109. data/app/models/kaui/user_role.rb +8 -5
  110. data/app/views/kaui/accounts/_billing_info.html.erb +1 -1
  111. data/app/views/kaui/accounts/_form.html.erb +2 -2
  112. data/app/views/kaui/accounts/index.html.erb +2 -0
  113. data/app/views/kaui/admin_allowed_users/_form.html.erb +3 -3
  114. data/app/views/kaui/admin_tenants/_form_catalog_translation.erb +1 -1
  115. data/app/views/kaui/admin_tenants/_form_invoice_template.erb +1 -1
  116. data/app/views/kaui/admin_tenants/_form_invoice_translation.erb +1 -1
  117. data/app/views/kaui/admin_tenants/_show_catalog_simple.erb +11 -5
  118. data/app/views/kaui/admin_tenants/new_catalog.html.erb +2 -2
  119. data/app/views/kaui/admin_tenants/new_overdue_config.html.erb +2 -2
  120. data/app/views/kaui/admin_tenants/new_plan_currency.html.erb +1 -1
  121. data/app/views/kaui/bundles/index.html.erb +1 -1
  122. data/app/views/kaui/bundles/transfer.html.erb +13 -5
  123. data/app/views/kaui/invoices/_invoice_table.html.erb +1 -1
  124. data/app/views/kaui/layouts/kaui_flash.html.erb +24 -0
  125. data/app/views/kaui/layouts/kaui_header.html.erb +19 -2
  126. data/app/views/kaui/payment_methods/_payment_methods_details_table.html.erb +13 -4
  127. data/app/views/kaui/payments/_payment_table.html.erb +21 -11
  128. data/app/views/kaui/payments/index.html.erb +9 -9
  129. data/app/views/kaui/queues/index.html.erb +1 -1
  130. data/app/views/kaui/subscriptions/_cancel_by_date_modal.html.erb +1 -4
  131. data/app/views/kaui/subscriptions/_edit_form.html.erb +1 -1
  132. data/app/views/kaui/subscriptions/_form.html.erb +1 -1
  133. data/app/views/kaui/subscriptions/_subscriptions_table.html.erb +3 -3
  134. data/app/views/kaui/subscriptions/edit_bcd.erb +1 -1
  135. data/app/views/kaui/tag_definitions/index.html.erb +2 -2
  136. data/app/views/kaui/tags/index.html.erb +1 -0
  137. data/config/initializers/bootstrap_datepicker_rails.rb +3 -0
  138. data/config/initializers/devise.rb +8 -6
  139. data/config/initializers/js_routes.rb +9 -0
  140. data/config/initializers/kilbill_client_init.rb +13 -0
  141. data/config/initializers/killbill_authenticatable.rb +71 -21
  142. data/config/initializers/killbill_registerable.rb +5 -3
  143. data/config/initializers/time_formats.rb +6 -4
  144. data/config/locales/en.yml +78 -1
  145. data/config/routes.rb +34 -32
  146. data/db/migrate/20130812155313_devise_create_kaui_users.rb +10 -8
  147. data/db/migrate/20150109214021_create_kaui_tenants.rb +10 -8
  148. data/db/migrate/20150112232813_create_kaui_allowed_users.rb +10 -8
  149. data/lib/generators/kaui/install/install_generator.rb +23 -20
  150. data/lib/generators/kaui/install/templates/config/initializers/kaui.rb +3 -1
  151. data/lib/kaui/engine.rb +17 -5
  152. data/lib/kaui/installer/installer.rb +27 -26
  153. data/lib/kaui/version.rb +3 -1
  154. data/lib/kaui.rb +28 -30
  155. data/lib/tasks/kaui_tasks.rake +1 -0
  156. metadata +136 -438
  157. data/app/assets/javascripts/application.js +0 -17
  158. data/app/assets/javascripts/kaui/bootstrap-tweaks.js +0 -172
  159. data/app/assets/stylesheets/bootstrap_and_overrides.css.less +0 -44
  160. data/app/assets/stylesheets/kaui/kaui.less +0 -21
  161. data/app/assets/stylesheets/kaui/timeline.less +0 -4
  162. data/config/initializers/asset.rb +0 -1
  163. data/config/initializers/kilbill-client-init.rb +0 -9
  164. data/db/schema.rb +0 -15
  165. data/test/dummy/README.rdoc +0 -261
  166. data/test/dummy/Rakefile +0 -7
  167. data/test/dummy/app/controllers/application_controller.rb +0 -9
  168. data/test/dummy/app/helpers/application_helper.rb +0 -2
  169. data/test/dummy/bin/bundle +0 -3
  170. data/test/dummy/bin/rails +0 -4
  171. data/test/dummy/bin/rake +0 -4
  172. data/test/dummy/bin/setup +0 -38
  173. data/test/dummy/bin/update +0 -29
  174. data/test/dummy/bin/yarn +0 -11
  175. data/test/dummy/config/application.rb +0 -18
  176. data/test/dummy/config/boot.rb +0 -3
  177. data/test/dummy/config/database.yml +0 -27
  178. data/test/dummy/config/environment.rb +0 -5
  179. data/test/dummy/config/environments/development.rb +0 -58
  180. data/test/dummy/config/environments/production.rb +0 -91
  181. data/test/dummy/config/environments/test.rb +0 -49
  182. data/test/dummy/config/initializers/application_controller_renderer.rb +0 -6
  183. data/test/dummy/config/initializers/assets.rb +0 -14
  184. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  185. data/test/dummy/config/initializers/cookies_serializer.rb +0 -5
  186. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  187. data/test/dummy/config/initializers/inflections.rb +0 -16
  188. data/test/dummy/config/initializers/killbill_client.rb +0 -1
  189. data/test/dummy/config/initializers/mime_types.rb +0 -4
  190. data/test/dummy/config/initializers/money.rb +0 -2
  191. data/test/dummy/config/initializers/new_framework_defaults_5_1.rb +0 -14
  192. data/test/dummy/config/initializers/secret_token.rb +0 -7
  193. data/test/dummy/config/initializers/session_store.rb +0 -8
  194. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  195. data/test/dummy/config/locales/en.yml +0 -33
  196. data/test/dummy/config/routes.rb +0 -3
  197. data/test/dummy/config/secrets.yml +0 -32
  198. data/test/dummy/config/symmetric-encryption.yml +0 -135
  199. data/test/dummy/config.ru +0 -4
  200. data/test/dummy/db/schema.rb +0 -52
  201. data/test/dummy/public/404.html +0 -26
  202. data/test/dummy/public/422.html +0 -26
  203. data/test/dummy/public/500.html +0 -25
  204. data/test/dummy/public/favicon.ico +0 -0
  205. data/test/dummy/script/rails +0 -6
  206. data/test/fixtures/SpyCarAdvanced.xml +0 -824
  207. data/test/fixtures/catalog-v1.xml +0 -73
  208. data/test/fixtures/catalog_translation_fr-v1.properties +0 -1
  209. data/test/fixtures/invoice_template-v1.html +0 -100
  210. data/test/fixtures/invoice_translation_fr-v1.properties +0 -23
  211. data/test/fixtures/overdue-v1.xml +0 -49
  212. data/test/fixtures/stripe.yml +0 -2
  213. data/test/functional/kaui/account_children_controller_test.rb +0 -32
  214. data/test/functional/kaui/account_custom_fields_controller_test.rb +0 -29
  215. data/test/functional/kaui/account_emails_controller_test.rb +0 -55
  216. data/test/functional/kaui/account_tags_controller_test.rb +0 -48
  217. data/test/functional/kaui/account_timelines_controller_test.rb +0 -20
  218. data/test/functional/kaui/accounts_controller_test.rb +0 -297
  219. data/test/functional/kaui/admin_allowed_users_controller_test.rb +0 -234
  220. data/test/functional/kaui/admin_controller_test.rb +0 -45
  221. data/test/functional/kaui/admin_tenants_controller_test.rb +0 -352
  222. data/test/functional/kaui/audit_logs_controller_test.rb +0 -71
  223. data/test/functional/kaui/bundle_tags_controller_test.rb +0 -23
  224. data/test/functional/kaui/bundles_controller_test.rb +0 -130
  225. data/test/functional/kaui/chargebacks_controller_test.rb +0 -42
  226. data/test/functional/kaui/charges_controller_test.rb +0 -60
  227. data/test/functional/kaui/credits_controller_test.rb +0 -54
  228. data/test/functional/kaui/custom_fields_controller_test.rb +0 -52
  229. data/test/functional/kaui/functional_test_helper.rb +0 -13
  230. data/test/functional/kaui/functional_test_helper_nosetup.rb +0 -120
  231. data/test/functional/kaui/home_controller_test.rb +0 -407
  232. data/test/functional/kaui/invoice_items_controller_test.rb +0 -82
  233. data/test/functional/kaui/invoices_controller_test.rb +0 -88
  234. data/test/functional/kaui/login_proxy_controller_test.rb +0 -12
  235. data/test/functional/kaui/payment_methods_controller_test.rb +0 -52
  236. data/test/functional/kaui/payments_controller_test.rb +0 -59
  237. data/test/functional/kaui/queues_controller_test.rb +0 -17
  238. data/test/functional/kaui/refunds_controller_test.rb +0 -63
  239. data/test/functional/kaui/registrations_controller_test.rb +0 -49
  240. data/test/functional/kaui/role_definitions_controller_test.rb +0 -47
  241. data/test/functional/kaui/subscriptions_controller_test.rb +0 -194
  242. data/test/functional/kaui/tag_definitions_controller_test.rb +0 -29
  243. data/test/functional/kaui/tags_controller_test.rb +0 -21
  244. data/test/functional/kaui/tenants_controller_test.rb +0 -60
  245. data/test/functional/kaui/transactions_controller_test.rb +0 -90
  246. data/test/integration/kaui/date_helper_integration_test.rb +0 -16
  247. data/test/integration/kaui/integration_test_helper.rb +0 -32
  248. data/test/integration/kaui/navigation_test.rb +0 -43
  249. data/test/killbill_test_helper.rb +0 -219
  250. data/test/test_helper.rb +0 -22
  251. data/test/unit/helpers/kaui/admin_allowed_users_helper_test.rb +0 -6
  252. data/test/unit/helpers/kaui/admin_tenants_helper_test.rb +0 -6
  253. data/test/unit/helpers/kaui/payment_method_helper_test.rb +0 -17
  254. data/test/unit/helpers/kaui/tenants_helper_test.rb +0 -6
  255. data/test/unit/helpers/kaui/uuid_helper_test.rb +0 -16
  256. data/test/unit/kaui/account_email_test.rb +0 -19
  257. data/test/unit/kaui/account_test.rb +0 -14
  258. data/test/unit/kaui/admin_tenant_test.rb +0 -132
  259. data/test/unit/kaui/allowed_user_test.rb +0 -34
  260. data/test/unit/kaui/base_test.rb +0 -23
  261. data/test/unit/kaui/date_helper_test.rb +0 -26
  262. data/test/unit/kaui/invoice_item_test.rb +0 -11
  263. data/test/unit/kaui/invoice_payment_test.rb +0 -42
  264. data/test/unit/kaui/invoice_test.rb +0 -20
  265. data/test/unit/kaui/money_helper_test.rb +0 -16
  266. data/test/unit/kaui/payment_test.rb +0 -42
  267. data/test/unit/kaui/tag_definition_test.rb +0 -11
  268. data/test/unit/kaui/tenant_test.rb +0 -19
  269. /data/app/assets/stylesheets/{application.css → kaui_application.css} +0 -0
@@ -1,509 +1,555 @@
1
- class Kaui::AdminTenantsController < Kaui::EngineController
1
+ # frozen_string_literal: true
2
2
 
3
- skip_before_action :check_for_redirect_to_tenant_screen
3
+ module Kaui
4
+ class AdminTenantsController < Kaui::EngineController
5
+ skip_before_action :check_for_redirect_to_tenant_screen
4
6
 
5
- def index
6
- # Display the configured tenants in KAUI (which could be different than the existing tenants known by Kill Bill)
7
- tenants_for_current_user = retrieve_tenants_for_current_user
8
- @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
9
- end
10
-
11
- def new
12
- @tenant = Kaui::Tenant.new
13
- end
14
-
15
- def create
16
- param_tenant = params[:tenant]
7
+ def index
8
+ # Display the configured tenants in KAUI (which could be different than the existing tenants known by Kill Bill)
9
+ tenants_for_current_user = retrieve_tenants_for_current_user
10
+ @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
11
+ end
17
12
 
18
- old_tenant = Kaui::Tenant.find_by_name(param_tenant[:name]) || Kaui::Tenant.find_by_api_key(param_tenant[:api_key])
19
- if old_tenant
20
- old_tenant.kaui_allowed_users << Kaui::AllowedUser.where(:kb_username => current_user.kb_username).first_or_create
21
- redirect_to admin_tenant_path(old_tenant[:id]), :notice => 'Tenant was successfully configured' and return
13
+ def new
14
+ @tenant = Kaui::Tenant.new
22
15
  end
23
16
 
24
- begin
25
- options = tenant_options_for_client
26
- new_tenant = nil
17
+ def create
18
+ param_tenant = params[:tenant]
19
+
20
+ old_tenant = Kaui::Tenant.find_by_name(param_tenant[:name]) || Kaui::Tenant.find_by_api_key(param_tenant[:api_key])
21
+ if old_tenant
22
+ old_tenant.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
23
+ redirect_to admin_tenant_path(old_tenant[:id]), notice: 'Tenant was successfully configured' and return
24
+ end
27
25
 
28
26
  begin
29
- options[:api_key] = param_tenant[:api_key]
30
- options[:api_secret] = param_tenant[:api_secret]
31
- new_tenant = Kaui::AdminTenant.find_by_api_key(param_tenant[:api_key], options)
32
- rescue KillBillClient::API::Unauthorized, KillBillClient::API::NotFound
33
-
34
- # Create the tenant in Kill Bill
35
- new_tenant = Kaui::AdminTenant.new
36
- new_tenant.external_key = param_tenant[:name]
37
- new_tenant.api_key = param_tenant[:api_key]
38
- new_tenant.api_secret = param_tenant[:api_secret]
39
- new_tenant = new_tenant.create(false, options[:username], nil, comment, options)
27
+ options = tenant_options_for_client
28
+ new_tenant = nil
29
+
30
+ begin
31
+ options[:api_key] = param_tenant[:api_key]
32
+ options[:api_secret] = param_tenant[:api_secret]
33
+ new_tenant = Kaui::AdminTenant.find_by_api_key(param_tenant[:api_key], options)
34
+ rescue KillBillClient::API::Unauthorized, KillBillClient::API::NotFound
35
+ # Create the tenant in Kill Bill
36
+ new_tenant = Kaui::AdminTenant.new
37
+ new_tenant.external_key = param_tenant[:name]
38
+ new_tenant.api_key = param_tenant[:api_key]
39
+ new_tenant.api_secret = param_tenant[:api_secret]
40
+ new_tenant = new_tenant.create(false, options[:username], nil, comment, options)
41
+ end
42
+
43
+ # Transform object to Kaui model
44
+ tenant_model = Kaui::Tenant.new
45
+ tenant_model.name = param_tenant[:name]
46
+ tenant_model.api_key = param_tenant[:api_key]
47
+ tenant_model.api_secret = param_tenant[:api_secret]
48
+ tenant_model.kb_tenant_id = new_tenant.tenant_id
49
+
50
+ # Save in KAUI tables
51
+ tenant_model.save!
52
+ # Make sure at least the current user can access the tenant
53
+ tenant_model.kaui_allowed_users << Kaui::AllowedUser.where(kb_username: current_user.kb_username).first_or_create
54
+ rescue KillBillClient::API::Conflict => _e
55
+ # tenant api_key was found but has a wrong api_secret
56
+ flash[:error] = "Submitted credentials for #{param_tenant[:api_key]} did not match the expected credentials."
57
+ redirect_to admin_tenants_path and return
58
+ rescue StandardError => e
59
+ flash[:error] = "Failed to create the tenant: #{as_string(e)}"
60
+ redirect_to admin_tenants_path and return
40
61
  end
41
62
 
42
- # Transform object to Kaui model
43
- tenant_model = Kaui::Tenant.new
44
- tenant_model.name = param_tenant[:name]
45
- tenant_model.api_key = param_tenant[:api_key]
46
- tenant_model.api_secret = param_tenant[:api_secret]
47
- tenant_model.kb_tenant_id = new_tenant.tenant_id
48
-
49
- # Save in KAUI tables
50
- tenant_model.save!
51
- # Make sure at least the current user can access the tenant
52
- tenant_model.kaui_allowed_users << Kaui::AllowedUser.where(:kb_username => current_user.kb_username).first_or_create
53
- rescue KillBillClient::API::Conflict => e
54
- # tenant api_key was found but has a wrong api_secret
55
- flash[:error] = "Submitted credentials for #{param_tenant[:api_key]} did not match the expected credentials."
56
- redirect_to admin_tenants_path and return
57
- rescue => e
58
- flash[:error] = "Failed to create the tenant: #{as_string(e)}"
59
- redirect_to admin_tenants_path and return
63
+ # Select the tenant, see TenantsController
64
+ session[:kb_tenant_id] = tenant_model.kb_tenant_id
65
+ session[:kb_tenant_name] = tenant_model.name
66
+ session[:tenant_id] = tenant_model.id
67
+
68
+ redirect_to admin_tenant_path(tenant_model[:id]), notice: 'Tenant was successfully configured'
60
69
  end
61
70
 
62
- # Select the tenant, see TenantsController
63
- session[:kb_tenant_id] = tenant_model.kb_tenant_id
64
- session[:kb_tenant_name] = tenant_model.name
65
- session[:tenant_id] = tenant_model.id
71
+ def show
72
+ @tenant = safely_find_tenant_by_id(params[:id])
73
+ @allowed_users = @tenant.kaui_allowed_users & retrieve_allowed_users_for_current_user
66
74
 
67
- redirect_to admin_tenant_path(tenant_model[:id]), :notice => 'Tenant was successfully configured'
68
- end
75
+ configure_tenant_if_nil(@tenant)
69
76
 
70
- def show
71
- @tenant = safely_find_tenant_by_id(params[:id])
72
- @allowed_users = @tenant.kaui_allowed_users & retrieve_allowed_users_for_current_user
73
-
74
- set_tenant_if_nil(@tenant)
77
+ options = tenant_options_for_client
78
+ options[:api_key] = @tenant.api_key
79
+ options[:api_secret] = @tenant.api_secret
75
80
 
76
- options = tenant_options_for_client
77
- options[:api_key] = @tenant.api_key
78
- options[:api_secret] = @tenant.api_secret
81
+ fetch_catalog_versions = promise do
82
+ Kaui::Catalog.get_tenant_catalog_versions(nil, options)
83
+ rescue StandardError
84
+ @catalog_versions = []
85
+ end
86
+ fetch_overdue = promise do
87
+ Kaui::Overdue.get_overdue_json(options)
88
+ rescue StandardError
89
+ @overdue = nil
90
+ end
91
+ fetch_overdue_xml = promise do
92
+ Kaui::Overdue.get_tenant_overdue_config(options)
93
+ rescue StandardError
94
+ @overdue_xml = nil
95
+ end
79
96
 
80
- fetch_catalog_versions = promise { Kaui::Catalog::get_tenant_catalog_versions(options) rescue @catalog_versions = []}
81
- fetch_overdue = promise { Kaui::Overdue::get_overdue_json(options) rescue @overdue = nil }
82
- fetch_overdue_xml = promise { Kaui::Overdue::get_tenant_overdue_config('xml', options) rescue @overdue_xml = nil }
97
+ fetch_tenant_plugin_config = promise { Kaui::AdminTenant.get_tenant_plugin_config(options) }
83
98
 
84
- fetch_tenant_plugin_config = promise { Kaui::AdminTenant::get_tenant_plugin_config(options) }
99
+ @catalog_versions = []
100
+ wait(fetch_catalog_versions).each_with_index do |effective_date, idx|
101
+ @catalog_versions << { version: idx,
102
+ version_date: effective_date }
103
+ end
85
104
 
86
- @catalog_versions = []
87
- wait(fetch_catalog_versions).each_with_index do |effective_date, idx|
88
- @catalog_versions << {:version => idx,
89
- :version_date => effective_date}
90
- end
105
+ @latest_version = begin
106
+ @catalog_versions[@catalog_versions.length - 1][:version_date]
107
+ rescue StandardError
108
+ nil
109
+ end
91
110
 
92
- @latest_version = @catalog_versions[@catalog_versions.length - 1][:version_date] rescue nil
111
+ @overdue = wait(fetch_overdue)
112
+ @overdue_xml = wait(fetch_overdue_xml)
113
+ @tenant_plugin_config = begin
114
+ wait(fetch_tenant_plugin_config)
115
+ rescue StandardError
116
+ ''
117
+ end
93
118
 
94
- @overdue = wait(fetch_overdue)
95
- @overdue_xml = wait(fetch_overdue_xml)
96
- @tenant_plugin_config = wait(fetch_tenant_plugin_config) rescue ''
119
+ # When reloading page from the view, it sends the last tab that was active
120
+ @active_tab = params[:active_tab] || 'CatalogShow'
121
+ end
97
122
 
98
- # When reloading page from the view, it sends the last tab that was active
99
- @active_tab = params[:active_tab] || 'CatalogShow'
100
- end
123
+ def upload_catalog
124
+ current_tenant = safely_find_tenant_by_id(params[:id])
101
125
 
102
- def upload_catalog
103
- current_tenant = safely_find_tenant_by_id(params[:id])
126
+ options = tenant_options_for_client
127
+ options[:api_key] = current_tenant.api_key
128
+ options[:api_secret] = current_tenant.api_secret
104
129
 
105
- options = tenant_options_for_client
106
- options[:api_key] = current_tenant.api_key
107
- options[:api_secret] = current_tenant.api_secret
130
+ uploaded_catalog = params.require(:catalog)
131
+ catalog_xml = uploaded_catalog.read
108
132
 
109
- uploaded_catalog = params[:catalog]
110
- catalog_xml = uploaded_catalog.read
133
+ Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)
111
134
 
112
- Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)
135
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_uploaded_successfully')
136
+ end
113
137
 
114
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Catalog was successfully uploaded'
115
- end
138
+ def new_catalog
139
+ options = tenant_options_for_client
140
+ fetch_state_for_new_catalog_screen(options)
141
+ @simple_plan = Kaui::SimplePlan.new({
142
+ product_category: 'BASE',
143
+ amount: 0,
144
+ trial_length: 0,
145
+ currency: 'USD',
146
+ billing_period: 'MONTHLY'
147
+ })
148
+ end
116
149
 
117
- def new_catalog
118
- options = tenant_options_for_client
119
- fetch_state_for_new_catalog_screen(options)
120
- @simple_plan = Kaui::SimplePlan.new({
121
- :product_category => 'BASE',
122
- :amount => 0,
123
- :trial_length => 0,
124
- :currency => 'USD',
125
- :billing_period => 'MONTHLY'
126
- })
127
- end
150
+ def delete_catalog
151
+ tenant = safely_find_tenant_by_id(params[:id])
128
152
 
129
- def delete_catalog
130
- tenant = safely_find_tenant_by_id(params[:id])
153
+ options = tenant_options_for_client
154
+ options[:api_key] = tenant.api_key
155
+ options[:api_secret] = tenant.api_secret
131
156
 
132
- options = tenant_options_for_client
133
- options[:api_key] = tenant.api_key
134
- options[:api_secret] = tenant.api_secret
157
+ begin
158
+ Kaui::Catalog.delete_catalog(options[:username], 'KAUI wrong catalog', comment, options)
159
+ rescue NoMethodError => _e
160
+ flash[:error] = 'Failed to delete catalog: only available in KB 0.19+ versions'
161
+ redirect_to admin_tenants_path and return
162
+ end
135
163
 
136
- begin
137
- Kaui::Catalog.delete_catalog(options[:username], 'KAUI wrong catalog', comment, options)
138
- rescue NoMethodError => _
139
- flash[:error] = 'Failed to delete catalog: only available in KB 0.19+ versions'
140
- redirect_to admin_tenants_path and return
164
+ redirect_to admin_tenant_path(tenant.id), notice: 'Catalog was successfully deleted'
141
165
  end
142
166
 
143
- redirect_to admin_tenant_path(tenant.id), :notice => 'Catalog was successfully deleted'
144
- end
167
+ def new_plan_currency
168
+ @tenant = safely_find_tenant_by_id(params[:id])
145
169
 
146
- def new_plan_currency
147
- @tenant = safely_find_tenant_by_id(params[:id])
170
+ is_plan_id_found = false
171
+ plan_id = params[:plan_id]
148
172
 
149
- is_plan_id_found = false
150
- plan_id = params[:plan_id]
173
+ options = tenant_options_for_client
174
+ options[:api_key] = @tenant.api_key
175
+ options[:api_secret] = @tenant.api_secret
151
176
 
152
- options = tenant_options_for_client
153
- options[:api_key] = @tenant.api_key
154
- options[:api_secret] = @tenant.api_secret
177
+ catalog = Kaui::Catalog.get_catalog_json(true, nil, options)
155
178
 
156
- catalog = Kaui::Catalog::get_catalog_json(true, nil, options)
179
+ # seek if plan id exists
180
+ catalog.products.each do |product|
181
+ product.plans.each { |plan| is_plan_id_found |= plan.name == plan_id }
182
+ break if is_plan_id_found
183
+ end
157
184
 
158
- # seek if plan id exists
159
- catalog.products.each do |product|
160
- product.plans.each { |plan| is_plan_id_found |= plan.name == plan_id }
161
- break if is_plan_id_found
162
- end
185
+ unless is_plan_id_found
186
+ flash[:error] = "Plan id #{plan_id} was not found."
187
+ redirect_to admin_tenant_path(@tenant[:id])
188
+ end
163
189
 
164
- unless is_plan_id_found
165
- flash[:error] = "Plan id #{plan_id} was not found."
166
- redirect_to admin_tenant_path(@tenant[:id])
190
+ @simple_plan = Kaui::SimplePlan.new
191
+ @simple_plan.plan_id = params[:plan_id]
167
192
  end
168
193
 
169
- @simple_plan = Kaui::SimplePlan.new
170
- @simple_plan.plan_id = params[:plan_id]
171
- end
194
+ def create_simple_plan
195
+ options = tenant_options_for_client
196
+ fetch_state_for_new_catalog_screen(options)
197
+
198
+ simple_plan = params.require(:simple_plan).delete_if { |_e, value| value.blank? }
199
+ # Fix issue in Rails where first entry in the multi-select array is an empty string
200
+ simple_plan['available_base_products']&.reject!(&:blank?)
201
+
202
+ @simple_plan = Kaui::SimplePlan.new(simple_plan)
203
+
204
+ valid = true
205
+ # Validate new simple plan
206
+ # https://github.com/killbill/killbill-admin-ui/issues/247
207
+ if @available_base_products.include?(@simple_plan.plan_id)
208
+ flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is a BASE product already)"
209
+ valid = false
210
+ elsif @available_ao_products.include?(@simple_plan.plan_id)
211
+ flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is an ADD_ON product already)"
212
+ valid = false
213
+ elsif @available_standalone_products.include?(@simple_plan.plan_id)
214
+ flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is a STANDALONE product already)"
215
+ valid = false
216
+ elsif @all_plans.include?(@simple_plan.product_name)
217
+ flash.now[:error] = "Error while creating plan: invalid product name (#{@simple_plan.product_name} is a plan name already)"
218
+ valid = false
219
+ elsif @all_plans.include?(@simple_plan.plan_id)
220
+ flash.now[:error] = "Error while creating plan: plan #{@simple_plan.plan_id} already exists"
221
+ valid = false
222
+ elsif @available_base_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'BASE'
223
+ flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is a BASE product"
224
+ valid = false
225
+ elsif @available_ao_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'ADD_ON'
226
+ flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is an ADD_ON product"
227
+ valid = false
228
+ elsif @available_standalone_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'STANDALONE'
229
+ flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is a STANDALONE product"
230
+ valid = false
231
+ end
172
232
 
173
- def create_simple_plan
174
- options = tenant_options_for_client
175
- fetch_state_for_new_catalog_screen(options)
176
-
177
- simple_plan = params.require(:simple_plan).delete_if { |e, value| value.blank? }
178
- # Fix issue in Rails where first entry in the multi-select array is an empty string
179
- simple_plan["available_base_products"].reject!(&:blank?) if simple_plan["available_base_products"]
180
-
181
- @simple_plan = Kaui::SimplePlan.new(simple_plan)
182
-
183
- valid = true
184
- # Validate new simple plan
185
- # https://github.com/killbill/killbill-admin-ui/issues/247
186
- if @available_base_products.include?(@simple_plan.plan_id)
187
- flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is a BASE product already)"
188
- valid = false
189
- elsif @available_ao_products.include?(@simple_plan.plan_id)
190
- flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is an ADD_ON product already)"
191
- valid = false
192
- elsif @available_standalone_products.include?(@simple_plan.plan_id)
193
- flash.now[:error] = "Error while creating plan: invalid plan name (#{@simple_plan.plan_id} is a STANDALONE product already)"
194
- valid = false
195
- elsif @all_plans.include?(@simple_plan.product_name)
196
- flash.now[:error] = "Error while creating plan: invalid product name (#{@simple_plan.product_name} is a plan name already)"
197
- valid = false
198
- elsif @all_plans.include?(@simple_plan.plan_id)
199
- flash.now[:error] = "Error while creating plan: plan #{@simple_plan.plan_id} already exists"
200
- valid = false
201
- elsif @available_base_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'BASE'
202
- flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is a BASE product"
203
- valid = false
204
- elsif @available_ao_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'ADD_ON'
205
- flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is an ADD_ON product"
206
- valid = false
207
- elsif @available_standalone_products.include?(@simple_plan.product_name) && @simple_plan.product_category != 'STANDALONE'
208
- flash.now[:error] = "Error while creating plan: product #{@simple_plan.product_name} is a STANDALONE product"
209
- valid = false
233
+ if valid
234
+ begin
235
+ Kaui::Catalog.add_tenant_catalog_simple_plan(@simple_plan, options[:username], nil, comment, options)
236
+ redirect_to admin_tenant_path(@tenant.id), notice: 'Catalog plan was successfully added'
237
+ rescue StandardError => e
238
+ flash.now[:error] = "Error while creating plan: #{as_string(e)}"
239
+ render action: :new_catalog
240
+ end
241
+ else
242
+ render action: :new_catalog
243
+ end
210
244
  end
211
245
 
212
- if valid
213
- begin
214
- Kaui::Catalog.add_tenant_catalog_simple_plan(@simple_plan, options[:username], nil, comment, options)
215
- redirect_to admin_tenant_path(@tenant.id), :notice => 'Catalog plan was successfully added'
216
- rescue => e
217
- flash.now[:error] = "Error while creating plan: #{as_string(e)}"
218
- render :action => :new_catalog
219
- end
220
- else
221
- render :action => :new_catalog
246
+ def new_overdue_config
247
+ @tenant = safely_find_tenant_by_id(params[:id])
248
+
249
+ options = tenant_options_for_client
250
+ options[:api_key] = @tenant.api_key
251
+ options[:api_secret] = @tenant.api_secret
252
+ @overdue = Kaui::Overdue.get_overdue_json(options)
222
253
  end
223
- end
224
254
 
225
- def new_overdue_config
226
- @tenant = safely_find_tenant_by_id(params[:id])
255
+ def modify_overdue_config
256
+ current_tenant = safely_find_tenant_by_id(params[:id])
227
257
 
228
- options = tenant_options_for_client
229
- options[:api_key] = @tenant.api_key
230
- options[:api_secret] = @tenant.api_secret
231
- @overdue = Kaui::Overdue::get_overdue_json(options)
232
- end
258
+ options = tenant_options_for_client
259
+ options[:api_key] = current_tenant.api_key
260
+ options[:api_secret] = current_tenant.api_secret
233
261
 
234
- def modify_overdue_config
235
- current_tenant = safely_find_tenant_by_id(params[:id])
262
+ view_form_model = params.require(:kill_bill_client_model_overdue).delete_if { |_e, value| value.blank? }
263
+ view_form_model['states'] = view_form_model['states'].values unless view_form_model['states'].blank?
236
264
 
237
- options = tenant_options_for_client
238
- options[:api_key] = current_tenant.api_key
239
- options[:api_secret] = current_tenant.api_secret
265
+ overdue = Kaui::Overdue.from_overdue_form_model(view_form_model)
266
+ Kaui::Overdue.upload_tenant_overdue_config_json(overdue.to_json, options[:username], nil, comment, options)
267
+ redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.translate('flashes.notices.overdue_added_successfully')
268
+ end
240
269
 
241
- view_form_model = params.require(:kill_bill_client_model_overdue).delete_if { |e, value| value.blank? }
242
- view_form_model['states'] = view_form_model['states'].values unless view_form_model['states'].blank?
270
+ def upload_overdue_config
271
+ uploaded_overdue_config = params.require(:overdue)
272
+ current_tenant = safely_find_tenant_by_id(params[:id])
243
273
 
244
- overdue = Kaui::Overdue::from_overdue_form_model(view_form_model)
245
- Kaui::Overdue::upload_tenant_overdue_config_json(overdue.to_json,options[:username], nil, comment, options)
246
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Overdue config was successfully added '
247
- end
274
+ options = tenant_options_for_client
275
+ options[:api_key] = current_tenant.api_key
276
+ options[:api_secret] = current_tenant.api_secret
248
277
 
249
- def upload_overdue_config
250
- current_tenant = safely_find_tenant_by_id(params[:id])
278
+ overdue_config_xml = uploaded_overdue_config.read
251
279
 
252
- options = tenant_options_for_client
253
- options[:api_key] = current_tenant.api_key
254
- options[:api_secret] = current_tenant.api_secret
280
+ begin
281
+ Nokogiri::XML(overdue_config_xml, &:strict)
282
+ rescue Nokogiri::XML::SyntaxError => e
283
+ flash[:error] = I18n.translate('errors.messages.invalid_xml', error: e)
284
+ redirect_to admin_tenant_path(current_tenant.id) and return
285
+ end
255
286
 
256
- uploaded_overdue_config = params[:overdue]
257
- overdue_config_xml = uploaded_overdue_config.read
287
+ Kaui::AdminTenant.upload_overdue_config(overdue_config_xml, options[:username], nil, comment, options)
258
288
 
259
- Kaui::AdminTenant.upload_overdue_config(overdue_config_xml, options[:username], nil, comment, options)
289
+ redirect_to admin_tenant_path(current_tenant.id, active_tab: 'OverdueShow'), notice: I18n.translate('flashes.notices.overdue_uploaded_successfully')
290
+ end
260
291
 
261
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Overdue config was successfully uploaded'
262
- end
292
+ def upload_invoice_template
293
+ current_tenant = safely_find_tenant_by_id(params[:id])
263
294
 
264
- def upload_invoice_template
265
- current_tenant = safely_find_tenant_by_id(params[:id])
295
+ options = tenant_options_for_client
296
+ options[:api_key] = current_tenant.api_key
297
+ options[:api_secret] = current_tenant.api_secret
266
298
 
267
- options = tenant_options_for_client
268
- options[:api_key] = current_tenant.api_key
269
- options[:api_secret] = current_tenant.api_secret
299
+ is_manual_pay = params[:manual_pay]
300
+ uploaded_invoice_template = params.require(:invoice_template)
301
+ invoice_template = uploaded_invoice_template.read
270
302
 
271
- is_manual_pay = params[:manual_pay]
272
- uploaded_invoice_template = params[:invoice_template]
273
- invoice_template = uploaded_invoice_template.read
303
+ Kaui::AdminTenant.upload_invoice_template(invoice_template, is_manual_pay, true, options[:username], nil, comment, options)
274
304
 
275
- Kaui::AdminTenant.upload_invoice_template(invoice_template, is_manual_pay, true, options[:username], nil, comment, options)
305
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.invoice_template_uploaded_successfully')
306
+ end
276
307
 
277
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Invoice template was successfully uploaded'
278
- end
308
+ def upload_invoice_translation
309
+ current_tenant = safely_find_tenant_by_id(params[:id])
279
310
 
280
- def upload_invoice_translation
281
- current_tenant = safely_find_tenant_by_id(params[:id])
311
+ options = tenant_options_for_client
312
+ options[:api_key] = current_tenant.api_key
313
+ options[:api_secret] = current_tenant.api_secret
282
314
 
283
- options = tenant_options_for_client
284
- options[:api_key] = current_tenant.api_key
285
- options[:api_secret] = current_tenant.api_secret
315
+ locale = params[:translation_locale]
316
+ uploaded_invoice_translation = params.require(:invoice_translation)
317
+ invoice_translation = uploaded_invoice_translation.read
286
318
 
287
- locale = params[:translation_locale]
288
- uploaded_invoice_translation = params[:invoice_translation]
289
- invoice_translation = uploaded_invoice_translation.read
319
+ Kaui::AdminTenant.upload_invoice_translation(invoice_translation, locale, true, options[:username], nil, comment, options)
290
320
 
291
- Kaui::AdminTenant.upload_invoice_translation(invoice_translation, locale, true, options[:username], nil, comment, options)
321
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.invoice_translation_uploaded_successfully')
322
+ end
292
323
 
293
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Invoice translation was successfully uploaded'
294
- end
324
+ def upload_catalog_translation
325
+ current_tenant = safely_find_tenant_by_id(params[:id])
295
326
 
296
- def upload_catalog_translation
297
- current_tenant = safely_find_tenant_by_id(params[:id])
327
+ options = tenant_options_for_client
328
+ options[:api_key] = current_tenant.api_key
329
+ options[:api_secret] = current_tenant.api_secret
298
330
 
299
- options = tenant_options_for_client
300
- options[:api_key] = current_tenant.api_key
301
- options[:api_secret] = current_tenant.api_secret
331
+ locale = params[:translation_locale]
332
+ uploaded_catalog_translation = params.require(:catalog_translation)
333
+ catalog_translation = uploaded_catalog_translation.read
302
334
 
303
- locale = params[:translation_locale]
304
- uploaded_catalog_translation = params[:catalog_translation]
305
- catalog_translation = uploaded_catalog_translation.read
335
+ Kaui::AdminTenant.upload_catalog_translation(catalog_translation, locale, true, options[:username], nil, comment, options)
306
336
 
307
- Kaui::AdminTenant.upload_catalog_translation(catalog_translation, locale, true, options[:username], nil, comment, options)
337
+ redirect_to admin_tenant_path(current_tenant.id), notice: I18n.translate('flashes.notices.catalog_translation_uploaded_successfully')
338
+ end
308
339
 
309
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Catalog translation was successfully uploaded'
310
- end
340
+ def upload_plugin_config
341
+ current_tenant = safely_find_tenant_by_id(params[:id])
311
342
 
312
- def upload_plugin_config
313
- current_tenant = safely_find_tenant_by_id(params[:id])
343
+ options = tenant_options_for_client
344
+ options[:api_key] = current_tenant.api_key
345
+ options[:api_secret] = current_tenant.api_secret
314
346
 
315
- options = tenant_options_for_client
316
- options[:api_key] = current_tenant.api_key
317
- options[:api_secret] = current_tenant.api_secret
347
+ plugin_name = params[:plugin_name]
348
+ plugin_properties = params[:plugin_properties]
349
+ plugin_type = params[:plugin_type]
350
+ plugin_key = params[:plugin_key]
318
351
 
319
- plugin_name = params[:plugin_name]
320
- plugin_properties = params[:plugin_properties]
321
- plugin_type = params[:plugin_type]
322
- plugin_key = params[:plugin_key]
352
+ if plugin_properties.blank?
353
+ flash[:error] = 'Plugin properties cannot be blank'
354
+ elsif plugin_name.blank?
355
+ flash[:error] = 'Plugin name cannot be blank'
356
+ else
357
+ plugin_config = Kaui::AdminTenant.format_plugin_config(plugin_key, plugin_type, plugin_properties)
323
358
 
324
- if plugin_properties.blank?
325
- flash[:error] = 'Plugin properties cannot be blank'
326
- elsif plugin_name.blank?
327
- flash[:error] = 'Plugin name cannot be blank'
328
- else
329
- plugin_config = Kaui::AdminTenant.format_plugin_config(plugin_key, plugin_type, plugin_properties)
359
+ Kaui::AdminTenant.upload_tenant_plugin_config(plugin_name, plugin_config, options[:username], nil, comment, options)
360
+ flash[:notice] = 'Config for plugin was successfully uploaded'
361
+ end
330
362
 
331
- Kaui::AdminTenant.upload_tenant_plugin_config(plugin_name, plugin_config, options[:username], nil, comment, options)
332
- flash[:notice] = 'Config for plugin was successfully uploaded'
363
+ redirect_to admin_tenant_path(current_tenant.id, active_tab: 'PluginConfig')
333
364
  end
334
365
 
335
- redirect_to admin_tenant_path(current_tenant.id, :active_tab => 'PluginConfig')
336
- end
366
+ def remove_allowed_user
367
+ current_tenant = safely_find_tenant_by_id(params[:id])
368
+ au = Kaui::AllowedUser.find(params.require(:allowed_user).require(:id))
337
369
 
338
- def remove_allowed_user
339
- current_tenant = safely_find_tenant_by_id(params[:id])
340
- au = Kaui::AllowedUser.find(params.require(:allowed_user).require(:id))
370
+ unless current_user.root?
371
+ render json: { alert: 'Only the root user can remove users from tenants' }.to_json, status: 401
372
+ return
373
+ end
341
374
 
342
- if !current_user.root?
343
- render :json => {:alert => 'Only the root user can remove users from tenants'}.to_json, :status => 401
344
- return
375
+ # remove the association
376
+ au.kaui_tenants.delete current_tenant
377
+ render json: '{}', status: 200
345
378
  end
346
379
 
347
- # remove the association
348
- au.kaui_tenants.delete current_tenant
349
- render :json => '{}', :status => 200
350
- end
380
+ def add_allowed_user
381
+ current_tenant = safely_find_tenant_by_id(params[:tenant_id])
382
+ allowed_user = Kaui::AllowedUser.find_by_kb_username(params.require(:allowed_user).require(:kb_username))
351
383
 
352
- def add_allowed_user
353
- current_tenant = safely_find_tenant_by_id(params[:tenant_id])
354
- allowed_user = Kaui::AllowedUser.find_by_kb_username(params.require(:allowed_user).require(:kb_username))
384
+ unless current_user.root?
385
+ flash[:error] = 'Only the root user can add users from tenants'
386
+ redirect_to admin_tenant_path(current_tenant.id)
387
+ return
388
+ end
355
389
 
356
- if !current_user.root?
357
- flash[:error] = 'Only the root user can add users from tenants'
358
- redirect_to admin_tenant_path(current_tenant.id)
359
- return
360
- end
390
+ if allowed_user.nil?
391
+ flash[:error] = "User #{params.require(:allowed_user).require(:kb_username)} does not exist!"
392
+ redirect_to admin_tenant_path(current_tenant.id)
393
+ return
394
+ end
361
395
 
362
- if allowed_user.nil?
363
- flash[:error] = "User #{params.require(:allowed_user).require(:kb_username)} does not exist!"
364
- redirect_to admin_tenant_path(current_tenant.id)
365
- return
396
+ tenants_ids = allowed_user.kaui_tenants.map(&:id) || []
397
+ tenants_ids << current_tenant.id
398
+ allowed_user.kaui_tenant_ids = tenants_ids
399
+ redirect_to admin_tenant_path(current_tenant.id), notice: 'Allowed user was successfully added'
366
400
  end
367
401
 
368
- tenants_ids = allowed_user.kaui_tenants.map(&:id) || []
369
- tenants_ids << current_tenant.id
370
- allowed_user.kaui_tenant_ids = tenants_ids
371
- redirect_to admin_tenant_path(current_tenant.id), :notice => 'Allowed user was successfully added'
372
- end
402
+ def allowed_users
403
+ json_response do
404
+ tenant = safely_find_tenant_by_id(params[:tenant_id])
405
+ actual_allowed_users = tenant.kaui_allowed_users.map(&:id)
373
406
 
374
- def allowed_users
375
- json_response do
376
- tenant = safely_find_tenant_by_id(params[:tenant_id])
377
- actual_allowed_users = tenant.kaui_allowed_users.map {|au| au.id}
407
+ retrieve_allowed_users_for_current_user.reject { |au| actual_allowed_users.include? au.id }
408
+ end
409
+ end
378
410
 
379
- retrieve_allowed_users_for_current_user.select {|au| !actual_allowed_users.include? au.id }
411
+ def display_catalog_xml
412
+ catalog_xml = fetch_catalog_xml(params[:id], params.require(:effective_date))
413
+ render xml: catalog_xml
380
414
  end
381
- end
382
415
 
383
- def display_catalog_xml
384
- catalog_xml = fetch_catalog_xml(params[:id], params.require(:effective_date))
385
- render xml: catalog_xml
386
- end
416
+ def download_catalog_xml
417
+ effective_date = params.require(:effective_date)
418
+ catalog_xml = fetch_catalog_xml(params[:id], effective_date)
419
+ send_data catalog_xml, filename: "catalog_#{effective_date}.xml", type: :xml
420
+ end
387
421
 
388
- def download_catalog_xml
389
- effective_date = params.require(:effective_date)
390
- catalog_xml = fetch_catalog_xml(params[:id], effective_date)
391
- send_data catalog_xml, filename: "catalog_#{effective_date}.xml", type: :xml
392
- end
422
+ def display_overdue_xml
423
+ render xml: params.require(:xml)
424
+ end
393
425
 
394
- def display_overdue_xml
395
- render xml: params.require(:xml)
396
- end
426
+ def catalog_by_effective_date
427
+ json_response do
428
+ current_tenant = safely_find_tenant_by_id(params[:id])
429
+ effective_date = params.require(:effective_date)
397
430
 
398
- def catalog_by_effective_date
399
- json_response do
400
- current_tenant = safely_find_tenant_by_id(params[:id])
401
- effective_date = params.require(:effective_date)
431
+ options = tenant_options_for_client
432
+ options[:api_key] = current_tenant.api_key
433
+ options[:api_secret] = current_tenant.api_secret
402
434
 
403
- options = tenant_options_for_client
404
- options[:api_key] = current_tenant.api_key
405
- options[:api_secret] = current_tenant.api_secret
435
+ catalog = []
436
+ result = begin
437
+ Kaui::Catalog.get_catalog_json(false, effective_date, options)
438
+ rescue StandardError
439
+ catalog = []
440
+ end
406
441
 
407
- catalog = []
408
- result = Kaui::Catalog::get_catalog_json(false, effective_date, options) rescue catalog = []
442
+ # convert result to a full hash since dynamic attributes of a class are ignored when converting to json
443
+ result.each do |data|
444
+ plans = []
445
+ data[:plans].each do |plan|
446
+ plans << plan.instance_variables.each_with_object({}) { |var, hash_plan| hash_plan[var.to_s.delete('@')] = plan.instance_variable_get(var) }
447
+ end
409
448
 
410
- # convert result to a full hash since dynamic attributes of a class are ignored when converting to json
411
- result.each do |data|
412
- plans = []
413
- data[:plans].each do |plan|
414
- plans << plan.instance_variables.each_with_object({}) {|var, hash_plan| hash_plan[var.to_s.delete("@")] = plan.instance_variable_get(var) }
449
+ catalog << { version_date: data[:version_date],
450
+ currencies: data[:currencies],
451
+ plans: }
415
452
  end
416
453
 
417
- catalog << {:version_date => data[:version_date],
418
- :currencies => data[:currencies],
419
- :plans => plans
420
- }
454
+ { catalog: }
421
455
  end
422
-
423
- {:catalog => catalog }
424
456
  end
425
- end
426
-
427
- def switch_tenant
428
- tenant = Kaui::Tenant.find_by_kb_tenant_id(params.require(:kb_tenant_id))
429
-
430
- # Select the tenant, see TenantsController
431
- session[:kb_tenant_id] = tenant.kb_tenant_id
432
- session[:kb_tenant_name] = tenant.name
433
- session[:tenant_id] = tenant.id
434
-
435
- redirect_to admin_tenant_path(tenant.id), :notice => "Tenant was switched to #{tenant.name}"
436
- end
437
457
 
438
- private
458
+ def switch_tenant
459
+ tenant = Kaui::Tenant.find_by_kb_tenant_id(params.require(:kb_tenant_id))
439
460
 
440
- # Share code to handle render on error
441
- def fetch_state_for_new_catalog_screen(options)
442
- @tenant = safely_find_tenant_by_id(params[:id])
461
+ # Select the tenant, see TenantsController
462
+ session[:kb_tenant_id] = tenant.kb_tenant_id
463
+ session[:kb_tenant_name] = tenant.name
464
+ session[:tenant_id] = tenant.id
443
465
 
444
- options[:api_key] = @tenant.api_key
445
- options[:api_secret] = @tenant.api_secret
466
+ redirect_to admin_tenant_path(tenant.id), notice: "Tenant was switched to #{tenant.name}"
467
+ end
446
468
 
447
- latest_catalog = Kaui::Catalog::get_catalog_json(true, nil, options)
448
- @all_plans = latest_catalog ? (latest_catalog.products || []).map(&:plans).flatten.map(&:name) : []
469
+ private
470
+
471
+ # Share code to handle render on error
472
+ def fetch_state_for_new_catalog_screen(options)
473
+ @tenant = safely_find_tenant_by_id(params[:id])
474
+
475
+ options[:api_key] = @tenant.api_key
476
+ options[:api_secret] = @tenant.api_secret
477
+
478
+ latest_catalog = Kaui::Catalog.get_catalog_json(true, nil, options)
479
+ @all_plans = latest_catalog ? (latest_catalog.products || []).map(&:plans).flatten.map(&:name) : []
480
+
481
+ @ao_mapping = Kaui::Catalog.build_ao_mapping(latest_catalog)
482
+
483
+ @available_base_products = if latest_catalog&.products
484
+ latest_catalog.products.select { |p| p.type == 'BASE' }.map(&:name)
485
+ else
486
+ []
487
+ end
488
+ @available_ao_products = if latest_catalog&.products
489
+ latest_catalog.products.select { |p| p.type == 'ADD_ON' }.map(&:name)
490
+ else
491
+ []
492
+ end
493
+ @available_standalone_products = if latest_catalog&.products
494
+ latest_catalog.products.select { |p| p.type == 'STANDALONE' }.map(&:name)
495
+ else
496
+ []
497
+ end
498
+ @product_categories = %i[BASE ADD_ON STANDALONE]
499
+ @billing_period = %i[DAILY WEEKLY BIWEEKLY THIRTY_DAYS THIRTY_ONE_DAYS MONTHLY QUARTERLY BIANNUAL ANNUAL SESQUIENNIAL BIENNIAL TRIENNIAL]
500
+ @time_units = %i[UNLIMITED DAYS WEEKS MONTHS YEARS]
501
+ end
449
502
 
450
- @ao_mapping = Kaui::Catalog::build_ao_mapping(latest_catalog)
503
+ def safely_find_tenant_by_id(tenant_id)
504
+ tenant = Kaui::Tenant.find_by_id(tenant_id)
505
+ raise ActiveRecord::RecordNotFound, "Could not find tenant #{tenant_id}" unless retrieve_tenants_for_current_user.include?(tenant.kb_tenant_id)
451
506
 
452
- @available_base_products = latest_catalog && latest_catalog.products ?
453
- latest_catalog.products.select { |p| p.type == 'BASE' }.map { |p| p.name } : []
454
- @available_ao_products = latest_catalog && latest_catalog.products ?
455
- latest_catalog.products.select { |p| p.type == 'ADD_ON' }.map { |p| p.name } : []
456
- @available_standalone_products = latest_catalog && latest_catalog.products ?
457
- latest_catalog.products.select { |p| p.type == 'STANDALONE' }.map { |p| p.name } : []
458
- @product_categories = [:BASE, :ADD_ON, :STANDALONE]
459
- @billing_period = [:DAILY, :WEEKLY, :BIWEEKLY, :THIRTY_DAYS, :MONTHLY, :QUARTERLY, :BIANNUAL, :ANNUAL, :BIENNIAL]
460
- @time_units = [:UNLIMITED, :DAYS, :WEEKS, :MONTHS, :YEARS]
461
- end
507
+ tenant
508
+ end
462
509
 
463
- def safely_find_tenant_by_id(tenant_id)
464
- tenant = Kaui::Tenant.find_by_id(tenant_id)
465
- raise ActiveRecord::RecordNotFound.new('Could not find tenant ' + tenant_id) unless retrieve_tenants_for_current_user.include?(tenant.kb_tenant_id)
466
- tenant
467
- end
510
+ def tenant_options_for_client
511
+ user = current_user
512
+ {
513
+ username: user.kb_username,
514
+ password: user.password,
515
+ session_id: user.kb_session_id
516
+ }
517
+ end
468
518
 
469
- def tenant_options_for_client
470
- user = current_user
471
- {
472
- :username => user.kb_username,
473
- :password => user.password,
474
- :session_id => user.kb_session_id
475
- }
476
- end
519
+ def comment
520
+ 'Multi-tenant Administrative operation'
521
+ end
477
522
 
478
- def comment
479
- 'Multi-tenant Administrative operation'
480
- end
523
+ def configure_tenant_if_nil(tenant)
524
+ return unless session[:kb_tenant_id].nil?
481
525
 
482
- def set_tenant_if_nil(tenant)
483
- if session[:kb_tenant_id].nil?
484
526
  session[:kb_tenant_id] = tenant.kb_tenant_id
485
527
  session[:kb_tenant_name] = tenant.name
486
528
  session[:tenant_id] = tenant.id
487
529
  end
488
- end
489
530
 
490
- def split_camel_dash_underscore_space(data)
491
- # to_s to handle nil
492
- data.to_s.split(/(?=[A-Z])|(?=[_])|(?=[-])|(?=[ ])/).select {|member| !member.gsub(/[_-]/,'').strip.empty?}.map { |member| member.gsub(/[_-]/,'').strip.downcase }
493
- end
531
+ def split_camel_dash_underscore_space(data)
532
+ # to_s to handle nil
533
+ data.to_s.split(/(?=[A-Z])|(?=_)|(?=-)|(?= )/).reject { |member| member.gsub(/[_-]/, '').strip.empty? }.map { |member| member.gsub(/[_-]/, '').strip.downcase }
534
+ end
494
535
 
495
- def fetch_catalog_xml(tenant_id, effective_date)
496
- current_tenant = safely_find_tenant_by_id(tenant_id)
536
+ def fetch_catalog_xml(tenant_id, effective_date)
537
+ current_tenant = safely_find_tenant_by_id(tenant_id)
497
538
 
498
- options = tenant_options_for_client
499
- options[:api_key] = current_tenant.api_key
500
- options[:api_secret] = current_tenant.api_secret
539
+ options = tenant_options_for_client
540
+ options[:api_key] = current_tenant.api_key
541
+ options[:api_secret] = current_tenant.api_secret
501
542
 
502
- response = Kaui::Catalog.get_catalog_xml(effective_date, options) rescue response = {}
543
+ response = begin
544
+ Kaui::Catalog.get_catalog_xml(effective_date, options)
545
+ rescue StandardError
546
+ {}
547
+ end
503
548
 
504
- catalog_xml = {}
505
- catalog_xml = response[0][:xml] unless response.blank?
549
+ catalog_xml = {}
550
+ catalog_xml = response[0][:xml] unless response.blank?
506
551
 
507
- catalog_xml
552
+ catalog_xml
553
+ end
508
554
  end
509
555
  end