Skip to content

Commit

Permalink
Merge pull request #132 from getguesstimate/595-calculator-privacy-co…
Browse files Browse the repository at this point in the history
…ntrols

Calculators should match the privacy restrictions of their underlying spaces. Closes getguesstimate/guesstimate-app#595
  • Loading branch information
OAGr authored Jul 15, 2016
2 parents 45af73a + 7a4d339 commit 62703c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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

0 comments on commit 62703c6

Please sign in to comment.