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 @@

Tagok

+ <% if @policy.edit? %> + <%= link_to 'Szerkesztés', edit_group_sub_group_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %> + <% end %> + <% if @sub_group_principle_policy.index? %> + <%= link_to 'Pontozási elvek', group_sub_group_principles_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %> + <% end %> + <% if @sub_group_evaluation_policy.table? %> + <%= link_to 'Pontozás', group_sub_group_evaluation_table_path(current_group, @sub_group, current_group.current_evaluation), class: 'uk-button uk-button-primary' %> + <% end %> +
+ <%= link_to 'Vissza', group_sub_groups_path(current_group), class: 'uk-button uk-margin-top' %> @@ -41,16 +52,5 @@ <% end %>
- - <% if @policy.edit? %> - <%= link_to 'Szerkesztés', edit_group_sub_group_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %> - <% end %> - <% if @sub_group_principle_policy.index? && - SystemAttribute.season.value != SystemAttribute::OFFSEASON && - current_group.current_evaluation&.semester == SystemAttribute.semester.to_s %> - <%= link_to 'Pontozási elvek', group_sub_group_principles_path(current_group, @sub_group), class: 'uk-button uk-button-primary' %> - <%= link_to 'Pontozás', group_sub_group_evaluation_table_path(current_group, @sub_group, current_group.current_evaluation), class: 'uk-button uk-button-primary' %> - <% end %> -
<%= link_to 'Vissza', group_sub_groups_path(current_group), class: 'uk-button uk-margin-top' %>
\ No newline at end of file diff --git a/spec/policies/sub_group_principle_policy_spec.rb b/spec/policies/sub_group_principle_policy_spec.rb index bb2a6d5f..a08409d6 100644 --- a/spec/policies/sub_group_principle_policy_spec.rb +++ b/spec/policies/sub_group_principle_policy_spec.rb @@ -1,12 +1,13 @@ require 'rails_helper' RSpec.describe SubGroupPrinciplePolicy, type: :policy do - subject { described_class.new(user, sub_group_principle) } + subject { described_class.new(user, sub_group) } let(:all_action) { [:index, :create, :update, :destroy] } - let(:sub_group_principle) { create(:sub_group_principle) } - let(:group) { sub_group_principle.sub_group.group } + let(:sub_group_principle) { create(:sub_group_principle)} + let(:sub_group) { sub_group_principle.sub_group} + let(:group) { sub_group.group } context 'when the user is the group leader' do let(:user) { group.leader.user }