qalam_oauth_engine 3.0.3 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7d5f44f20e6e7274024cb0db320eea0b44c806fe1071a6b1f22f55126d9705a7
4
- data.tar.gz: 98a8dbd9c702bcf62038b8acc8916c2357684995fb44538ac95a073d6254af8d
3
+ metadata.gz: 9ce5f3ea19a895f5964a450249d587a9a9e847064071bb260f42f078d3a5ed58
4
+ data.tar.gz: d2621e2449cb88ec7dfcaea89219c4e825a981e4ed82eaaf2dd9af40beb554a2
5
5
  SHA512:
6
- metadata.gz: 33f504f27fad28dd842e8cc0c8e1766e77dc1561b6fbd93e9b798ee29f72355431f7acca0007cdf0b82d2d131b82b4d3b6bb116d0190c12889dcf4bcc2f96699
7
- data.tar.gz: fd3270a95699a8cc7c9bedbda1b0ec3cc46a2bf31ff06f363e50b1d5f6dbd459b83487bbf2a8f6ae09793bffe5fdadcb16e8ae5a044f22e0564a413238f5dd14
6
+ metadata.gz: 68a55695a6a6b196a6cb7cc3402d314b987473ed58a1b78dd538b35f80903b0035beff1dd00acb714c1a2f521348f9baf978f941584098c742e182f2be003a0b
7
+ data.tar.gz: df1b190fb6e62609f9867ca96c6365786137681c2c5bc3f0b14fef895308912fce6d1956d40385e1a06d233c7e2437fd0bf5ebf3f2c98372d2ec0ea9cc62743c
@@ -5,8 +5,9 @@ module CanvasOauth
5
5
  def oauth
6
6
  if verify_oauth2_state(params[:state]) && params[:code]
7
7
  if token = canvas.get_access_token(params[:code])
8
+ set_root_account
8
9
  refresh_token = canvas.refresh_token
9
- if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, tool_consumer_instance_guid)
10
+ if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, @root_account_id, tool_consumer_instance_guid)
10
11
  redirect_to main_app.root_path
11
12
  else
12
13
  render plain: "Error: unable to save token"
@@ -22,5 +23,13 @@ module CanvasOauth
22
23
  def verify_oauth2_state(callback_state)
23
24
  callback_state.present? && callback_state == session.delete(:oauth2_state)
24
25
  end
26
+
27
+ def set_root_account
28
+ if session[:account_id]
29
+ @root_account_id = canvas.root_account_id(session[:account_id])
30
+ elsif session[:course_id]
31
+ @root_account_id = canvas.course_root_account_id(session[:course_id])
32
+ end
33
+ end
25
34
  end
26
35
  end
@@ -2,16 +2,25 @@ module CanvasOauth
2
2
  class Authorization < ActiveRecord::Base
3
3
  validates :canvas_user_id, :token, :refresh_token, :last_used_at, presence: true
4
4
 
5
- def self.cache_token(token, refresh_token, user_id, tool_consumer_instance_guid)
5
+ def self.cache_token(token, refresh_token, user_id, account_id, tool_consumer_instance_guid)
6
6
  create do |t|
7
7
  t.token = token
8
8
  t.refresh_token = refresh_token
9
9
  t.canvas_user_id = user_id
10
+ t.canvas_root_account_id = account_id
10
11
  t.tool_consumer_instance_guid = tool_consumer_instance_guid
11
12
  t.last_used_at = Time.now
12
13
  end
13
14
  end
14
15
 
16
+ def self.fetch_account(user_id, tool_consumer_instance_guid)
17
+ user_accounts = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
18
+ if canvas_auth = user_accounts.first
19
+ canvas_auth.update_attribute(:last_used_at, Time.now)
20
+ return canvas_auth.canvas_root_account_id
21
+ end
22
+ end
23
+
15
24
  def self.fetch_token(user_id, tool_consumer_instance_guid)
16
25
  user_tokens = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
17
26
  if canvas_auth = user_tokens.first
@@ -2,6 +2,7 @@ class CreateCanvasOauthAuthorizations < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table "canvas_oauth_authorizations", :force => true do |t|
4
4
  t.integer "canvas_user_id", :limit => 8
