Skip to content

Commit 9312a50

Browse files
committed
secure organisations configuration page
1 parent 42aee1a commit 9312a50

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

app/controllers/organisations_controller.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class OrganisationsController < ApplicationController
2+
before_action :authorize_user, except: [:index]
23
before_action :load_org, only: %i(
34
config_saml_app update show setup_saml save_config_saml_app
45
remove_user_saml_app add_user_saml_app
@@ -75,10 +76,6 @@ def create
7576
end
7677

7778
def update
78-
unless current_user.admin?
79-
return redirect_to organisations_path, notice: 'Unauthorized access'
80-
end
81-
8279
@org.update_profile(organisation_params.to_h || {})
8380
if @org.errors.blank?
8481
flash[:success] = 'Successfully updated organisation'
@@ -105,6 +102,15 @@ def setup_saml
105102

106103
private
107104

105+
def authorize_user
106+
unless current_user.admin?
107+
respond_to do |format|
108+
format.html { redirect_to organisations_path, notice: 'Unauthorized access' }
109+
format.json { render json: {}, status: :unauthorized }
110+
end
111+
end
112+
end
113+
108114
def load_org
109115
id = params[:id] || params[:organisation_id]
110116
@org = Organisation.where(id: id).first

spec/controllers/organisations_controller_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
}
2828
end
2929

30+
describe 'GET #index' do
31+
context 'authenticated as non admin' do
32+
it 'should respond with 200' do
33+
create(:user)
34+
non_admin = create(:user, admin: false)
35+
sign_in non_admin
36+
organisation = create(:organisation, valid_attributes)
37+
get :index
38+
expect(response.status).to eq(200)
39+
end
40+
end
41+
end
42+
3043
describe 'PATCH #update' do
3144
context 'authenticated as admin' do
3245
it 'should update requested organisations' do
@@ -89,4 +102,26 @@
89102
end
90103
end
91104
end
105+
106+
describe 'GET #config_saml_app' do
107+
context 'authenticated as non admin' do
108+
it 'should redirect to organisations path' do
109+
create(:user)
110+
non_admin = create(:user, admin: false)
111+
sign_in non_admin
112+
organisation = create(:organisation, valid_attributes)
113+
get :config_saml_app, params: { organisation_id: organisation.id, app_name: 'datadog' }
114+
expect(response).to redirect_to(organisations_path)
115+
end
116+
117+
it 'should flash unauthorized access' do
118+
create(:user)
119+
non_admin = create(:user, admin: false)
120+
sign_in non_admin
121+
organisation = create(:organisation, valid_attributes)
122+
get :config_saml_app, params: { organisation_id: organisation.id, app_name: 'datadog' }
123+
expect(flash[:notice]).to eq('Unauthorized access')
124+
end
125+
end
126+
end
92127
end

0 commit comments

Comments
 (0)