Class: AtomicLti::Services::NamesAndRoles

Inherits:
Base
  • Object
show all
Defined in:
app/lib/atomic_lti/services/names_and_roles.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_next_url, #headers, #logged_service_call, #service_delete, #service_get, #service_post, #service_put

Constructor Details

#initialize(id_token_decoded:) ⇒ NamesAndRoles

Returns a new instance of NamesAndRoles.



5
6
7
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 5

def initialize(id_token_decoded:)
  super(id_token_decoded: id_token_decoded)
end

Class Method Details

.enabled?(id_token_decoded) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 26

def self.enabled?(id_token_decoded)
  return false unless id_token_decoded&.dig(AtomicLti::Definitions::NAMES_AND_ROLES_CLAIM)

  (AtomicLti::Definitions::NAMES_AND_ROLES_SERVICE_VERSIONS &
    (id_token_decoded.dig(AtomicLti::Definitions::NAMES_AND_ROLES_CLAIM, "service_versions") || [])).present?
end

Instance Method Details

#endpointObject



13
14
15
16
17
18
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 13

def endpoint
  url = @id_token_decoded.dig(AtomicLti::Definitions::NAMES_AND_ROLES_CLAIM, "context_memberships_url")
  raise AtomicLti::Exceptions::NamesAndRolesError, "Unable to access names and roles" unless url.present?

  url
end

#list(query: {}, page_url: nil) ⇒ Object

List names and roles limit query param - see ‘Limit query parameter’ section of NRPS spec to get differences - see ‘Membership differences’ section of NRPS spec query parameter of ‘=> “Learner”’ will filter the memberships to just those which have a Learner role. query parameter of ‘=> “49566-rkk96”’ will filter the memberships to just those which have access to the resource link with ID ‘49566-rkk96’



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 44

def list(query: {}, page_url: nil)
  url = if page_url.present?
          page_url
        else
          uri = Addressable::URI.parse(endpoint)
          uri.query_values = (uri.query_values || {}).merge(query)
          uri.to_str
        end
  response, = service_get(
    url,
    headers: headers(
      {
        "Accept" => "application/vnd.ims.lti-nrps.v2.membershipcontainer+json",
      },
    ),
  )

  verify_received_user_names(response)
end

#list_all(query: {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 64

def list_all(query: {})
  page_body = nil

  members = AtomicLti::PagingHelper.paginate_request do |next_link|
    result_page = list(query: query, page_url: next_link)
    page_body = JSON.parse(result_page.body)
    [page_body["members"], get_next_url(result_page)]
  end

  page_body["members"] = members
  page_body
end

#scopesObject



9
10
11
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 9

def scopes
  [AtomicLti::Definitions::NAMES_AND_ROLES_SCOPE]
end

#url_for(query = nil) ⇒ Object



20
21
22
23
24
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 20

def url_for(query = nil)
  url = endpoint.dup
  url << "?#{query}" if query.present?
  url
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 33

def valid?
  self.class.enabled?(@id_token_decoded)
end

#verify_received_user_names(names_and_roles_memberships) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/lib/atomic_lti/services/names_and_roles.rb', line 77

def verify_received_user_names(names_and_roles_memberships)
  if names_and_roles_memberships&.body.present?
    members = JSON.parse(names_and_roles_memberships.body)["members"]
    if members.present? && members.all? { |member| member["name"].nil? }
      raise(
        AtomicLti::Exceptions::NamesAndRolesError,
        "Unable to fetch user data. Your LTI key may be set to private.",
      )
    end
  end
  names_and_roles_memberships
end