5
+ t.integer "canvas_root_account_id", :limit => 8
5
6
  t.string "tool_consumer_instance_guid", :null => false
6
7
  t.string "token"
7
8
  t.string "refresh_token"
@@ -6,7 +6,7 @@ module CanvasOauth
6
6
  attr_accessor :token, :refresh_token, :key, :secret
7
7
  attr_reader :canvas_url, :canvas_user_id
8
8
 
9
- def initialize(canvas_url, canvas_user_id, token, refresh_token, key, secret)
9
+ def initialize(canvas_url, canvas_user_id, canvas_root_account_id, token, refresh_token, key, secret)
10
10
  unless [key, secret].all?(&:present?)
11
11
  raise "Invalid Qalam oAuth configuration"
12
12
  end
@@ -14,6 +14,7 @@ module CanvasOauth
14
14
  self.refresh_token = refresh_token
15
15
  self.canvas_url = canvas_url
16
16
  self.canvas_user_id = canvas_user_id
17
+ self.canvas_root_account_id = canvas_root_account_id
17
18
  self.token = token
18
19
  self.key = key
19
20
  self.secret = secret
@@ -103,18 +104,23 @@ module CanvasOauth
103
104
  authenticated_request(:put, *params)
104
105
  end
105
106
 
106
- def paginated_get(url, params = {})
107
+ def paginated_get(url, params={})
107
108
  params[:query] ||= {}
108
- params[:query][:per_page] = PER_PAGE
109
-
109
+ params[:query][:per_page] ||= PER_PAGE
110
+ params[:query][:page] ||= 1
110
111
  all_pages = []
111
112
 
112
- while url && current_page = authenticated_get(url, params) do
113
- all_pages.concat(current_page) if valid_page?(current_page)
113
+ if params[:all_pages]
114
+ while url && current_page = authenticated_get(url, params) do
115
+ all_pages.concat(current_page) if valid_page?(current_page)
114
116
 
115
- links = LinkHeader.parse(current_page.headers['link'])
116
- url = links.find_link(["rel", "next"]).try(:href)
117
- params[:query] = nil if params[:query]
117
+ links = LinkHeader.parse(current_page.headers['link'])
118
+ url = links.find_link(["rel", "next"]).try(:href)
119
+ params[:query] = nil if params[:query]
120
+ end
121
+ else
122
+ current_page = authenticated_get(url, params)
123
+ all_pages.concat(current_page) if valid_page?(current_page)
118
124
  end
119
125
 
120
126
  all_pages
@@ -130,6 +136,10 @@ module CanvasOauth
130
136
  def canvas_user_id=(value)
131
137
  @canvas_user_id = value
132
138
  end
139
+
140
+ def canvas_root_account_id=(value)
141
+ @canvas_root_account_id = value
142
+ end
133
143
 
134
144
  def hex_sis_id(name, value)
135
145
  hex = value.unpack("H*")[0]
@@ -190,7 +200,7 @@ module CanvasOauth
190
200
  # root_account_id
191
201
  # course_root_account_id
192
202
 
193
- def get_report(account_id, report_type, params)
203
+ def get_report(account_id, report_type, params={})
194
204
  report = authenticated_post("/api/v1/accounts/#{account_id}/reports/#{report_type}", { body: params })
195
205
  report = authenticated_get "/api/v1/accounts/#{account_id}/reports/#{report_type}/#{report['id']}"
196
206
  while (report['status'] == 'created' || report['status'] == 'running')
@@ -207,108 +217,108 @@ module CanvasOauth
207
217
  end
208
218
  end
209
219
 
210
- def get_file(file_id)
220
+ def get_file(file_id, params={})
211
221
  authenticated_get "/api/v1/files/#{file_id}"
212
222
  end
213
223
 
214
- def get_accounts_provisioning_report(account_id)
224
+ def get_accounts_provisioning_report(account_id, params={})
215
225
  get_report(account_id, :provisioning_csv, 'parameters[accounts]' => true)
216
226
  end
217
227
 
