Skip to content

Commit

Permalink
fix: use unique perms for scorm test retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
satikaj committed Jul 2, 2024
1 parent 5db5f35 commit 08a0090
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/api/authentication_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ class AuthenticationApi < Grape::API
desc 'Get SCORM authentication token'
get '/auth/scorm' do
if authenticated?
unless authorise? current_user, User, :get_scorm_test
error!({ error: 'You cannot access SCORM tests' }, 403)
unless authorise? current_user, User, :get_scorm_token
error!({ error: 'You cannot get SCORM tokens' }, 403)
end

token = current_user.auth_tokens.find_by(token_type: 'scorm')
Expand Down
7 changes: 4 additions & 3 deletions app/api/scorm_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ def stream_file_from_zip(zip_path, file_path)
requires :task_def_id, type: Integer, desc: 'Task Definition ID to get SCORM test data for'
end
get '/scorm/:task_def_id/:username/:auth_token/*file_path' do
unless authorise? current_user, User, :get_scorm_test
error!({ error: 'You cannot access SCORM tests' }, 403)
task_def = TaskDefinition.find(params[:task_def_id])

unless authorise? current_user, task_def.unit, :get_unit
error!({ error: 'You cannot access SCORM tests of unit' }, 403)
end

env['api.format'] = :txt
task_def = TaskDefinition.find(params[:task_def_id])
if task_def.has_scorm_data?
zip_path = task_def.task_scorm_data
content_type 'application/octet-stream'
Expand Down
8 changes: 5 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,22 @@ def self.permissions
:convene_units,
:get_staff_list,
:get_teaching_periods,
:use_overseer
:use_overseer,
:get_scorm_token
]

# What can tutors do with users?
tutor_role_permissions = [
:get_unit_roles,
:download_unit_csv,
:get_teaching_periods
:get_teaching_periods,
:get_scorm_token
]

# What can students do with users?
student_role_permissions = [
:get_teaching_periods,
:get_scorm_test
:get_scorm_token
]

# Return the permissions hash
Expand Down
4 changes: 2 additions & 2 deletions test/api/auth_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ def test_token_signout_works_with_multiple
# # SCORM auth test

def test_scorm_auth
tutor = FactoryBot.create(:user, :tutor)
admin = FactoryBot.create(:user, :admin)

add_auth_header_for(user: tutor)
add_auth_header_for(user: admin)

# When user is unauthorised
get "api/auth/scorm"
Expand Down
1 change: 0 additions & 1 deletion test/api/scorm_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_serve_scorm_content
assert_equal 'text/javascript', last_response.content_type

tutor = FactoryBot.create(:user, :tutor, username: :test_tutor)
unit.employ_staff(tutor, Role.tutor)

# When the user is unauthorised
get "/api/scorm/#{td.id}/#{tutor.username}/#{auth_token(tutor)}/index.html"
Expand Down

0 comments on commit 08a0090

Please sign in to comment.