Class: Kaui::AdminTenantsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/admin_tenants_controller.rb

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 15

def create
  param_tenant = params[:tenant]
  old_tenant = Kaui::Tenant.find_by_name(param_tenant[:name])
  if old_tenant
    flash.now[:error] = "Tenant with name #{param_tenant[:name]} already exists!"
    @tenant = Kaui::Tenant.new
    render :new and return
  end

  old_tenant = Kaui::Tenant.find_by_api_key(param_tenant[:api_key])
  if old_tenant
    flash.now[:error] = "Tenant with api key #{param_tenant[:api_key]} already exists!"
    @tenant = Kaui::Tenant.new
    render :new and return
  end

  begin
    options = tenant_options_for_client
    new_tenant = nil

    begin
      options[:api_key] = param_tenant[:api_key]
      options[:api_secret] = param_tenant[:api_secret]
      new_tenant = Kaui::AdminTenant.find_by_api_key(param_tenant[:api_key], options)
    rescue KillBillClient::API::Unauthorized, KillBillClient::API::NotFound
      # Create the tenant in Kill Bill
      new_tenant = Kaui::AdminTenant.new
      new_tenant.external_key = param_tenant[:name]
      new_tenant.api_key = param_tenant[:api_key]
      new_tenant.api_secret = param_tenant[:api_secret]
      new_tenant = new_tenant.create(options[:username], nil, comment, options)
    end

    # Transform object to Kaui model
    tenant_model = Kaui::Tenant.new
    tenant_model.name = new_tenant.external_key
    tenant_model.kb_tenant_id = new_tenant.tenant_id
    tenant_model.api_key = new_tenant.api_key
    tenant_model.api_secret = param_tenant[:api_secret]

    # Save in KAUI tables
    tenant_model.save!

    # Make sure at least the current user can access the tenant
    tenant_model.kaui_allowed_users << Kaui::AllowedUser.where(:kb_username => current_user.kb_username).first_or_create
  rescue => e
    flash[:error] = "Failed to create the tenant: #{as_string(e)}"
    redirect_to admin_tenants_path and return
  end

  # Select the tenant, see TenantsController
  session[:kb_tenant_id] = tenant_model.kb_tenant_id
  session[:kb_tenant_name] = tenant_model.name

  redirect_to admin_tenant_path(tenant_model[:id]), :notice => 'Tenant was successfully configured'
end

#indexObject



5
6
7
8
9
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 5

def index
  # Display the configured tenants in KAUI (which could be different than the existing tenants known by Kill Bill)
  tenants_for_current_user = retrieve_tenants_for_current_user
  @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
end

#newObject



11
12
13
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 11

def new
  @tenant = Kaui::Tenant.new
end

#remove_allowed_userObject



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 172

def remove_allowed_user
  current_tenant = safely_find_tenant_by_id(params[:id])
  au = Kaui::AllowedUser.find(params.require(:allowed_user).require(:id))

  if Kaui.root_username != current_user.kb_username
    render :json => {:alert => 'Only the root user can remove users from tenants'}.to_json, :status => 401
    return
  end

  # remove the association
  au.kaui_tenants.delete current_tenant
  render :json => '{}', :status => 200
end

#showObject



72
73
74
75
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 72

def show
  @tenant = safely_find_tenant_by_id(params[:id])
  @allowed_users = @tenant.kaui_allowed_users & retrieve_allowed_users_for_current_user
end

#upload_catalogObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 77

def upload_catalog
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  uploaded_catalog = params[:catalog]
  catalog_xml = uploaded_catalog.read

  Kaui::AdminTenant.upload_catalog(catalog_xml, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Catalog was successfully uploaded'
end

#upload_catalog_translationObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 140

def upload_catalog_translation
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  locale = params[:translation_locale]
  uploaded_catalog_translation = params[:catalog_translation]
  catalog_translation = uploaded_catalog_translation.read

  Kaui::AdminTenant.upload_catalog_translation(catalog_translation, locale, true, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Catalog translation was successfully uploaded'
end

#upload_invoice_templateObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 108

def upload_invoice_template
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  is_manual_pay = params[:manual_pay]
  uploaded_invoice_template = params[:invoice_template]
  invoice_template = uploaded_invoice_template.read

  Kaui::AdminTenant.upload_invoice_template(invoice_template, is_manual_pay, true, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Invoice template was successfully uploaded'
end

#upload_invoice_translationObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 124

def upload_invoice_translation
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  locale = params[:translation_locale]
  uploaded_invoice_translation = params[:invoice_translation]
  invoice_translation = uploaded_invoice_translation.read

  Kaui::AdminTenant.upload_invoice_translation(invoice_translation, locale, true, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Invoice translation was successfully uploaded'
end

#upload_overdue_configObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 92

def upload_overdue_config
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  uploaded_overdue_config = params[:overdue]
  overdue_config_xml = uploaded_overdue_config.read

  Kaui::AdminTenant.upload_overdue_config(overdue_config_xml, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Overdue config was successfully uploaded'
end

#upload_plugin_configObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/kaui/admin_tenants_controller.rb', line 156

def upload_plugin_config
  current_tenant = safely_find_tenant_by_id(params[:id])

  options = tenant_options_for_client
  options[:api_key] = current_tenant.api_key
  options[:api_secret] = current_tenant.api_secret

  plugin_name = params[:plugin_name]
  uploaded_plugin_config = params[:plugin_config]
  plugin_config = uploaded_plugin_config.read

  Kaui::AdminTenant.upload_tenant_plugin_config(plugin_name, plugin_config, options[:username], nil, comment, options)

  redirect_to admin_tenant_path(current_tenant.id), :notice => 'Config for plugin was successfully uploaded'
end