218
- def get_courses
228
+ def get_courses(params={})
219
229
  paginated_get "/api/v1/courses"
220
230
  end
221
231
 
222
- def get_account(account_id)
232
+ def get_account(account_id, params={})
223
233
  authenticated_get "/api/v1/accounts/#{account_id}"
224
234
  end
225
235
 
226
- def get_account_sub_accounts(account_id)
236
+ def get_account_sub_accounts(account_id, params={})
227
237
  paginated_get "/api/v1/accounts/#{account_id}/sub_accounts", { query: { :recursive => true } }
228
238
  end
229
239
 
230
- def get_account_courses(account_id)
240
+ def get_account_courses(account_id, params={})
231
241
  paginated_get "/api/v1/accounts/#{account_id}/courses"
232
242
  end
233
243
 
234
- def get_account_users(account_id)
244
+ def get_account_users(account_id, params={})
235
245
  paginated_get "/api/v1/accounts/#{account_id}/users"
236
246
  end
237
247
 
238
- def get_course(course_id)
248
+ def get_course(course_id, params={})
239
249
  authenticated_get "/api/v1/courses/#{course_id}"
240
250
  end
241
251
 
242
- def get_section_enrollments(section_id)
252
+ def get_section_enrollments(section_id, params={})
243
253
  paginated_get "/api/v1/sections/#{section_id}/enrollments"
244
254
  end
245
255
 
246
- def get_user_enrollments(user_id)
256
+ def get_user_enrollments(user_id, params={})
247
257
  paginated_get "/api/v1/users/#{user_id}/enrollments"
248
258
  end
249
259
 
250
- def get_course_users(course_id)
260
+ def get_course_users(course_id, params={})
251
261
  paginated_get "/api/v1/courses/#{course_id}/users"
252
262
  end
253
263
 
254
- def get_all_course_users(course_id)
264
+ def get_all_course_users(course_id, params={})
255
265
  paginated_get "/api/v1/courses/#{course_id}/users", { query: {enrollment_state: ["active","invited","rejected","completed","inactive"] } }
256
266
  end
257
267
 
258
- def get_course_teachers_and_tas(course_id)
268
+ def get_course_teachers_and_tas(course_id, params={})
259
269
  paginated_get "/api/v1/courses/#{course_id}/users", { query: { enrollment_type: ['teacher', 'ta'] } }
260
270
  end
261
271
 
262
- def get_course_students(course_id)
272
+ def get_course_students(course_id, params={})
263
273
  paginated_get "/api/v1/courses/#{course_id}/students"
264
274
  end
265
275
 
266
- def get_course_active_students(course_id)
276
+ def get_course_active_students(course_id, params={})
267
277
  paginated_get "/api/v1/courses/#{course_id}/active_users"
268
278
  end
269
279
 
270
- def get_section(section_id)
280
+ def get_section(section_id, params={})
271
281
  authenticated_get "/api/v1/sections/#{section_id}"
272
282
  end
273
283
 
274
- def get_sections(course_id)
284
+ def get_sections(course_id, params={})
275
285
  paginated_get "/api/v1/courses/#{course_id}/sections", { query: { :include => ['students', 'avatar_url', 'enrollments'] } }
276
286
  end
277
287
 
278
- def get_assignments(course_id)
288
+ def get_assignments(course_id, params={})
279
289
  paginated_get "/api/v1/courses/#{course_id}/assignments"
280
290
  end
281
291
 
282
- def get_assignment(course_id, assignment_id)
292
+ def get_assignment(course_id, assignment_id, params={})
283
293
  authenticated_get "/api/v1/courses/#{course_id}/assignments/#{assignment_id}"
284
294
  end
285
295
 
286
- def get_user_profile(user_id)
296
+ def get_user_profile(user_id, params={})
287
297
  authenticated_get "/api/v1/users/#{user_id}/profile"
288
298
  end
289
299
 
290
- def create_assignment(course_id, params)
300
+ def create_assignment(course_id, params={})
291
301
  authenticated_post "/api/v1/courses/#{course_id}/assignments", { body: { assignment: params } }
292
302
  end
293
303
 
