فهرست منبع

Hide some features when connection is not local

theenglishway (time) 4 سال پیش
والد
کامیت
74109aa615

+ 2 - 1
lib/vaccins_web/live/index.ex

@@ -5,12 +5,13 @@ defmodule VaccinsWeb.IndexLive do
   @refresh_period_ms 5 * 1000
 
   @impl true
-  def mount(_params, _session, socket) do
+  def mount(_params, %{"is_local?" => is_local?}, socket) do
     locations = LocationStore.get_locations()
 
     {:ok,
      socket
      |> assign(
+       is_local?: is_local?,
        locations: locations,
        locations_with_early_slots: MapSet.new(),
        locations_with_slots: MapSet.new(),

+ 2 - 2
lib/vaccins_web/live/index_live.html.leex

@@ -1,6 +1,6 @@
 <button phx-click="trigger_all">Trigger all</button>
 <button phx-click="reload_file">Reload file</button>
-<button phx-click="toggle_form">Display/hide form</button>
+<%= if @is_local? do %><button phx-click="toggle_form">Display/hide form</button><% end %>
 
 <%= if @display_cs do %>
   <%= f = form_for @location_cs, "#", [phx_submit: :add_location] %>
@@ -30,7 +30,7 @@
   </thead>
   <tbody>
     <%= for l <- assigns |> locations_by_availability do %>
-      <tr><%= live_component @socket, VaccinsWeb.LocationComponent, id: l.id, location: l, render_as: :table_row %></tr>
+      <tr><%= live_component @socket, VaccinsWeb.LocationComponent, id: l.id, location: l, render_as: :table_row, is_local?: @is_local? %></tr>
     <% end %>
   </tbody>
 </table>

+ 5 - 2
lib/vaccins_web/live/location_component.ex

@@ -13,7 +13,8 @@ defmodule VaccinsWeb.LocationComponent do
          loading: false,
          last_refresh_date: nil,
          last_early_slot_seen: nil,
-         render_as: :description_list
+         render_as: :description_list,
+         is_local?: false
        )}
 
   @impl true
@@ -120,7 +121,9 @@ defmodule VaccinsWeb.LocationComponent do
       <ul class="actions-list">
         <li><button phx-click="trigger_query" phx-target="<%= @myself %>">Trigger</button></li>
         <li><a href="<%= @location |> to_json_query %>"><button>Debug</button></a></li>
-        <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
+        <%= if @is_local? do %>
+          <li><button class="alert-danger" phx-click="delete" phx-target="<%= @myself %>" data-confirm="Etes-vous sur?">Delete</button></li>
+        <% end %>
       </ul>
     """
 

+ 8 - 0
lib/vaccins_web/plugs.ex

@@ -0,0 +1,8 @@
+defmodule VaccinsWeb.Plugs do
+  import Plug.Conn
+
+  def set_local_conn(conn = %{remote_ip: {ip1, _, _, _}}, opts) do
+    is_local? = ip1 in [192, 127]
+    conn |> assign(:is_local?, is_local?) |> put_session(:is_local?, is_local?)
+  end
+end

+ 2 - 0
lib/vaccins_web/router.ex

@@ -1,5 +1,6 @@
 defmodule VaccinsWeb.Router do
   use VaccinsWeb, :router
+  import VaccinsWeb.Plugs
 
   pipeline :browser do
     plug :accepts, ["html"]
@@ -8,6 +9,7 @@ defmodule VaccinsWeb.Router do
     plug :put_root_layout, {VaccinsWeb.LayoutView, :root}
     plug :protect_from_forgery
     plug :put_secure_browser_headers
+    plug :set_local_conn
   end
 
   pipeline :api do