feat(permissions): add a membership picker

The member role has been in the API and the docs since 1.0, but neither
console ever offered it — the only way to set one was to know the syntax
and type it into the custom permission box.

It keys on a membership ID, which nothing in the console surfaces, so
the picker resolves it in two steps: find the user, then choose which of
their team memberships to grant. It has to run in that direction —
teams.listMemberships indexes only the membership ID and the user ID for
search, so picking a team first would leave the member step with a
search box that matches nothing. users.list indexes name, email and
phone, so the first step behaves like the existing user picker.
This commit is contained in:
harsh mahajan committed 2026-08-20 22:32:02 +05:30
1 parent 9baccd3407
commit 085f840277
4 files changed
+300

No files matched your search

@@ -2,6 +2,7 @@
import { createEventDispatcher } from 'svelte';
import Label from './label.svelte';
import Custom from './custom.svelte';
import Member from './member.svelte';
import Team from './team.svelte';
import User from './user.svelte';
import type { Permission } from './permissions.svelte';
@@ -10,6 +11,7 @@
export let showUser: boolean;
export let showTeam: boolean;
export let showMember: boolean;
export let showLabel: boolean;
export let showCustom: boolean;
export let groups: Writable<Map<string, Permission>>;
@@ -55,6 +57,11 @@
showTeam = true;
hide(e);
}}>Select teams</ActionMenu.Item.Button>
<ActionMenu.Item.Button
on:click={(e) => {
showMember = true;
hide(e);
}}>Select memberships</ActionMenu.Item.Button>
<ActionMenu.Item.Button
on:click={(e) => {
showLabel = true;
@@ -82,6 +89,9 @@
}}
{groups} />
{/if}
{#if showMember}
<Member bind:show={showMember} on:create {groups} />
{/if}
{#if showLabel}
<Label bind:show={showLabel} on:create {groups} />
{/if}
@@ -0,0 +1,284 @@
<script lang="ts">
import { Button, InputSearch } from '$lib/elements/forms';
import { createEventDispatcher } from 'svelte';
import { AvatarInitials, EmptySearch, Modal, PaginationInline } from '..';
import { sdk } from '$lib/stores/sdk';
import { Query, Role, type Models } from '@appwrite.io/console';
import type { Writable } from 'svelte/store';
import type { Permission } from './permissions.svelte';
import {
Avatar,
Card,
Empty,
Icon,
Layout,
Link,
Selector,
Spinner,
Table,
Typography
} from '@appwrite.io/pink-svelte';
import {
IconAnonymous,
IconArrowNarrowLeft,
IconMinusSm,
IconUsers
} from '@appwrite.io/pink-icons-svelte';
import { page } from '$app/state';
export let show: boolean;
export let groups: Writable<Map<string, Permission>>;
const dispatch = createEventDispatcher();
let search = '';
let offset = 0;
let isLoading = false;
let hasSelection = false;
let selected: Set<string> = new Set();
let users: Models.UserList<Record<string, unknown>>;
// A member role keys on a membership rather than a user, and memberships are searchable
// only by their own ID, so the user is resolved first and their memberships listed after.
let user: Models.User<Record<string, unknown>> = null;
let memberships: Models.MembershipList;
let membershipOffset = 0;
function clearUser() {
user = null;
memberships = undefined;
membershipOffset = 0;
}
function reset() {
offset = 0;
search = '';
selected.clear();
clearUser();
show = false;
}
function create() {
dispatch('create', Array.from(selected));
reset();
}
async function requestUsers() {
if (!show || user) return;
isLoading = true;
users = await sdk.forProject(page.params.region, page.params.project).users.list({
queries: [Query.limit(5), Query.offset(offset)],
search: search || undefined
});
isLoading = false;
}
async function requestMemberships() {
if (!show || !user) return;
isLoading = true;
memberships = await sdk
.forProject(page.params.region, page.params.project)
.users.listMemberships({
userId: user.$id,
queries: [Query.limit(5), Query.offset(membershipOffset)]
});
isLoading = false;
}
function onSelection(role: string) {
const checked = selected.has(role);
if (checked) {
selected.delete(role);
} else {
selected.add(role);
}
selected = selected;
hasSelection = selected.size > 0;
}
$: if (show) {
requestUsers();
}
$: if (offset !== null) {
requestUsers();
}
$: if (search !== null) {
offset = 0;
requestUsers();
}
$: if (user) {
requestMemberships();
}
$: if (membershipOffset !== null) {
requestMemberships();
}
</script>
<Modal title="Select memberships" bind:show onSubmit={create} on:close={reset}>
<Typography.Text slot="description">
Grant access to a user through one of their team memberships. Access is revoked
automatically when they leave that team.
</Typography.Text>
{#if user}
<Layout.Stack direction="row" alignItems="center" gap="s">
<Button text size="s" on:click={clearUser}>
<Icon icon={IconArrowNarrowLeft} size="s" />
Back
</Button>
<Typography.Caption variant="400" color="--fgcolor-neutral-tertiary">
{user.name || user.email || user.phone || user.$id}
</Typography.Caption>
</Layout.Stack>
{#if memberships?.memberships?.length}
<Table.Root columns={[{ id: 'checkbox', width: 40 }, { id: 'team' }]} let:root>
{#each memberships.memberships as membership (membership.$id)}
{@const role = Role.member(membership.$id)}
{@const exists = $groups.has(role)}
<Table.Row.Button {root} on:click={() => onSelection(role)} disabled={exists}>
<Table.Cell column="checkbox" {root}>
<div style:pointer-events="none">
<Selector.Checkbox
size="s"
id={membership.$id}
disabled={exists}
checked={exists || selected.has(role)} />
</div>
</Table.Cell>
<Table.Cell column="team" {root}>
<Layout.Stack direction="row" alignItems="center" gap="s">
<Avatar size="xs">
<Icon icon={IconUsers} size="s" />
</Avatar>
<Layout.Stack gap="none">
<Typography.Caption variant="400">
{membership.teamName || membership.teamId}
</Typography.Caption>
<Typography.Caption
variant="400"
color="--fgcolor-neutral-tertiary">
{membership.$id}
</Typography.Caption>
</Layout.Stack>
</Layout.Stack>
</Table.Cell>
</Table.Row.Button>
{/each}
</Table.Root>
<Layout.Stack direction="row" justifyContent="space-between" alignItems="center">
<p class="text">Total results: {memberships?.total}</p>
<PaginationInline
limit={5}
bind:offset={membershipOffset}
total={memberships?.total}
hidePages
on:change={requestMemberships} />
</Layout.Stack>
{:else if isLoading}
<!-- 275px nearly matches the height of at-least 5 items in the table above -->
<div style:margin-inline="auto" style:min-height="275px" style:align-content="center">
<Spinner size="m" />
</div>
{:else}
<Card.Base padding="none">
<Empty title="This user is not a member of any team." type="secondary">
<Typography.Text slot="description">
Add them to a team to grant access this way, or learn more in our <Link.Anchor
href="https://appwrite.io/docs/products/auth/teams"
target="_blank"
rel="noopener noreferrer">
documentation</Link.Anchor
>.
</Typography.Text>
</Empty>
</Card.Base>
{/if}
{:else if users?.users?.length}
<InputSearch
autofocus
placeholder="Search by name, email, phone or ID"
bind:value={search} />
<Table.Root columns={[{ id: 'user' }]} let:root>
{#each users.users as item (item.$id)}
<Table.Row.Button {root} on:click={() => (user = item)}>
<Table.Cell column="user" {root}>
<Layout.Stack direction="row" alignItems="center" gap="s">
{#if item.name}
<AvatarInitials size="xs" name={item.name} />
{:else}
<Avatar size="xs">
<Icon
icon={item.email || item.phone
? IconMinusSm
: IconAnonymous}
size="s" />
</Avatar>
{/if}
<Layout.Stack gap="none">
<Typography.Caption variant="400">
{item.name || item.email || item.phone || '-'}
</Typography.Caption>
<Typography.Caption
variant="400"
color="--fgcolor-neutral-tertiary">
{item.$id}
</Typography.Caption>
</Layout.Stack>
</Layout.Stack>
</Table.Cell>
</Table.Row.Button>
{/each}
</Table.Root>
<Layout.Stack direction="row" justifyContent="space-between" alignItems="center">
<p class="text">Total results: {users?.total}</p>
<PaginationInline
limit={5}
bind:offset
total={users?.total}
hidePages
on:change={requestUsers} />
</Layout.Stack>
{:else if search}
<InputSearch
autofocus
placeholder="Search by name, email, phone or ID"
bind:value={search} />
<EmptySearch bind:search target="users" hidePages>
<Button
external
href="https://appwrite.io/docs/products/auth/users"
text
event="empty_documentation"
size="s">Documentation</Button>
<Button secondary on:click={() => (search = '')}>Clear search</Button>
</EmptySearch>
{:else if isLoading}
<!-- 275px nearly matches the height of at-least 5 items in the table above -->
<div style:margin-inline="auto" style:min-height="275px" style:align-content="center">
<Spinner size="m" />
</div>
{:else}
<Card.Base padding="none">
<Empty title="No users yet. Create a user to see it here." type="secondary">
<Typography.Text slot="description">
Need a hand? Learn more in our <Link.Anchor
href="https://appwrite.io/docs/products/auth/users"
target="_blank"
rel="noopener noreferrer">
documentation</Link.Anchor
>.
</Typography.Text>
</Empty>
</Card.Base>
{/if}
<svelte:fragment slot="footer">
<Button submit disabled={!hasSelection}>Add</Button>
</svelte:fragment>
</Modal>
@@ -27,6 +27,7 @@
let showUser = false;
let showTeam = false;
let showMember = false;
let showLabel = false;
let showCustom = false;
@@ -182,6 +183,7 @@
bind:showLabel
bind:showCustom
bind:showTeam
bind:showMember
bind:showUser
{groups}
on:create={create}
@@ -199,6 +201,7 @@
bind:showLabel
bind:showCustom
bind:showTeam
bind:showMember
bind:showUser
{groups}
on:create={create}
@@ -14,6 +14,7 @@
let showUser = false;
let showTeam = false;
let showMember = false;
let showLabel = false;
let showCustom = false;
@@ -102,6 +103,7 @@
bind:showLabel
bind:showCustom
bind:showTeam
bind:showMember
bind:showUser
{groups}
on:create={create}
@@ -121,6 +123,7 @@
bind:showLabel
bind:showCustom
bind:showTeam
bind:showMember
bind:showUser
{groups}
on:create={create}