294
- def update_assignment(course_id, assignment_id, params)
304
+ def update_assignment(course_id, assignment_id, params={})
295
305
  authenticated_put "/api/v1/courses/#{course_id}/assignments/#{assignment_id}", { body: { assignment: params } }
296
306
  end
297
307
 
298
- def grade_assignment(course_id, assignment_id, user_id, params)
308
+ def grade_assignment(course_id, assignment_id, user_id, params={})
299
309
  authenticated_put "/api/v1/courses/#{course_id}/assignments/#{assignment_id}/submissions/#{user_id}", { body: params }
300
310
  end
301
311
 
302
- def get_submission(course_id, assignment_id, user_id)
312
+ def get_submission(course_id, assignment_id, user_id, params={})
303
313
  authenticated_get "/api/v1/courses/#{course_id}/assignments/#{assignment_id}/submissions/#{user_id}"
304
314
  end
305
315
 
306
- def course_account_id(course_id)
316
+ def course_account_id(course_id, params={})
307
317
  course = get_course(course_id)
308
318
  course['account_id'] if course
309
319
  end
310
320
 
311
- def root_account_id(account_id)
321
+ def root_account_id(account_id, params={})
312
322
  if account_id && account = get_account(account_id)
313
323
  root_id = account['root_account_id']
314
324
  end
@@ -316,7 +326,7 @@ module CanvasOauth
316
326
  root_id || account_id
317
327
  end
318
328
 
319
- def course_root_account_id(course_id)
329
+ def course_root_account_id(course_id, params={})
320
330
  root_account_id(course_account_id(course_id))
321
331
  end
322
332
  ### END CANVAS API ###
@@ -324,44 +334,66 @@ module CanvasOauth
324
334
  ### QALAM API ENDPOINTS ###
325
335
  # get_canvas_user_profile
326
336
  # get_course_active_pages
327
- # course_external_tool_update
328
- # account_external_tool_update
337
+ # update_external_tool_nav
329
338
  # get_school_details
330
- # get_grade_students
331
- # get_classroom_students
339
+ # get_school_grades
340
+ # get_school_class_rooms
341
+ # get_school_subjects
342
+ # get_students_by_grade
343
+ # get_students_by_class_room
332
344
 
333
- def get_canvas_user_profile
345
+ def get_canvas_user_profile(params={})
334
346
  authenticated_get "/api/v1/users/#{canvas_user_id}/profile"
335
347
  end
336
348
 
337
- def get_course_active_pages(course_id, publish=nil)
349
+ def get_course_active_pages(course_id, publish=nil, params={})
338
350
  unless publish.nil?
339
- paginated_get "/api/v1/courses/#{course_id}/pages?published=#{publish}&sort=created_at&order=desc"
351
+ paginated_get "/api/v1/courses/#{course_id}/pages?published=#{publish}&sort=created_at&order=desc", params
340
352
  else
341
- paginated_get "/api/v1/courses/#{course_id}/pages?sort=created_at&order=desc"
353
+ paginated_get "/api/v1/courses/#{course_id}/pages?sort=created_at&order=desc", params
342
354
  end
343
355
  end
344
356
 
345
- def course_external_tool_update(external_tool_url, tool_id, account_id, publish)
346
- authenticated_put "/api/v1/accounts/#{account_id}/external_tools_url", { body: {"course_navigation"=>{"enabled"=>"#{publish}"},
347
- "tool_id"=>tool_id, "external_tool_url"=>external_tool_url} }
357
+ def update_external_tool_nav(external_tool_url, tool_id, publish, type, account_id=@canvas_root_account_id, params={})
358
+ account_id ||= @canvas_root_account_id
359
+ if type == 'account'
360
+ body = {"account_navigation"=>{"enabled"=>"#{publish}"},
361
+ "tool_id"=>tool_id, "external_tool_url"=>external_tool_url}
362
+ elsif type == 'course'
363
+ body = {"course_navigation"=>{"enabled"=>"#{publish}"},
364
+ "tool_id"=>tool_id, "external_tool_url"=>external_tool_url}
365
+ end
366
+ authenticated_put "/api/v1/accounts/#{account_id}/external_tools_url", { body: body } if body
348
367
  end
