Skip to content

Commit

Permalink
fix clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Jul 27, 2024
1 parent 0e50826 commit 6b1dddb
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 11 deletions.
2 changes: 1 addition & 1 deletion decidim-admin/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ en:
'true': 'Yes'
index:
back_to_share_tokens: Back to share tokens
copied: Copied!
copied: Share URL Copied!
copy_message: The text was successfully copied to clipboard.
create_new_token: Create your first token!
empty: There are no active tokens. %{new_token_link}
Expand Down
1 change: 1 addition & 0 deletions decidim-assemblies/app/models/decidim/assembly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Assembly < ApplicationRecord
include Decidim::TranslatableResource
include Decidim::HasArea
include Decidim::FilterableResource
include Decidim::ShareableWithToken

CREATED_BY = %w(city_council public others).freeze

Expand Down
1 change: 1 addition & 0 deletions decidim-conferences/app/models/decidim/conference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Conference < ApplicationRecord
include Decidim::HasUploadValidations
include Decidim::TranslatableResource
include Decidim::FilterableResource
include Decidim::ShareableWithToken

translatable_fields :title, :slogan, :short_description, :description, :objectives, :registration_terms

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Decidim
class HomepageController < Decidim::ApplicationController
skip_before_action :store_current_location

def show; end
end
end
4 changes: 2 additions & 2 deletions decidim-core/app/packs/src/decidim/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ $(() => {

const $input = $($el.data("clipboard-copy"));

let selectedText = $el.data("clipboard-content");
if (selectedText === "" && $input.length > 1 && $input.is("input, textarea, select")) {
let selectedText = $el.data("clipboard-content") || "";
if (selectedText === "" && $input.is("input, textarea, select")) {
selectedText = select($input[0]);
}

Expand Down
1 change: 1 addition & 0 deletions decidim-initiatives/app/models/decidim/initiative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Initiative < ApplicationRecord
include Decidim::HasResourcePermission
include Decidim::HasArea
include Decidim::FilterableResource
include Decidim::ShareableWithToken

translatable_fields :title, :description, :answer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ParticipatoryProcess < ApplicationRecord
include Decidim::TranslatableResource
include Decidim::HasArea
include Decidim::FilterableResource
include Decidim::ShareableWithToken

translatable_fields :title, :subtitle, :short_description, :description, :developer_group, :meta_scope, :local_area,
:target, :participatory_scope, :participatory_structure, :announcement
Expand Down
145 changes: 138 additions & 7 deletions docs/modules/develop/pages/share_tokens.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Share tokens

Share tokens can be assigned to any model to provide a system to share unpublished resources with expirable and manageable tokens.
Share tokens can be assigned to any model to provide a system to share unpublished resources with expiration dates through the creation/destruction of tokens.

A share token is created by a user with an expiration time, and can be added as a query param to access otherwise restricted locations.

Expand Down Expand Up @@ -31,23 +31,140 @@ return unless token.present?
allow! if Decidim::ShareToken.use!(token_for: your_resource, token: token)
----

Note that, if you are using a controller who is inheriting from `Decidim::ApplicationController`, you don't need to include the `:share_token` in the context when calling methods like `enforce_permission_to`, as it is already included in the `Decidim::NeedsPermissions` class through the method `store_share_token`.

Check failure on line 34 in docs/modules/develop/pages/share_tokens.adoc

View workflow job for this annotation

GitHub Actions / Lint code (./.github/run_spelling_check.sh)

Use "do not" instead of "don't".

== Manage tokens

Tokens can be managed in the components view similar as other resources (with and action icon like permissions for instance). By default, assemblies, conferences and participatory spaces are configured to have share tokens.
By default, participatory spaces like process, assemblies, conferences and initiatives are configured to have share tokens, as well as the individual components that are included in them. Participatory spaces have a "Share tokens" tab in the admin view, where you can create new tokens, see the list of existing ones, and revoke them.
Tokens can also be managed in the components view similarly as other resources to give pre-access (with and action icon like permissions for instance).

Tokens generated for a participatory space are valid for all the components included in it (regardless of their publication status), and tokens generated for a component are valid for that component only.

== Implementation for participatory spaces

In order to implement share tokens for a participatory spaces, you need to:

=== 1. Routes

Add the `share_tokens` CRUD routes in your `admin_engine.rb` file:

[source,ruby]
----
scope "/assemblies/:assembly_slug" do
...
resources :assembly_share_tokens, except: [:show], path: "share_tokens"
...
end
----

=== 2. Controller

Add the controller for the participatory space, it only requires to inherit from `Decidim::Admin::ShareTokensController` and define the `resource` method to return the participatory space:

[source,ruby]
----
# frozen_string_literal: true
module Decidim
module Assemblies
module Admin
# This controller allows sharing unpublished things.
# It is targeted for customizations for sharing unpublished things that lives under
# an assembly.
class AssemblyShareTokensController < Decidim::Admin::ShareTokensController
include Concerns::AssemblyAdmin
def resource
current_assembly
end
end
end
end
end
----

=== 3. Menu entry

Add the menu entry for the share tokens in the participatory space admin view. In Decidim we do this in the `menu.rb` file for each participatory space:

[source,ruby]
----
Decidim.menu :admin_assembly_menu do |menu|
...
menu.add_item :assembly_share_tokens,
I18n.t("menu.share_tokens", scope: "decidim.admin"),
decidim_admin_assemblies.assembly_share_tokens_path(current_participatory_space),
active: is_active_link?(decidim_admin_assemblies.assembly_share_tokens_path(current_participatory_space)),
icon_name: "share-line",
if: allowed_to?(:read, :share_tokens, current_participatory_space:)
...
end
----

=== 4. Model

In order to implement it in other participatory spaces, you should:
Ensure your participatory space model includes the `Decidim::ShareableWithToken` module and implements the `shareable_url` method:

1. Add the `share_tokens` CRUD routes in your `admin_engine.rb` file:
[source,ruby]
----
module Decidim
class Assembly < ApplicationRecord
...
include Decidim::ShareableWithToken
...
def shareable_url(share_token)
EngineRouter.main_proxy(self).assembly_url(self, share_token: share_token.token)
end
...
end
end
----

=== 5. Permissions

Add the permissions logic to the participatory space controller in the `permissions.rb` file:

For admin controllers:

[source,ruby]
----
allow! if permission_action.subject == :share_tokens
----

For frontend controllers:

[source,ruby]
----
token = context[:share_token]
return unless token.present?
allow! if Decidim::ShareToken.use!(token_for: current_assembly, token: token)
----

== Implementation for components

Components all inherit from `Decidim::Component`, so they already have the `Decidim::ShareableWithToken` module included. But you still need to do some steps:

=== 1. Routes

Add the `share_tokens` CRUD routes in your `admin_engine.rb` file:

[source,ruby]
----
scope "/assemblies/:assembly_slug" do
...
resources :components do
...
resources :share_tokens, except: [:show]
resources :component_share_tokens, except: [:show], path: "share_tokens", as: "share_tokens"
...
end
end
----

2. Add the controller for the participatory space, it only requires to inherit from `Decidim::Admin::ShareTokensController`:
=== 2. Controller

Add the controller for the component, it only requires to inherit from `Decidim::Admin::ShareTokensController` and define the `resource` method to return the component:

[source,ruby]
----
Expand All @@ -59,10 +176,24 @@ module Decidim
# This controller allows sharing unpublished things.
# It is targeted for customizations for sharing unpublished things that lives under
# an assembly.
class ShareTokensController < Decidim::Admin::ShareTokensController
class ComponentShareTokensController < Decidim::Admin::ShareTokensController
include Concerns::AssemblyAdmin
def resource
@resource ||= current_participatory_space.components.find(params[:component_id])
end
end
end
end
end
----

=== 3. Permissions

Similarly, add the same permissions logic to the component controller in the `permissions.rb` file as for participatory spaces.


== Other implementations

You can implement share tokens for any other model by following the same steps as for participatory spaces and components.
In that case, however, you might have to override some methods from the `Decidim::Admin::ShareTokensController` to adapt them to your model (check the source code for details).

0 comments on commit 6b1dddb

Please sign in to comment.