diff --git a/app/controllers/sub_group_principles_controller.rb b/app/controllers/sub_group_principles_controller.rb index 4683ad82..088e6f7e 100644 --- a/app/controllers/sub_group_principles_controller.rb +++ b/app/controllers/sub_group_principles_controller.rb @@ -6,10 +6,10 @@ class SubGroupPrinciplesController < ApplicationController before_action :require_sssl def index - authorize Principle.new(sub_group: @sub_group), policy_class: SubGroupPrinciplePolicy + authorize @sub_group, policy_class: SubGroupPrinciplePolicy @evaluation = current_evaluation @principles = @evaluation.principles.where(sub_group: @sub_group).order(:type, :id) - @can_edit = true + @can_edit = SubGroupPrinciplePolicy.new(current_user, @sub_group).edit? end def update @@ -26,7 +26,7 @@ def create @principle.evaluation = @evaluation @principle.sub_group = @sub_group - authorize @principle, policy_class: SubGroupPrinciplePolicy + authorize @sub_group, policy_class: SubGroupPrinciplePolicy # Todo add error handling @principle.save @can_edit = true diff --git a/app/controllers/sub_groups_controller.rb b/app/controllers/sub_groups_controller.rb index 9b0489ec..efe64a6e 100644 --- a/app/controllers/sub_groups_controller.rb +++ b/app/controllers/sub_groups_controller.rb @@ -16,7 +16,8 @@ def show authorize @sub_group @policy = policy(@sub_group) @sub_group_memberships = @sub_group.sub_group_memberships.includes(membership: :user) - @sub_group_principle_policy = SubGroupPrinciplePolicy.new(current_user, Principle.new(sub_group: @sub_group)) + @sub_group_principle_policy = SubGroupPrinciplePolicy.new(current_user, @sub_group) + @sub_group_evaluation_policy = SubGroupEvaluationPolicy.new(current_user, @sub_group) end # GET /sub_groups/new diff --git a/app/helpers/sub_group_policy_helper.rb b/app/helpers/sub_group_policy_helper.rb new file mode 100644 index 00000000..b55f390b --- /dev/null +++ b/app/helpers/sub_group_policy_helper.rb @@ -0,0 +1,10 @@ +module SubGroupPolicyHelper + + private + + def admin_for_any_sub_group? + SubGroupMembership.joins(:sub_group, :membership).where('sub_groups.group_id': sub_group.group.id, + 'memberships.user_id': user.id, + admin: true).present? + end +end diff --git a/app/policies/sub_group_evaluation_policy.rb b/app/policies/sub_group_evaluation_policy.rb index 07f24e94..1d6737c5 100644 --- a/app/policies/sub_group_evaluation_policy.rb +++ b/app/policies/sub_group_evaluation_policy.rb @@ -1,15 +1,21 @@ # frozen_string_literal: true class SubGroupEvaluationPolicy < ApplicationPolicy + include SubGroupPolicyHelper + alias sub_group record def table? return false if off_season? - EvaluationPolicy.new(user, evaluation).table? || admin_of_the_sub_group? + EvaluationPolicy.new(user, evaluation).table? || admin_of_the_sub_group? || admin_for_any_sub_group? end - alias update_point_request? table? + def update_point_request? + return false if off_season? + + EvaluationPolicy.new(user, evaluation).edit? || admin_of_the_sub_group? + end def update_entry_request? EvaluationPolicy.new(user, evaluation).update_entry_request? diff --git a/app/policies/sub_group_principle_policy.rb b/app/policies/sub_group_principle_policy.rb index 44b9a0b9..2297161b 100644 --- a/app/policies/sub_group_principle_policy.rb +++ b/app/policies/sub_group_principle_policy.rb @@ -1,13 +1,19 @@ class SubGroupPrinciplePolicy < ApplicationPolicy - alias principle record + include SubGroupPolicyHelper + + alias sub_group record def index? + leader_of_the_group? || leader_assistant_of_the_group? || admin_of_the_sub_group? || admin_for_any_sub_group? + end + + def edit? leader_of_the_group? || leader_assistant_of_the_group? || admin_of_the_sub_group? end - alias create? index? - alias update? create? - alias destroy? create? + alias create? edit? + alias update? edit? + alias destroy? edit? def leader_of_the_group? membership.present? && membership.has_post?(PostType::LEADER_POST_ID) @@ -21,10 +27,6 @@ def admin_of_the_sub_group? sub_group_membership.present? && sub_group_membership.admin? end - def sub_group - @sub_group ||= principle.sub_group - end - def membership @membership ||= user.membership_for(sub_group.group) end diff --git a/app/views/sub_groups/show.html.erb b/app/views/sub_groups/show.html.erb index fe6f51a5..991a63ae 100644 --- a/app/views/sub_groups/show.html.erb +++ b/app/views/sub_groups/show.html.erb @@ -6,6 +6,17 @@
@@ -41,16 +52,5 @@ <% end %> |
---|