Class: PandaPal::Organization

Constant Summary collapse

CONST_PLATFORM_TYPE =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PandaPal::OrganizationConcerns::TaskScheduling

build_settings_entry, #generate_schedule, #sync_schedule

Methods included from PandaPal::OrganizationConcerns::SettingsValidation

#settings_structure, #validate_settings

Methods included from PandaPal::OrganizationConcerns::OrganizationBuilder

#generate_orgbuilder_ruby, #interactive_install!, #interactive_update!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object



142
143
144
145
146
147
148
# File 'app/models/panda_pal/organization.rb', line 142

def method_missing(method, *args, **kwargs, &block)
  if (self.class == PandaPal::Organization) && (plat_api = platform_api).present?
    plat_api.send(method, *args, **kwargs, &block)
  else
    super
  end
end

Class Method Details

.currentObject



96
97
98
# File 'app/models/panda_pal/organization.rb', line 96

def self.current
  find_by_name(Apartment::Tenant.current)
end

.extend_platform_api(platform_type = CONST_PLATFORM_TYPE, &blk) ⇒ Object

Extend a particular type of Platform API.



152
153
154
155
# File 'app/models/panda_pal/organization.rb', line 152

def self.extend_platform_api(platform_type = CONST_PLATFORM_TYPE, &blk)
  scl = platform_type.organization_api
  scl.class_eval(&blk)
end

Instance Method Details

#create_api(logic, expiration: nil, uses: nil, host: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/panda_pal/organization.rb', line 100

def create_api(logic, expiration: nil, uses: nil, host: nil)
  switch_tenant do
    logic = "current_organization.#{logic}" if logic.is_a?(Symbol)
    ac = ApiCall.create!(
      logic: logic,
      expiration: expiration,
      uses_remaining: uses,
    )
    ac.call_url(host: host)
  end
end

#encryption_keyObject



78
79
80
81
82
83
84
85
86
# File 'app/models/panda_pal/organization.rb', line 78

def encryption_key
  # production environment might not have loaded secret_key_base yet.
  # In that case, just read it from env.
  if (Rails.application.secrets.secret_key_base)
    Rails.application.secrets.secret_key_base[0,32]
  else
    ENV["SECRET_KEY_BASE"][0,32]
  end
end

#platform_api(platform_type = primary_platform) ⇒ Object

Retrieve the specified Platform API for this Organization. If only a single Platform is used (when lti_options is a constant string or nil), passing the platform_type is unnecessary

The API is currently an Organization subclass (using ‘becomes()`), but such may change.



161
162
163
164
165
166
# File 'app/models/panda_pal/organization.rb', line 161

def platform_api(platform_type = primary_platform)
  return nil if platform_type.nil?
  scl = platform_type.organization_api
  return self if scl == self.class
  becomes(scl)
end

#rename!(new_name) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'app/models/panda_pal/organization.rb', line 112

def rename!(new_name)
  do_switch = Apartment::Tenant.current == name
  ActiveRecord::Base.connection.execute(
    "ALTER SCHEMA \"#{name}\" RENAME TO \"#{new_name}\";"
  )
  self.class.where(id: id).update_all(name: new_name)
  reload
  switch_tenant if do_switch
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

… via method_missing/delegation if this is a multi-platform tool

Returns:

  • (Boolean)


134
135
136
137
138
139
140
# File 'app/models/panda_pal/organization.rb', line 134

def respond_to_missing?(name, include_private = false)
  if (self.class == PandaPal::Organization) && (plat_api = platform_api).present?
    plat_api.respond_to?(name, include_private)
  else
    super
  end
end

#settings=(settings) ⇒ Object



48
49
50
51
# File 'app/models/panda_pal/organization.rb', line 48

def settings=(settings)
  settings = settings.with_indifferent_access if settings.is_a?(Hash) && !settings.is_a?(HashWithIndifferentAccess)
  self.settings_panda_pal_super = settings
end

#settings_panda_pal_super=Object



47
# File 'app/models/panda_pal/organization.rb', line 47

alias_method "settings_panda_pal_super=", "settings="

#switch_tenant(&block) ⇒ Object



88
89
90
91
92
93
94
# File 'app/models/panda_pal/organization.rb', line 88

def switch_tenant(&block)
  if block_given?
    Apartment::Tenant.switch(name, &block)
  else
    Apartment::Tenant.switch!(name)
  end
end

#trusted_platform?(platform) ⇒ Boolean

Determines if the specified platform can create sessions in this organization

Returns:

  • (Boolean)


175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/panda_pal/organization.rb', line 175

def trusted_platform?(platform)
  # Trust Instructure-hosted Canvas
  return true if platform.is_a?(PandaPal::Platform::Canvas) && platform.is_trusted_env?

  # Trust issuers added to the Org settings
  if (issuer = platform.platform_uri rescue nil).present?
    trusted_issuers = settings.dig(:lti, :trusted_platforms) || []
    return true if trusted_issuers.include?(issuer)
  end

  false
end