diff --git a/decidim-admin/app/commands/decidim/admin/create_share_token.rb b/decidim-admin/app/commands/decidim/admin/create_share_token.rb index 0d55b66b18fdf..6de1ebf80aa11 100644 --- a/decidim-admin/app/commands/decidim/admin/create_share_token.rb +++ b/decidim-admin/app/commands/decidim/admin/create_share_token.rb @@ -4,12 +4,44 @@ module Decidim module Admin # A command with all the business logic to create a taxonomy. # This command is called from the controller. - class CreateShareToken < Decidim::Commands::CreateResource - fetch_form_attributes :token, :expires_at, :registered_only, :organization, :user, :token_for + class CreateShareToken < Decidim::Command + # Public: Initializes the command. + # + # form - A form object with the params. + def initialize(form) + @form = form + end - protected + # Executes the command. Broadcasts these events: + # + # - :ok when everything is valid. + # - :invalid if the form wasn't valid and we couldn't proceed. + # + # Returns nothing. + def call + return broadcast(:invalid) if form.invalid? - def resource_class = Decidim::ShareToken + create_share_token + broadcast(:ok) + end + + private + + attr_reader :form + + def create_share_token + Decidim.traceability.create!( + ShareToken, + form.user, + { + token: form.token, + expires_at: form.expires_at, + token_for: form.token_for, + organization: form.organization, + user: form.user + } + ) + end end end end diff --git a/decidim-admin/app/views/decidim/admin/share_tokens/index.html.erb b/decidim-admin/app/views/decidim/admin/share_tokens/index.html.erb index e56b76f57cec8..316ebc3a31c2e 100644 --- a/decidim-admin/app/views/decidim/admin/share_tokens/index.html.erb +++ b/decidim-admin/app/views/decidim/admin/share_tokens/index.html.erb @@ -1,45 +1,58 @@ +<% add_decidim_page_title(t(".title", name: resource_title)) %>
-
-

+
+

<%= t(".title", name: resource_title) %> - <%= icon "add-line" %><%= t(".new_share_token_button") %> + <%= link_to t(".new_share_token_button"), share_tokens_path(:new), class: "button tiny button--title new" %>

-
-

<%= t(".share_tokens_help", clipboard: icon("clipboard-line", class: "inline-block mb-1")).html_safe %>

-
- <% if @share_tokens.any? %> -
- - - - - - - - - - - - <% @share_tokens.each do |share_token| %> +
+
+

<%= t(".share_tokens_help", clipboard: icon("clipboard", class: "inline-block mb-1")).html_safe %>