349
368
 
350
- def account_external_tool_update(external_tool_url, tool_id, account_id, publish)
351
- authenticated_put "/api/v1/accounts/#{account_id}/external_tools_url", { body: {"account_navigation"=>{"enabled"=>"#{publish}"},
352
- "tool_id"=>tool_id, "external_tool_url"=>external_tool_url} }
369
+ def get_school_details(account_id=@canvas_root_account_id, params={})
370
+ account_id ||= @canvas_root_account_id
371
+ authenticated_get "/api/v1/accounts/#{account_id}/school_details"
353
372
  end
354
373
 
355
- def get_school_details(account_id)
356
- authenticated_get "/api/v1/accounts/#{account_id}/school_details"
374
+ def get_school_grades(account_id=@canvas_root_account_id, params={})
375
+ account_id ||= @canvas_root_account_id
376
+ paginated_get "/api/v1/accounts/#{account_id}/school_grades", params
377
+ end
378
+
379
+ def get_school_subjects(account_id=@canvas_root_account_id, params={})
380
+ account_id ||= @canvas_root_account_id
381
+ paginated_get "/api/v1/accounts/#{account_id}/school_subjects", params
382
+ end
383
+
384
+ def get_school_class_rooms(account_id=@canvas_root_account_id, params={})
385
+ account_id ||= @canvas_root_account_id
386
+ paginated_get "/api/v1/accounts/#{account_id}/school_class_rooms", params
357
387
  end
358
388
 
359
- def get_grade_students(account_id, grade_id)
360
- paginated_get "/api/v1/accounts/#{account_id}/grade_students/#{grade_id}"
389
+ def get_students_by_grade(grade_id, account_id=@canvas_root_account_id, params={})
390
+ account_id ||= @canvas_root_account_id
391
+ paginated_get "/api/v1/accounts/#{account_id}/grade_students/#{grade_id}", params
361
392
  end
362
393
 
363
- def get_classroom_students(account_id, class_id)
364
- paginated_get "/api/v1/accounts/#{account_id}/classroom_students/#{class_id}"
394
+ def get_students_by_class_room(class_room_id, account_id=@canvas_root_account_id, params={})
395
+ account_id ||= @canvas_root_account_id
396
+ paginated_get "/api/v1/accounts/#{account_id}/class_room_students/#{class_room_id}", params
365
397
  end
366
398
  ### END QALAM API ###
367
399
  end
@@ -1,11 +1,12 @@
1
1
  module CanvasOauth
2
2
  class CanvasApiExtensions
3
3
  def self.build(canvas_url, user_id, tool_consumer_instance_guid)
4
- refresh_token = CanvasOauth::Authorization.fetch_refresh_token(user_id, tool_consumer_instance_guid)
4
+ account_id = CanvasOauth::Authorization.fetch_account(user_id, tool_consumer_instance_guid)
5
5
  token = CanvasOauth::Authorization.fetch_token(user_id, tool_consumer_instance_guid)
6
+ refresh_token = CanvasOauth::Authorization.fetch_refresh_token(user_id, tool_consumer_instance_guid)
6
7
  canvas_key = ((CanvasLtiKey.table_exists? && CanvasLtiKey.find_by(canvas_url: canvas_url)&.key) or CanvasConfig.key)
7
8
  canvas_secret = ((CanvasLtiKey.table_exists? && CanvasLtiKey.find_by(key: canvas_key, canvas_url: canvas_url)&.secret) or CanvasConfig.secret)
8
- CanvasApi.new(canvas_url, user_id, token, refresh_token, canvas_key, canvas_secret)
9
+ CanvasApi.new(canvas_url, user_id, account_id, token, refresh_token, canvas_key, canvas_secret)
9
10
  end
10
11
  end
11
12
  end
@@ -1,3 +1,3 @@
1
1
  module CanvasOauth
2
- VERSION = "3.0.3"
2
+ VERSION = "3.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qalam_oauth_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.3
4
+ version: 3.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Donahue
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2021-08-09 00:00:00.000000000 Z
14
+ date: 2021-08-24 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty