defmodule VigilWeb.OrgSettingsLive.Groups do
@moduledoc """
Membership from identity-provider groups (`Vigil.Organizations.GroupSync`):
the rules, what happens when someone leaves a group, and a re-sync from
the groups stored at each member's last login.
"""
use VigilWeb, :live_view
import VigilWeb.OrgSettingsLive.Components
alias Vigil.Accounts.Scope
alias Vigil.Organizations
alias Vigil.Organizations.{GroupRule, GroupSync, Roles}
@impl true
def mount(_params, _session, socket) do
org = socket.assigns.current_scope.organization
{:ok,
socket
|> assign(:page_title, "#{org.name} · Groups")
|> assign(:teams, Organizations.list_teams(org))
|> assign(:providers, Enum.map(NeiamAuth.providers(), & &1.id))
|> assign(:form, new_form())
|> assign(:policy_form, to_form(Organizations.change_organization(org)))
|> stream(:rules, Organizations.list_group_rules(org))}
end
defp new_form, do: to_form(Organizations.change_group_rule(%GroupRule{}, %{role: :member}))
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash} current_scope={@current_scope} active={:org_settings}>
<.settings scope={@current_scope} tab={:groups}>
<p class="max-w-2xl text-sm text-base-content/70">
At each NEIAM login, members of these groups join {@current_scope.organization.name} with
the role given (the highest, if several match). Memberships added by hand are left alone.
Accounts themselves come from <code class="font-mono">OIDC_PROVISION_GROUPS</code>:
list the same groups there.
</p>
<ul
id="rules"
phx-update="stream"
class="divide-y divide-base-300 rounded-box border border-base-300"
>
<li id="rules-empty" class="hidden p-4 text-center text-sm text-base-content/60 only:block">
No group rules.
</li>
<li
:for={{dom_id, r} <- @streams.rules}
id={dom_id}
class="flex flex-wrap items-center gap-3 px-4 py-3"
>
<div class="min-w-0 flex-1">
<p class="font-mono text-sm">{r.group}</p>
<p class="text-xs text-base-content/60">
{if r.provider, do: "from #{r.provider}", else: "from any provider"}
<span :if={r.declared} class="badge badge-info badge-xs ml-1">from config</span>
</p>
</div>
<span class="badge badge-ghost">{Roles.label(r.role)}</span>
<button
:if={!r.declared and Scope.can?(@current_scope, "member:admin")}
type="button"
id={"delete-#{dom_id}"}
class="btn btn-ghost btn-xs text-error"
phx-click="delete"
phx-value-id={r.id}
>
Delete
</button>
</li>
</ul>
<%= if Scope.can?(@current_scope, "member:admin") do %>
<.form
for={@form}
id="rule-form"
phx-submit="create"
class="grid gap-x-3 rounded-box border border-base-300 bg-base-200/60 p-4 sm:grid-cols-4 sm:items-end"
>
<.input field={@form[:group]} label="Group" placeholder="neiam-admins" required />
<.input
field={@form[:role]}
type="select"
label="Role"
options={Enum.map(Roles.all(), &{Roles.label(&1), &1})}
/>
<.input
field={@form[:provider]}
type="select"
label="Provider"
options={[{"Any", ""} | Enum.map(@providers, &{&1, &1})]}
/>
<.button class="btn btn-primary mb-2" phx-disable-with="Adding…">Add rule</.button>
<div :if={@teams != []} class="sm:col-span-4">
<.input
field={@form[:team_ids]}
type="select"
multiple
label="Teams"
options={Enum.map(@teams, &{&1.name, &1.id})}
/>
</div>
</.form>
<.form for={@policy_form} id="policy-form" phx-submit="policy" class="max-w-xl space-y-2">
<.input
field={@policy_form[:group_removal]}
type="select"
label="When someone leaves every matching group"
options={[
{"Remove them from the organization", :remove},
{"Make them a member", :demote},
{"Keep their role", :keep}
]}
/>
<.input
field={@policy_form[:group_rules_promote_manual]}
type="checkbox"
label="Rules may raise (never lower) the role of members added by hand"
/>
<div class="flex gap-2">
<.button class="btn btn-primary btn-sm" phx-disable-with="Saving…">Save</.button>
<button type="button" id="resync" class="btn btn-ghost btn-sm" phx-click="resync">
<.icon name="hero-arrow-path-micro" class="size-4" /> Re-sync everyone now
</button>
</div>
</.form>
<% end %>
</.settings>
</Layouts.app>
"""
end
@impl true
def handle_event("create", %{"group_rule" => params}, socket) do
case Organizations.create_group_rule(socket.assigns.current_scope, params) do
{:ok, rule} ->
{:noreply,
socket
|> assign(:form, new_form())
|> stream_insert(:rules, rule)
|> put_flash(:info, "Rule added. It applies at each member's next login, or on re-sync.")}
{:error, %Ecto.Changeset{} = cs} ->
{:noreply, assign(socket, :form, to_form(cs))}
{:error, reason} ->
{:noreply, put_flash(socket, :error, forbidden_message(reason))}
end
end
def handle_event("delete", %{"id" => id}, socket) do
case Organizations.delete_group_rule(socket.assigns.current_scope, id) do
{:ok, rule} -> {:noreply, stream_delete(socket, :rules, rule)}
{:error, reason} -> {:noreply, put_flash(socket, :error, forbidden_message(reason))}
end
end
def handle_event("policy", %{"organization" => params}, socket) do
scope = socket.assigns.current_scope
with {:ok, org} <- Organizations.update_group_policy(scope, scope.organization, params) do
{:noreply,
socket
|> assign(:current_scope, %{scope | organization: org})
|> assign(:policy_form, to_form(Organizations.change_organization(org)))
|> put_flash(:info, "Saved.")}
else
{:error, %Ecto.Changeset{} = cs} -> {:noreply, assign(socket, :policy_form, to_form(cs))}
{:error, reason} -> {:noreply, put_flash(socket, :error, forbidden_message(reason))}
end
end
def handle_event("resync", _params, socket) do
case Organizations.authorize(socket.assigns.current_scope, "member:admin") do
:ok ->
n = GroupSync.resync_all()
{:noreply,
put_flash(socket, :info, "Re-synced #{n} #{if n == 1, do: "person", else: "people"}.")}
{:error, reason} ->
{:noreply, put_flash(socket, :error, forbidden_message(reason))}
end
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
7.1 KiB · text
History
573fef1