+
+ <% if @share_tokens.any? %> +
+
+
<%= sort_link(query, :token, t("models.share_token.fields.token", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :expires_at, t("models.share_token.fields.expires_at", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :registered_only, t("models.share_token.fields.registered_only", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :times_used, t("models.share_token.fields.times_used", scope: "decidim.admin"), default_order: :desc) %><%= t("models.share_token.fields.actions", scope: "decidim.admin") %>
+ - - - - - + + + + + - <% end %> - -
<%= share_token.token %><%= share_token.expires_at.present? ? ("#{l(share_token.expires_at, format: :decidim_short)}".html_safe) : content_tag(:em, t(".never")) %><%= t("booleans.#{share_token.registered_only.present?}") %><%= share_token.times_used %> - <%= icon_link_to "pencil-line", share_tokens_path(:edit, id: share_token ), t("actions.edit", scope: "decidim.admin.share_tokens"), class: "action-icon--edit" %> - <%= icon_link_to "clipboard-line", "#", t("actions.copy_link", scope: "decidim.admin.share_tokens"), class: "action-icon--copy", data: { "clipboard-copy" => "#js-token-#{share_token.id}", "clipboard-content" => share_token.url,"clipboard-copy-label" => t(".copied"),"clipboard-copy-message" => t(".copy_message") } %> - <%= icon_link_to "eye-line", share_token.url, t("actions.preview", scope: "decidim.admin.share_tokens"), class: "action-icon--preview", target: :blank %> - <%= icon_link_to "delete-bin-line", share_tokens_path(:destroy, id: share_token ), t("actions.destroy", scope: "decidim.admin.share_tokens"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin.share_tokens") } %> - <%= sort_link(query, :token, t("models.share_token.fields.token", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :expires_at, t("models.share_token.fields.expires_at", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :registered_only, t("models.share_token.fields.registered_only", scope: "decidim.admin"), default_order: :desc) %><%= sort_link(query, :times_used, t("models.share_token.fields.times_used", scope: "decidim.admin"), default_order: :desc) %><%= t("models.share_token.fields.actions", scope: "decidim.admin") %>
-
- <% else %> -

<%= t(".empty", new_token_link: link_to(t(".create_new_token"), share_tokens_path(:new) , class: "button button__text-secondary")).html_safe %>

- <% end %> + + + <% @share_tokens.each do |share_token| %> + + <%= share_token.token %> + <%= share_token.expires_at.present? ? ("#{l(share_token.expires_at, format: :decidim_short)}".html_safe) : content_tag(:em, t(".never")) %> + <%= t("booleans.#{share_token.registered_only.present?}") %> + <%= share_token.times_used %> + + <%= icon_link_to "pencil", share_tokens_path(:edit, id: share_token ), t("actions.edit", scope: "decidim.admin.share_tokens"), class: "action-icon--edit" %> + <%= icon_link_to "clipboard", + "#", + t("actions.copy_link", scope: "decidim.admin.share_tokens"), + class: "action-icon--copy", + data: { + "clipboard-copy" => "#js-token-#{share_token.id}", + "clipboard-content" => share_token.url, + "clipboard-copy-label" => t(".copied"), + "clipboard-copy-message" => t(".copy_message") } %> + <%= icon_link_to "eye", share_token.url, t("actions.preview", scope: "decidim.admin.share_tokens"), class: "action-icon--preview", target: :blank %> + <%= icon_link_to "trash", share_tokens_path(:destroy, id: share_token ), t("actions.destroy", scope: "decidim.admin.share_tokens"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin.share_tokens") } %> + + + <% end %> + + +

+
+ <% else %> +

<%= t(".empty", new_token_link: link_to(t(".create_new_token"), share_tokens_path(:new) , class: "")).html_safe %>

+ <% end %> + <%= decidim_paginate @share_tokens %> diff --git a/decidim-admin/spec/commands/decidim/admin/create_share_token_spec.rb b/decidim-admin/spec/commands/decidim/admin/create_share_token_spec.rb index e95b995460b39..18c2c83a96415 100644 --- a/decidim-admin/spec/commands/decidim/admin/create_share_token_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/create_share_token_spec.rb @@ -7,18 +7,18 @@ module Decidim::Admin subject { described_class.new(form) } let(:organization) { create(:organization) } - let(:current_user) { create(:user, :admin, organization:) } - let(:component) { create(:component, participatory_space: create(:participatory_process, organization:)) } + let(:current_user) { create(:user, :admin, organization: organization) } + let(:component) { create(:component, participatory_space: create(:participatory_process, organization: organization)) } let(:form) do ShareTokenForm.from_params( - token:, - expires_at:, - automatic_token:, - no_expiration:, - registered_only: + token: token, + expires_at: expires_at, + automatic_token: automatic_token, + no_expiration: no_expiration, + registered_only: registered_only ).with_context( - current_user:, + current_user: current_user, current_organization: organization, resource: component ) diff --git a/decidim-admin/spec/forms/share_token_form_spec.rb b/decidim-admin/spec/forms/share_token_form_spec.rb index 1bc0a0a4ae32a..472251fd12429 100644 --- a/decidim-admin/spec/forms/share_token_form_spec.rb +++ b/decidim-admin/spec/forms/share_token_form_spec.rb @@ -5,18 +5,18 @@ module Decidim::Admin describe ShareTokenForm do let(:organization) { create(:organization) } - let(:current_user) { create(:user, :admin, organization:) } - let(:component) { create(:component, participatory_space: create(:participatory_process, organization:)) } + let(:current_user) { create(:user, :admin, organization: organization) } + let(:component) { create(:component, participatory_space: create(:participatory_process, organization: organization)) } let(:form) do described_class.from_params( - token:, - automatic_token:, - expires_at:, - no_expiration:, - registered_only: + token: token, + automatic_token: automatic_token, + expires_at: expires_at, + no_expiration: no_expiration, + registered_only: registered_only ).with_context( - current_user:, + current_user: current_user, current_organization: organization, resource: component ) @@ -91,7 +91,7 @@ module Decidim::Admin context "when token exists" do let(:automatic_token) { false } - let!(:share_token) { create(:share_token, organization:, token_for: component, token:) } + let!(:share_token) { create(:share_token, organization: organization, token_for: component, token: token) } it "validates uniqueness of token" do expect(form).to be_invalid diff --git a/decidim-core/db/migrate/20240717093514_add_registered_only_to_decidim_share_tokens.rb b/decidim-core/db/migrate/20240717093514_add_registered_only_to_decidim_share_tokens.rb index 11417bf8e1b46..23dd0b57699ee 100644 --- a/decidim-core/db/migrate/20240717093514_add_registered_only_to_decidim_share_tokens.rb +++ b/decidim-core/db/migrate/20240717093514_add_registered_only_to_decidim_share_tokens.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class AddRegisteredOnlyToDecidimShareTokens < ActiveRecord::Migration[7.0] +class AddRegisteredOnlyToDecidimShareTokens < ActiveRecord::Migration[6.1] def change add_column :decidim_share_tokens, :registered_only, :boolean end