Skip to content

Commit

Permalink
Allow Mitglieder::Schreibrecht to edit mailing_lists in layer (#1396)
Browse files Browse the repository at this point in the history
* allow person with schreibrechte mailing lists in same group

* allow person with schreibrechte mailing lists in same group

* draft ability test for schreibrecht in mailing lists

* implement ability test for schreibrecht in mailing list

* fix rubocop issue

* allow member with schreibrecht to modify subgroups
  • Loading branch information
openscript authored Dec 18, 2024
1 parent abf4574 commit 5115e8d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/abilities/sac_cas/mailing_list_ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club.
# This file is part of hitobito_sac_cas and
# licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

module SacCas::MailingListAbility
extend ActiveSupport::Concern
include AbilityDsl::Constraints::Group

prepended do
on(MailingList) do
permission(:group_and_below_full)
.may(:show, :index_subscriptions, :create, :update, :destroy, :export_subscriptions)
.schreibrecht_in_main_group?
end
end

# Checks if a user has a Schreibrecht for the mailing list in the same main group
#
# For example if users are members of SAC Hitobito and have Schreibrecht, they
# can manage the mailing list of SAC Hitobito.
def schreibrecht_in_main_group?
user.roles.any? do |r|
r.is_a?(Group::SektionsMitglieder::Schreibrecht) && r.group.layer_group_id == group.layer_group_id
end
end
end
1 change: 1 addition & 0 deletions lib/hitobito_sac_cas/wagon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Wagon < Rails::Engine
Event::RoleAbility.prepend SacCas::Event::RoleAbility
Event::ParticipationAbility.prepend SacCas::Event::ParticipationAbility
GroupAbility.prepend SacCas::GroupAbility
MailingListAbility.prepend SacCas::MailingListAbility
PersonAbility.prepend SacCas::PersonAbility
PersonReadables.prepend SacCas::PersonReadables
QualificationAbility.include SacCas::QualificationAbility
Expand Down
53 changes: 53 additions & 0 deletions spec/abilities/sac_cas/mailing_list_ability_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas

require "spec_helper"

describe MailingListAbility do
let(:mitglied_with_schreibrecht) do
Fabricate(
Group::SektionsMitglieder::Schreibrecht.sti_name.to_sym,
group: groups(:bluemlisalp_mitglieder)
).person
end

let(:mitglied_without_schreibrecht) do
Fabricate(
Group::SektionsMitglieder::Mitglied.sti_name.to_sym,
group: groups(:bluemlisalp_mitglieder)
).person
end
let(:mailing_list) { Fabricate(:mailing_list, group: groups(:bluemlisalp)) }
let(:mailing_list_in_other_sub_group) { Fabricate(:mailing_list, group: groups(:bluemlisalp_funktionaere)) }
let(:mailing_list_in_foreign_group) { Fabricate(:mailing_list, group: groups(:matterhorn)) }

subject(:ability) { Ability.new(person.reload) }

context "mitglied with Schreibrecht" do
let(:person) { mitglied_with_schreibrecht }

it "is permitted to manage main group abos" do
expect(ability).to be_able_to(:create, mailing_list)
end

it "is permitted to manage sub group abos in same main group" do
expect(ability).to be_able_to(:create, mailing_list_in_other_sub_group)
end

it "is denied to manage foreign main group abos" do
expect(ability).not_to be_able_to(:create, mailing_list_in_foreign_group)
end
end

context "mitglied without Schreibrecht" do
let(:person) { mitglied_without_schreibrecht }

it "is denied to manage main group abos" do
expect(ability).not_to be_able_to(:create, mailing_list)
end
end
end

0 comments on commit 5115e8d

Please sign in to comment.