diff --git a/app/forms/decidim/decidim_awesome/admin/menu_form.rb b/app/forms/decidim/decidim_awesome/admin/menu_form.rb index 5013ae29e..8f1660b45 100644 --- a/app/forms/decidim/decidim_awesome/admin/menu_form.rb +++ b/app/forms/decidim/decidim_awesome/admin/menu_form.rb @@ -5,7 +5,7 @@ module DecidimAwesome module Admin class MenuForm < Decidim::Form include Decidim::TranslatableAttributes - VISIBILITY_STATES = %w(default hidden logged non_logged).freeze + VISIBILITY_STATES = %w(default hidden logged non_logged verified_user).freeze translatable_attribute :raw_label, String attribute :url, String diff --git a/config/locales/en.yml b/config/locales/en.yml index faa6a3d56..32f4a5124 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -342,6 +342,7 @@ en: hidden: Always hidden logged: Only visible for logged users non_logged: Only visible for non-logged users + verified_user: Only visible for users with a valid authorization index: confirm_destroy: Are you sure to remove this customization? edit: Edit diff --git a/lib/decidim/decidim_awesome/menu_hacker.rb b/lib/decidim/decidim_awesome/menu_hacker.rb index 22195681c..6c4b0f2d9 100644 --- a/lib/decidim/decidim_awesome/menu_hacker.rb +++ b/lib/decidim/decidim_awesome/menu_hacker.rb @@ -78,6 +78,10 @@ def visible?(item) user.present? when "non_logged" user.blank? + when "verified_user" + # the cleaner version should be user.authorizations.any? + # but there is not relationship between users and authorizations + Decidim::Authorization.where(user: user).any? { |auth| auth.granted? && !auth.expired? } else true end diff --git a/spec/system/menu_hacks_spec.rb b/spec/system/menu_hacks_spec.rb index 6a77b6d2c..f8be9df8d 100644 --- a/spec/system/menu_hacks_spec.rb +++ b/spec/system/menu_hacks_spec.rb @@ -181,96 +181,135 @@ end end end - end - end - describe "active" do - let!(:component) { create(:proposal_component, participatory_space: participatory_process) } - let!(:participatory_process2) { create :participatory_process, organization: organization } - let!(:component2) { create(:proposal_component, participatory_space: participatory_process2) } - let(:menu) do - [ - { - url: "/", - label: { - "en" => "A new beggining" - }, - position: 1 - }, - { - url: "/processes/#{participatory_process.slug}", - label: { - "en" => "A single process" - }, - position: 2 - } - ] - end + context "when only verified user", with_authorization_workflows: ["dummy_authorization_handler"] do + let!(:authorization) { create(:authorization, granted_at: Time.zone.now, user: user, name: "dummy_authorization_handler") } + let(:visibility) { "verified_user" } - it_behaves_like "has active link", "A new beggining" + before do + switch_to_host(organization.host) + login_as user, scope: :user + visit decidim.root_path + end - context "when visiting all processes list" do - before do - visit decidim_participatory_processes.participatory_processes_path - end + context "when user is verified" do + it "shows the item" do + within ".main-nav" do + expect(page).to have_content("A new beggining") + end + end + end - it_behaves_like "has active link", "Processes" - end + context "when user is not verified" do + let(:authorization) { nil } - context "when visiting a process in a custom link" do - before do - visit decidim_participatory_processes.participatory_process_path(participatory_process.slug) - end + it "shows the item" do + within ".main-nav" do + expect(page).not_to have_content("A new beggining") + end + end + end - it_behaves_like "has active link", "A single process" - end + context "when verification is expired" do + let!(:authorization) { create(:authorization, granted_at: 3.months.ago, user: user, name: "dummy_authorization_handler") } - context "when visiting a sublink of a process in a custom link" do - before do - visit main_component_path(component) + it "shows the item" do + within ".main-nav" do + expect(page).not_to have_content("A new beggining") + end + end + end end - it_behaves_like "has active link", "A single process" - end + describe "active" do + let!(:component) { create(:proposal_component, participatory_space: participatory_process) } + let!(:participatory_process2) { create :participatory_process, organization: organization } + let!(:component2) { create(:proposal_component, participatory_space: participatory_process2) } + let(:menu) do + [ + { + url: "/", + label: { + "en" => "A new beggining" + }, + position: 1 + }, + { + url: "/processes/#{participatory_process.slug}", + label: { + "en" => "A single process" + }, + position: 2 + } + ] + end - context "when visiting a process not in a custom link" do - before do - visit decidim_participatory_processes.participatory_process_path(participatory_process2.slug) - end + it_behaves_like "has active link", "A new beggining" - it_behaves_like "has active link", "Processes" - end + context "when visiting all processes list" do + before do + visit decidim_participatory_processes.participatory_processes_path + end - context "when visiting a sublink of a process not in a custom link" do - before do - visit main_component_path(component2) - end + it_behaves_like "has active link", "Processes" + end - it_behaves_like "has active link", "Processes" - end - end + context "when visiting a process in a custom link" do + before do + visit decidim_participatory_processes.participatory_process_path(participatory_process.slug) + end - context "when menu is :disabled" do - let(:disabled_features) { [:menu] } + it_behaves_like "has active link", "A single process" + end - it "renders the normal menu" do - within ".main-nav" do - expect(page).to have_content("Home") - expect(page).to have_content("Processes") - expect(page).to have_content("Help") - expect(page).not_to have_content("A new beggining") - expect(page).not_to have_content("Blog") - end - end + context "when visiting a sublink of a process in a custom link" do + before do + visit main_component_path(component) + end - it_behaves_like "has active link", "Home" + it_behaves_like "has active link", "A single process" + end - context "when visiting another page" do - before do - visit decidim_participatory_processes.participatory_processes_path + context "when visiting a process not in a custom link" do + before do + visit decidim_participatory_processes.participatory_process_path(participatory_process2.slug) + end + + it_behaves_like "has active link", "Processes" + end + + context "when visiting a sublink of a process not in a custom link" do + before do + visit main_component_path(component2) + end + + it_behaves_like "has active link", "Processes" + end end - it_behaves_like "has active link", "Processes" + context "when menu is :disabled" do + let(:disabled_features) { [:menu] } + + it "renders the normal menu" do + within ".main-nav" do + expect(page).to have_content("Home") + expect(page).to have_content("Processes") + expect(page).to have_content("Help") + expect(page).not_to have_content("A new beggining") + expect(page).not_to have_content("Blog") + end + end + + it_behaves_like "has active link", "Home" + + context "when visiting another page" do + before do + visit decidim_participatory_processes.participatory_processes_path + end + + it_behaves_like "has active link", "Processes" + end + end end end end