Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculators should match the privacy restrictions of their underlying spaces. Closes getguesstimate/guesstimate-app#595 #132

Merged
merged 1 commit into from
Jul 15, 2016
Merged
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
14 changes: 11 additions & 3 deletions app/controllers/calculators_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CalculatorsController < ApplicationController
before_action :authenticate, :set_space, :check_authorization, only: [:create]
before_action :authenticate, :set_space, :check_create_authorization, only: [:create]
before_action :set_calculator, :check_show_authorization, only: [:show]

# GET /calculators/:id
def show
@calculator = Calculator.find(params[:id])
render json: CalculatorRepresenter.new(@calculator).to_json
end

Expand All @@ -21,7 +21,15 @@ def set_space
@space = Space.find(params[:space_id])
end

def check_authorization
def set_calculator
@calculator = Calculator.find(params[:id])
end

def check_show_authorization
head :unauthorized unless @calculator.space.viewable_by_user? current_user
end

def check_create_authorization
head :unauthorized unless @space.editable_by_user? current_user
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def has_interesting_metrics?
metrics.length > 3
end

def viewable_by_user?(user)
is_public? || (user.present? && editable_by_user?(user))
end

def editable_by_user?(user)
if organization
user.member_of?(organization)
Expand Down