Shared links (#29)
* Create shared links UI * Show shared links in a list on settings page * Show dropdown for each shared link * Show icon actions for shared links * Log user in when they click non-password-protected shared link * Actually authenticate using a password * Delete shared linkspull/30/head
parent
39b5ea47f9
commit
5f62025176
@ -0,0 +1,30 @@
|
||||
defmodule Plausible.Site.SharedLink do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "shared_links" do
|
||||
belongs_to :site, Plausible.Site
|
||||
field :slug, :string
|
||||
field :password_hash, :string
|
||||
field :password, :string, virtual: true
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def changeset(link, attrs \\ %{}) do
|
||||
link
|
||||
|> cast(attrs, [:slug, :password])
|
||||
|> validate_required([:slug])
|
||||
|> unique_constraint(:slug)
|
||||
|> hash_password()
|
||||
end
|
||||
|
||||
defp hash_password(link) do
|
||||
case link.changes[:password] do
|
||||
nil -> link
|
||||
password ->
|
||||
hash = Plausible.Auth.Password.hash(password)
|
||||
change(link, password_hash: hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 3.4 KiB |
@ -0,0 +1,14 @@
|
||||
<%= form_for @changeset, "/sites/#{@site.domain}/shared-links", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2>New shared link</h2>
|
||||
<div class="my-6">
|
||||
Add a password or leave it blank so anyone with the link can see the stats.
|
||||
Once the link is created, we cannot reveal the password. Please make sure you save it in a secure place.
|
||||
</div>
|
||||
<div class="my-6">
|
||||
<%= label f, :password, "Password (optional)", class: "block text-sm font-bold" %>
|
||||
<%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %>
|
||||
<%= error_tag f, :password %>
|
||||
</div>
|
||||
|
||||
<%= submit "Create shared link", class: "button mt-4 w-full" %>
|
||||
<% end %>
|
||||
@ -0,0 +1,17 @@
|
||||
<%= form_for @conn, "/share/#{@link.slug}/authenticate", [class: "max-w-sm w-full mx-auto bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>
|
||||
<h2>Enter password</h2>
|
||||
<div class="my-6">
|
||||
This link is password-protected. Please enter the password to continue to the dashboard.
|
||||
</div>
|
||||
|
||||
<div class="my-6">
|
||||
<%= label f, :password, "Password", class: "block text-sm font-bold" %>
|
||||
<%= password_input f, :password, class: "transition mt-3 bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:bg-white focus:border-grey-light" %>
|
||||
|
||||
<%= if @conn.assigns[:error] do %>
|
||||
<div class="text-red text-xs italic mt-4"><%= @conn.assigns[:error] %></div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= submit "Continue", class: "button mt-4 w-full" %>
|
||||
<% end %>
|
||||
@ -0,0 +1,13 @@
|
||||
defmodule Plausible.Repo.Migrations.AddSharedLinks do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:shared_links) do
|
||||
add :site_id, references(:sites, on_delete: :delete_all), null: false
|
||||
add :slug, :string, null: false
|
||||
add :password_hash, :string
|
||||
|
||||
timestamps()
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue