Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team management wip #4997

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/plausible/teams/invitations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ defmodule Plausible.Teams.Invitations do
end
end

def find_team_invitations(user) do
def find_team_invitations(%Teams.Team{} = team) do
Repo.all(
from ti in Teams.Invitation,
inner_join: inviter in assoc(ti, :inviter),
where: ti.team_id == ^team.id,
where: ti.role != :guest,
preload: [inviter: inviter]
)
end

def find_team_invitations(%Plausible.Auth.User{} = user) do
Repo.all(
from ti in Teams.Invitation,
inner_join: inviter in assoc(ti, :inviter),
Expand Down
12 changes: 12 additions & 0 deletions lib/plausible/teams/memberships.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ defmodule Plausible.Teams.Memberships do
alias Plausible.Repo
alias Plausible.Teams

def all_members(team) do
query =
from tm in Teams.Membership,
inner_join: u in assoc(tm, :user),
where: tm.team_id == ^team.id,
where: tm.role != :guest,
order_by: [asc: u.id],
preload: [user: u]

Repo.all(query)
end

def all_pending_site_transfers(email) do
email
|> pending_site_transfers_query()
Expand Down
9 changes: 6 additions & 3 deletions lib/plausible/teams/memberships/remove.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ defmodule Plausible.Teams.Memberships.Remove do

def remove(nil, _, _), do: {:error, :permission_denied}

def remove(team, user_id, current_user) do
def remove(team, user_id, current_user, opts \\ []) do
with {:ok, team_membership} <- Memberships.get_team_membership(team, user_id),
{:ok, current_user_role} <- Memberships.team_role(team, current_user),
:ok <- check_can_remove_membership(current_user_role, team_membership.role),
:ok <- check_owner_can_get_removed(team, team_membership.role) do
team_membership = Repo.preload(team_membership, [:team, :user])
Repo.delete!(team_membership)
send_team_member_removed_email(team_membership)

if Keyword.get(opts, :send_email?, true) do
send_team_member_removed_email(team_membership)
end

{:ok, team_membership}
end
Expand All @@ -35,7 +38,7 @@ defmodule Plausible.Teams.Memberships.Remove do

defp check_owner_can_get_removed(_team, _role), do: :ok

defp send_team_member_removed_email(team_membership) do
def send_team_member_removed_email(team_membership) do
team_membership
|> PlausibleWeb.Email.team_member_removed()
|> Plausible.Mailer.send()
Expand Down
2 changes: 1 addition & 1 deletion lib/plausible_web/components/generic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule PlausibleWeb.Components.Generic do
"border border-gray-300 dark:border-gray-500 text-red-700 bg-white dark:bg-gray-900 hover:text-red-500 dark:hover:text-red-400 focus:border-blue-300 dark:text-red-500 active:text-red-800"
}

@button_base_class "whitespace-nowrap truncate inline-flex items-center justify-center gap-x-2 font-medium rounded-md px-3.5 py-2.5 text-sm shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 disabled:bg-gray-400 dark:disabled:text-white dark:disabled:text-gray-400 dark:disabled:bg-gray-700"
@button_base_class "whitespace-nowrap truncate inline-flex items-center justify-center gap-x-2 font-medium rounded-md px-3 py-2 text-sm shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 disabled:bg-gray-400 dark:disabled:text-white dark:disabled:text-gray-400 dark:disabled:bg-gray-700"

attr(:type, :string, default: "button")
attr(:theme, :string, default: "primary")
Expand Down
3 changes: 2 additions & 1 deletion lib/plausible_web/controllers/settings_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule PlausibleWeb.SettingsController do

render(conn, :team_general,
team_name_changeset: name_changeset,
layout: {PlausibleWeb.LayoutView, :settings}
layout: {PlausibleWeb.LayoutView, :settings},
connect_live_socket: true
)
end

Expand Down
122 changes: 122 additions & 0 deletions lib/plausible_web/live/components/team.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
defmodule PlausibleWeb.Live.Components.Team do
use Phoenix.Component
import PlausibleWeb.Components.Generic
alias Plausible.Auth.User

attr :user, User, required: true
attr :label, :string, default: nil
attr :role, :atom, default: nil
attr :my_role, :atom, required: true
attr :disabled, :boolean, default: false
attr :remove_disabled, :boolean, default: false

def member(assigns) do
~H"""
<div class="member mt-4">
<div class="flex items-center gap-x-5">
<img src={User.profile_img_url(@user)} class="w-7 rounded-full" />
<span class="text-sm">
{@user.name}
<span
:if={@label}
class="ml-1 dark:bg-indigo-600 dark:text-gray-200 bg-gray-100 text-gray-500 text-xs px-1 rounded"
>
{@label}
</span>

<br /><span class="text-gray-500 text-xs">{@user.email}</span>
</span>
<div class="flex-1 text-right">
<.dropdown class="relative">
<:button class="role bg-transparent text-gray-800 dark:text-gray-100 hover:bg-gray-50 dark:hover:bg-gray-700 focus-visible:outline-gray-100 whitespace-nowrap truncate inline-flex items-center gap-x-2 font-medium rounded-md px-3.5 py-2.5 text-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 disabled:bg-gray-400 dark:disabled:text-white dark:disabled:text-gray-400 dark:disabled:bg-gray-700">
{@role |> to_string() |> String.capitalize()}
<Heroicons.chevron_down mini class="size-4 mt-0.5" />
</:button>
<:menu class="dropdown-items max-w-60">
<.role_item
phx-value-email={@user.email}
phx-value-name={@user.name}
role={:owner}
disabled={@disabled or @role == :owner}
phx-click="update-role"
>
Manage the team without restrictions
</.role_item>
<.role_item
phx-value-email={@user.email}
phx-value-name={@user.name}
role={:admin}
disabled={@disabled or @role == :admin}
phx-click="update-role"
>
Manage all team settings
</.role_item>
<.role_item
phx-value-email={@user.email}
phx-value-name={@user.name}
role={:editor}
disabled={@disabled or @role == :editor}
phx-click="update-role"
>
Create and view new sites
</.role_item>
<.role_item
phx-value-email={@user.email}
phx-value-name={@user.name}
role={:billing}
disabled={@disabled or @role == :billing}
phx-click="update-role"
>
Manage subscription
</.role_item>
<.role_item
phx-value-email={@user.email}
phx-value-name={@user.name}
role={:viewer}
disabled={@disabled or @role == :viewer}
phx-click="update-role"
>
View all sites under your team
</.role_item>
<.dropdown_divider />
<.dropdown_item
href="#"
disabled={@disabled or @remove_disabled}
phx-click="remove-member"
phx-value-email={@user.email}
phx-value-name={@user.name}
>
<div class={
not @remove_disabled &&
"text-red-600 hover:text-red-600 dark:text-red-500 hover:dark:text-red-400"
}>
Remove member
</div>
<div class="text-gray-500 dark:text-gray-400 text-xs/5">
Remove member from your team
</div>
</.dropdown_item>
</:menu>
</.dropdown>
</div>
</div>
</div>
"""
end

attr :role, :atom, required: true
attr :disabled, :boolean, default: false
slot :inner_block, required: true
attr :rest, :global

def role_item(assigns) do
~H"""
<.dropdown_item href="#" phx-value-role={@role} disabled={@disabled} {@rest}>
<div>{@role |> Atom.to_string() |> String.capitalize()}</div>
<div class="text-gray-500 dark:text-gray-400 text-xs/5">
{render_slot(@inner_block)}
</div>
</.dropdown_item>
"""
end
end
Loading
Loading