Class: Kaui::TenantsController

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

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient

Instance Method Details

#indexObject



6
7
8
9
10
11
12
13
14
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
# File 'app/controllers/kaui/tenants_controller.rb', line 6

def index
  begin

    # Retrieve current user and extract allowed list of tenants
    user = current_user
    allowed_user = Kaui::AllowedUser.find_by_kb_username(user.kb_username)
    @tenants = (allowed_user.kaui_tenants if allowed_user) || []

    #
    # If there is nothing we check for override with KillBillClient.api_key/KillBillClient.api_secret
    # If there is only one, we skip the tenant screen since the choice is obvious
    # If not, we allow user to chose what he wants
    #
    case @tenants.size
      when 0
        # If KillBillClient.api_key and KillBillClient.api_secret are not set, the client library will throw
        # an KillBillClient::API::Unauthorized exception which will end up in the rescue below
        tenant = KillBillClient::Model::Tenant.find_by_api_key(KillBillClient.api_key, {
            :session_id => user.kb_session_id
        })
        if tenant.present?
          select_tenant_for_tenant_id(tenant.tenant_id, tenant.external_key)
        end
      when 1
        # If there is only one tenant defined we skip the screen and set the tenant for the user
        select_tenant_for_tenant_id(@tenants[0].kb_tenant_id, @tenants[0].name)
      else
        # Jump to default view allowing to chose which tenant to pick
        respond_to do |format|
          format.html # index.html.erb
          format.json { render :json => @tenants }
        end
    end
  rescue => e
    flash[:error] = "Error while retrieving tenants: No tenants configured for users AND KillBillClient.api_key, KillBillClient.api_secret have not been set"
    @tenants = []
    # We then display the view with NO tenants and the flash error so user understands he does not have any configured tenants available
  end

end

#select_tenantObject



47
48
49
50
51
# File 'app/controllers/kaui/tenants_controller.rb', line 47

def select_tenant
  kb_tenant_id = params[:kb_tenant_id]
  tenant = Kaui::Tenant.find_by_kb_tenant_id(kb_tenant_id)
  select_tenant_for_tenant_id(tenant.kb_tenant_id, tenant.name)
end