Skip to content

Commit

Permalink
Enhancement: Added authorization checks for ticket controller actions…
Browse files Browse the repository at this point in the history
…: ticket_customer ticket_history ticket_related ticket_recent ticket_merge ticket_split.
  • Loading branch information
rolfschmidt authored and thorsteneckel committed Jun 10, 2020
1 parent 4014839 commit 6e56aee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TicketsController < ApplicationController
include ChecksUserAttributesByCurrentUserPermission
include TicketStats

prepend_before_action -> { authorize! }, only: %i[create selector import_example import_start]
prepend_before_action -> { authorize! }, only: %i[create selector import_example import_start ticket_customer ticket_history ticket_related ticket_recent ticket_merge ticket_split]
prepend_before_action :authentication_check

# GET /api/v1/tickets
Expand Down
1 change: 1 addition & 0 deletions app/policies/controllers/tickets_controller_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Controllers::TicketsControllerPolicy < Controllers::ApplicationControllerPolicy
permit! %i[import_example import_start], to: 'admin'
permit! :selector, to: 'admin.*'
permit! %i[ticket_customer ticket_history ticket_related ticket_recent ticket_merge ticket_split], to: 'ticket.agent'
permit! :create, to: ['ticket.agent', 'ticket.customer']
end
42 changes: 41 additions & 1 deletion spec/requests/ticket_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,10 @@
created_by_id: 1,
)

authenticated_as(customer_user)
get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
expect(response).to have_http_status(:unauthorized)

authenticated_as(agent_user)
get "/api/v1/ticket_split?ticket_id=#{ticket.id}&article_id=#{article.id}&form_id=new_form_id123", params: {}, as: :json
expect(response).to have_http_status(:ok)
Expand Down Expand Up @@ -1918,6 +1922,10 @@
customer_id: customer_user.id,
)

authenticated_as(customer_user)
get "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
expect(response).to have_http_status(:unauthorized)

authenticated_as(agent_user)
get "/api/v1/ticket_merge/#{ticket2.id}/#{ticket1.id}", params: {}, as: :json
expect(response).to have_http_status(:ok)
Expand Down Expand Up @@ -2068,7 +2076,39 @@
expect(json_response['assets'].class).to eq(Hash)
expect(json_response['assets']['User'][customer_user.id.to_s]).not_to be_nil
expect(json_response['assets']['Ticket'][ticket1.id.to_s]).not_to be_nil

authenticated_as(customer_user)
get "/api/v1/ticket_history/#{ticket1.id}", params: {}, as: :json
expect(response).to have_http_status(:unauthorized)
end

it 'does ticket related' do
ticket1 = create(
:ticket,
title: 'some title',
group: ticket_group,
customer_id: customer_user.id,
)

authenticated_as(agent_user)
get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
expect(response).to have_http_status(:ok)

authenticated_as(customer_user)
get "/api/v1/ticket_related/#{ticket1.id}", params: {}, as: :json
expect(response).to have_http_status(:unauthorized)
end

it 'does ticket recent' do
authenticated_as(agent_user)
get '/api/v1/ticket_recent', params: {}, as: :json
expect(response).to have_http_status(:ok)

authenticated_as(customer_user)
get '/api/v1/ticket_recent', params: {}, as: :json
expect(response).to have_http_status(:unauthorized)
end

end

describe 'stats' do
Expand Down Expand Up @@ -2213,7 +2253,7 @@
end

context 'as authorized customer', authenticated_as: -> { customer_authorized } do
include_examples 'has access'
include_examples 'has no access'
end

context 'as unauthorized customer', authenticated_as: -> { customer_unauthorized } do
Expand Down

0 comments on commit 6e56aee

Please sign in to comment.