Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ def after_sign_in_path_for(resource)
new_facility_path
end

def render_unauthorized(text)
render_status_code(text, 401)
end

def render_not_found(text)
render_status_code(text, 404)
end

def render_status_code(text, code)
render(text: text, status: code, layout: 'application') && return
end

protected

Expand Down
12 changes: 10 additions & 2 deletions app/controllers/facilities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class FacilitiesController < ApplicationController
before_action :set_facility_user
before_action :set_facility, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:show]
before_action :set_facility_user

# GET /facilities
# GET /facilities.json
Expand Down Expand Up @@ -68,7 +68,15 @@ def destroy
private
# Use callbacks to share common setup or constraints between actions.
def set_facility
@facility = Facility.find(params[:id])
if current_user.site_admin? || action_name == 'show'
@facility = Facility.find(params[:id])
else # restrict edit/update/destroy to only facilities associated with the user.
begin
@facility = current_user.facilities.find(params[:id])
rescue
render_not_found('Sorry, that facility could not be found.')
end
end
end

def set_facility_user
Expand Down
1 change: 0 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ def full_name
def display_name
full_name || email
end

end