Sha256: 3f9eb4c15e8797be6e3c741d038ab90cfdf58b3a2ad0870e6a2e502fd641d902

Contents?: true

Size: 1.93 KB

Versions: 36

Compression:

Stored size: 1.93 KB

Contents

= Share tokens

Share tokens can be assigned to any model to provide a system to share unpublished resources with expirable and manageable 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.

== Add share tokens to a model

The model must `include Decidim::ShareableWithToken` and implement `shareable_url(share_token)`, which should return the public url for the resource you want to share, including the token as a query parameter.

[source,ruby]
----
# Public: Public URL for your_resource with given share token as query parameter
def shareable_url(share_token)
  your_resource_public_path(self, share_token: share_token.token)
end
----

== Set permissions

You should change permissions logic for the resource to check if there's a `share_token` query parameter in the request url, and call `Decidim::ShareToken.use!` to both check if the token is valid, and if it is, to _use it_ (which increments `times_used` variable and sets `last_used_at` to current time).

It should do something similar to this:

[source,ruby]
----
token = context[:share_token]

return unless token.present?

allow! if Decidim::ShareToken.use!(token_for: your_resource, token: token)
----

== Manage tokens

Render the partial `decidim-admin/app/views/decidim/admin/share_tokens/_share_tokens.html.erb` inside a view, with:

[source,ruby]
----
locals: { share_tokens: your_share_tokens_variable }
----

to let admins see and manage tokens for that resource.

== Link to url with token

Implement a `share` action (see below) in the resource controller (admin scope), redirecting to a url with a newly generated token, so you can call `share_my_resource_url`.

[source,ruby]
----
def share
  @your_resource = YourResource.find(params[:id]) # or whatever
  share_token = @your_resource.share_tokens.create!(user: current_user, organization: current_organization)

  redirect_to share_token.url
end
----

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
decidim-0.27.9 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.8 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.7 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.6 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.10 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.9 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.5 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.8 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.4 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.3 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.7 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.5 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.2 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.1 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.4 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.0 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.3 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.0.rc2 docs/modules/develop/pages/share_tokens.adoc
decidim-0.27.0.rc1 docs/modules/develop/pages/share_tokens.adoc
decidim-0.26.2 docs/modules/develop/pages/share_tokens.adoc