implemented team-dependent exercise policy

This commit is contained in:
Hauke Klement
2015-01-29 17:13:22 +01:00
parent a6d3c7c539
commit cbde9529aa
2 changed files with 28 additions and 9 deletions

View File

@ -4,20 +4,27 @@ class ExercisePolicy < AdminOrAuthorPolicy
end
private :author?
[:clone?, :statistics?].each do |action|
define_method(action) { admin? || author? }
[:clone?, :destroy?, :edit?, :show?, :statistics?, :update?].each do |action|
define_method(action) { admin? || author? || team_member? }
end
[:implement?, :submit?].each do |action|
define_method(action) { everyone }
end
def team_member?
@record.team.try(:members, []).include?(@user)
end
private :team_member?
class Scope < Scope
def resolve
if @user.admin?
@scope.all
elsif @user.internal?
@scope.where("user_id = #{@user.id} OR public = TRUE OR (team_id IS NOT NULL AND team_id IN (SELECT t.id FROM teams t JOIN internal_users_teams iut ON t.id = iut.team_id WHERE iut.internal_user_id = #{@user.id}))")
else
@scope.where("user_id = #{@user.id} OR public = TRUE")
@scope.none
end
end
end