Class: PandaPal::Session

Inherits:
PandaPalRecord show all
Defined in:
app/models/panda_pal/session.rb

Defined Under Namespace

Classes: DataSerializer, RoleStore

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



10
11
12
13
14
15
16
# File 'app/models/panda_pal/session.rb', line 10

def [](key)
  if self.class.column_names.include?(key.to_s)
    super
  else
    data[key]
  end
end

#[]=(key, value) ⇒ Object



18
19
20
21
22
23
24
# File 'app/models/panda_pal/session.rb', line 18

def []=(key, value)
  if self.class.column_names.include?(key.to_s)
    super
  else
    data[key] = value
  end
end

#cache(key, &blk) ⇒ Object



26
27
28
29
30
31
# File 'app/models/panda_pal/session.rb', line 26

def cache(key, &blk)
  data[:cache] ||= {}
  return data[:cache][key] if data[:cache].key?(key)

  data[:cache][key] = blk.call
end

#canvas_account_role_labels(account = 'self') ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/panda_pal/session.rb', line 88

def ( = 'self')
   = 'self' if .to_s == "root"
   = .canvas_id if .respond_to?(:canvas_id)

  if defined?(::Admin) && ::Admin < ::ActiveRecord::Base
     = current_organization. if  == 'self'
    adm_query = ::Admin.where(canvas_account_id: , workflow_state: "active")
    adm_query.pluck(:role_name)
  else
    Rails.cache.fetch([self.class.name, "AccountAdminLinks", , canvas_user_id], expires_in: 1.hour) do
      admin_entries = canvas_sync_client.(, user_id: [canvas_user_id])
      admin_entries = admin_entries.select{|ent| ent[:workflow_state] == 'active' }
      admin_entries.map{|ent| ent[:role] }
    end
  end
end

#canvas_role_labelsObject



83
84
85
86
# File 'app/models/panda_pal/session.rb', line 83

def canvas_role_labels
  labels = get_lti_cust_param('custom_canvas_role')
  labels.is_a?(String) ? labels.split(',') : []
end

#canvas_site_admin?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/models/panda_pal/session.rb', line 117

def canvas_site_admin?
  lti_roles.system_roles.include?("sys_admin")
end

#canvas_user_idObject



109
110
111
# File 'app/models/panda_pal/session.rb', line 109

def canvas_user_id
  get_lti_cust_param('canvas_user_id')
end

#custom_lti_paramsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/panda_pal/session.rb', line 47

def custom_lti_params
  @custom_lti_params ||= begin
    # LT 1.3
    custom_params = launch_params["https://purl.imsglobal.org/spec/lti/claim/custom"]
    return custom_params if custom_params.present?

    # LTI 1.0/1.1
    custom_params = {}
    launch_params.each do |k, v|
      next unless k.start_with?("custom_")
      custom_params[k[7..-1]] = v
    end

    custom_params.with_indifferent_access
  end
end

#get_lti_cust_param(key, default: :if_not_var) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/panda_pal/session.rb', line 64

def get_lti_cust_param(key, default: :if_not_var)
  nkey = key.to_s.gsub(/^custom_/, '')
  default_value = ->() { PandaPal.lti_custom_params[nkey] || PandaPal.lti_custom_params["custom_#{nkey}"] }

  val = launch_params.dig("https://purl.imsglobal.org/spec/lti/claim/custom", nkey) || launch_params[nkey] || launch_params["custom_#{nkey}"]

  if default == :if_not_var
    if val.is_a?(String) && /\$[\.\w]+/.match?(val) && val == default_value[]
      return nil
    end
  elsif default && !val.present?
    return default_value[]
  elsif !default && val == default_value[]
    return nil
  end

  val
end

#launch_paramsObject



33
34
35
# File 'app/models/panda_pal/session.rb', line 33

def launch_params
  data[:launch_params] || {}
end

#lti_launch_placementObject



43
44
45
# File 'app/models/panda_pal/session.rb', line 43

def lti_launch_placement
  launch_params['https://www.instructure.com/placement'] || launch_params[:launch_type]
end

#lti_platformObject



37
38
39
40
41
# File 'app/models/panda_pal/session.rb', line 37

def lti_platform
  return nil unless data[:lti_platform].present?

  @lti_platform ||= Platform.from_serialized(data[:lti_platform])
end

#lti_rolesObject



105
106
107
# File 'app/models/panda_pal/session.rb', line 105

def lti_roles
  @lti_roles ||= RoleStore.new(launch_params["https://purl.imsglobal.org/spec/lti/claim/roles"] || launch_params['ext_roles'] || '')
end

#userObject



113
114
115
# File 'app/models/panda_pal/session.rb', line 113

def user
  @user ||= ::User.find_by(canvas_id: canvas_user_id) if defined?(::User) && ::User < ::ActiveRecord::Base
end