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



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

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

#[]=(key, value) ⇒ Object



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

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

#cache(key, &blk) ⇒ Object



25
26
27
28
# File 'app/models/panda_pal/session.rb', line 25

def cache(key, &blk)
  data[:cache] ||= {}
  data[:cache][key] ||= blk.call
end

#canvas_account_role_labels(account = 'self') ⇒ Object



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

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



80
81
82
83
# File 'app/models/panda_pal/session.rb', line 80

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)


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

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

#canvas_user_idObject



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

def canvas_user_id
  get_lti_cust_param('canvas_user_id')
end

#custom_lti_paramsObject



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

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



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

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



30
31
32
# File 'app/models/panda_pal/session.rb', line 30

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

#lti_launch_placementObject



40
41
42
# File 'app/models/panda_pal/session.rb', line 40

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

#lti_platformObject



34
35
36
37
38
# File 'app/models/panda_pal/session.rb', line 34

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

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

#lti_rolesObject



102
103
104
# File 'app/models/panda_pal/session.rb', line 102

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

#userObject



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

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