Add workspace groups with space grants
Workspace admins create groups of members, and space admins grant a role to a whole group. A member's space role is now the highest of their own grant and their groups' grants. Leaving a workspace drops its group memberships, and deleting a group removes its space grants (they are not a real relation, so PocketBase cannot cascade them). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
342d073460
commit
34660c6b46
21 files changed
+1667
-7
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
/// <reference path="../pb_data/types.d.ts" />
|
||||
|
||||
// Groups of workspace members. A space can grant a role to a group
|
||||
// (space_members.principal_type = "group"); members get the highest of their
|
||||
// own and their groups' roles.
|
||||
|
||||
migrate(
|
||||
(app) => {
|
||||
const users = app.findCollectionByNameOrId("users");
|
||||
const workspaces = app.findCollectionByNameOrId("workspaces");
|
||||
|
||||
const groups = new Collection({
|
||||
type: "base",
|
||||
name: "groups",
|
||||
fields: [
|
||||
{ name: "workspace", type: "relation", collectionId: workspaces.id, maxSelect: 1, required: true, cascadeDelete: true },
|
||||
{ name: "name", type: "text", required: true, max: 120 },
|
||||
{ name: "description", type: "text", max: 500 },
|
||||
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
||||
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
||||
],
|
||||
indexes: ["CREATE UNIQUE INDEX idx_groups_name ON groups (workspace, name COLLATE NOCASE)"],
|
||||
});
|
||||
app.save(groups);
|
||||
|
||||
const members = new Collection({
|
||||
type: "base",
|
||||
name: "group_members",
|
||||
fields: [
|
||||
{ name: "group", type: "relation", collectionId: groups.id, maxSelect: 1, required: true, cascadeDelete: true },
|
||||
{ name: "user", type: "relation", collectionId: users.id, maxSelect: 1, required: true, cascadeDelete: true },
|
||||
{ name: "created", type: "autodate", onCreate: true, onUpdate: false },
|
||||
{ name: "updated", type: "autodate", onCreate: true, onUpdate: true },
|
||||
],
|
||||
indexes: [
|
||||
"CREATE UNIQUE INDEX idx_group_members_unique ON group_members (`group`, user)",
|
||||
"CREATE INDEX idx_group_members_user ON group_members (user)",
|
||||
],
|
||||
});
|
||||
app.save(members);
|
||||
},
|
||||
(app) => {
|
||||
for (const name of ["group_members", "groups"]) {
|
||||
try {
|
||||
app.delete(app.findCollectionByNameOrId(name));
|
||||
} catch (_) {}
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -33,6 +33,10 @@ export type PublicSettings = Schemas["PublicSettings"];
|
||||
export type InstanceSettings = Schemas["InstanceSettings"];
|
||||
export type InstanceSettingsUpdate = Schemas["InstanceSettingsUpdate"];
|
||||
export type Invite = Schemas["Invite"];
|
||||
export type Group = Schemas["Group"];
|
||||
export type GroupRequest = Schemas["GroupRequest"];
|
||||
export type SpaceGroup = Schemas["SpaceGroup"];
|
||||
export type MemberUser = Schemas["MemberUser"];
|
||||
export type InviteInfo = Schemas["InviteInfo"];
|
||||
export type CreateInviteRequest = Schemas["CreateInviteRequest"];
|
||||
export type CreateSpaceRequest = { name: string; description?: string; icon?: string; visibility?: SpaceVisibility };
|
||||
@@ -175,6 +179,20 @@ export function createClient(opts: ClientOptions) {
|
||||
spaces: (id: string) => request<Space[]>("GET", `/api/v1/workspaces/${encodeURIComponent(id)}/spaces`),
|
||||
createSpace: (id: string, input: CreateSpaceRequest) =>
|
||||
post<Space>(`/api/v1/workspaces/${encodeURIComponent(id)}/spaces`, input),
|
||||
groups: (id: string) => request<Group[]>("GET", `/api/v1/workspaces/${encodeURIComponent(id)}/groups`),
|
||||
createGroup: (id: string, input: GroupRequest) =>
|
||||
post<Group>(`/api/v1/workspaces/${encodeURIComponent(id)}/groups`, input),
|
||||
},
|
||||
|
||||
groups: {
|
||||
update: (id: string, patch: GroupRequest) =>
|
||||
request<Group>("PATCH", `/api/v1/groups/${encodeURIComponent(id)}`, patch),
|
||||
remove: (id: string) => request<void>("DELETE", `/api/v1/groups/${encodeURIComponent(id)}`),
|
||||
members: (id: string) => request<MemberUser[]>("GET", `/api/v1/groups/${encodeURIComponent(id)}/members`),
|
||||
addMember: (id: string, email: string) =>
|
||||
request<MemberUser>("PUT", `/api/v1/groups/${encodeURIComponent(id)}/members`, { email }),
|
||||
removeMember: (id: string, userId: string) =>
|
||||
request<void>("DELETE", `/api/v1/groups/${encodeURIComponent(id)}/members/${encodeURIComponent(userId)}`),
|
||||
},
|
||||
|
||||
spaces: {
|
||||
@@ -187,6 +205,11 @@ export function createClient(opts: ClientOptions) {
|
||||
request<SpaceMember>("PUT", `/api/v1/spaces/${encodeURIComponent(id)}/members`, { email, role }),
|
||||
removeMember: (id: string, userId: string) =>
|
||||
request<void>("DELETE", `/api/v1/spaces/${encodeURIComponent(id)}/members/${encodeURIComponent(userId)}`),
|
||||
groups: (id: string) => request<SpaceGroup[]>("GET", `/api/v1/spaces/${encodeURIComponent(id)}/groups`),
|
||||
setGroup: (id: string, group: string, role: SpaceRole) =>
|
||||
request<SpaceGroup>("PUT", `/api/v1/spaces/${encodeURIComponent(id)}/groups`, { group, role }),
|
||||
removeGroup: (id: string, groupId: string) =>
|
||||
request<void>("DELETE", `/api/v1/spaces/${encodeURIComponent(id)}/groups/${encodeURIComponent(groupId)}`),
|
||||
pages: (id: string) => request<PageNode[]>("GET", `/api/v1/spaces/${encodeURIComponent(id)}/pages`),
|
||||
createPage: (id: string, input: { parent?: string; title?: string; index?: number } = {}) =>
|
||||
post<Page>(`/api/v1/spaces/${encodeURIComponent(id)}/pages`, input),
|
||||
|
||||
Vendored
+401
@@ -846,6 +846,126 @@ export interface paths {
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/workspaces/{id}/groups": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
/** A workspace's groups (members and above) */
|
||||
get: operations["listGroups"];
|
||||
put?: never;
|
||||
/** Create a group (workspace admin) */
|
||||
post: operations["createGroup"];
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/groups/{id}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put?: never;
|
||||
post?: never;
|
||||
/** Delete a group and its space grants (workspace admin) */
|
||||
delete: operations["deleteGroup"];
|
||||
options?: never;
|
||||
head?: never;
|
||||
/** Rename a group or change its description (workspace admin) */
|
||||
patch: operations["updateGroup"];
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/groups/{id}/members": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
/** A group's members */
|
||||
get: operations["listGroupMembers"];
|
||||
/** Add a workspace member to a group by email (workspace admin) */
|
||||
put: operations["addGroupMember"];
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/groups/{id}/members/{userId}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
userId: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put?: never;
|
||||
post?: never;
|
||||
/** Remove someone from a group (workspace admin) */
|
||||
delete: operations["removeGroupMember"];
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/spaces/{id}/groups": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
/** Groups with a role in the space */
|
||||
get: operations["listSpaceGroups"];
|
||||
/** Grant a group a role in the space (space admin) */
|
||||
put: operations["setSpaceGroup"];
|
||||
post?: never;
|
||||
delete?: never;
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
"/api/v1/spaces/{id}/groups/{groupId}": {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
groupId: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
get?: never;
|
||||
put?: never;
|
||||
post?: never;
|
||||
/** Take a group's role in the space away (space admin) */
|
||||
delete: operations["removeSpaceGroup"];
|
||||
options?: never;
|
||||
head?: never;
|
||||
patch?: never;
|
||||
trace?: never;
|
||||
};
|
||||
}
|
||||
export type webhooks = Record<string, never>;
|
||||
export interface components {
|
||||
@@ -1163,6 +1283,30 @@ export interface components {
|
||||
/** @description Sign in to accept instead of signing up */
|
||||
hasAccount: boolean;
|
||||
};
|
||||
MemberUser: {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
};
|
||||
Group: {
|
||||
id: string;
|
||||
workspace: string;
|
||||
name: string;
|
||||
description: string;
|
||||
members: number;
|
||||
created: string;
|
||||
};
|
||||
GroupRequest: {
|
||||
name?: string;
|
||||
description?: string;
|
||||
};
|
||||
SpaceGroup: {
|
||||
group: {
|
||||
id: string;
|
||||
name: string;
|
||||
};
|
||||
role: components["schemas"]["SpaceRole"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description Signed in */
|
||||
@@ -2717,4 +2861,261 @@ export interface operations {
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
listGroups: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["Group"][];
|
||||
};
|
||||
};
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
createGroup: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["GroupRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description Created */
|
||||
201: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["Group"];
|
||||
};
|
||||
};
|
||||
400: components["responses"]["Error"];
|
||||
403: components["responses"]["Error"];
|
||||
409: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
deleteGroup: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description Deleted */
|
||||
204: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
updateGroup: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["GroupRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["Group"];
|
||||
};
|
||||
};
|
||||
400: components["responses"]["Error"];
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
listGroupMembers: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["MemberUser"][];
|
||||
};
|
||||
};
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
addGroupMember: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": components["schemas"]["EmailRequest"];
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["MemberUser"];
|
||||
};
|
||||
};
|
||||
400: components["responses"]["Error"];
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
removeGroupMember: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
userId: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description Removed */
|
||||
204: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
listSpaceGroups: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SpaceGroup"][];
|
||||
};
|
||||
};
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
setSpaceGroup: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody: {
|
||||
content: {
|
||||
"application/json": {
|
||||
group: string;
|
||||
role: components["schemas"]["SpaceRole"];
|
||||
};
|
||||
};
|
||||
};
|
||||
responses: {
|
||||
/** @description OK */
|
||||
200: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content: {
|
||||
"application/json": components["schemas"]["SpaceGroup"];
|
||||
};
|
||||
};
|
||||
400: components["responses"]["Error"];
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
removeSpaceGroup: {
|
||||
parameters: {
|
||||
query?: never;
|
||||
header?: never;
|
||||
path: {
|
||||
id: string;
|
||||
groupId: string;
|
||||
};
|
||||
cookie?: never;
|
||||
};
|
||||
requestBody?: never;
|
||||
responses: {
|
||||
/** @description Removed */
|
||||
204: {
|
||||
headers: {
|
||||
[name: string]: unknown;
|
||||
};
|
||||
content?: never;
|
||||
};
|
||||
403: components["responses"]["Error"];
|
||||
404: components["responses"]["Error"];
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1451,3 +1451,110 @@ class InviteInfo {
|
||||
'hasAccount': hasAccount,
|
||||
};
|
||||
}
|
||||
|
||||
class MemberUser {
|
||||
const MemberUser({required this.id, required this.email, required this.name});
|
||||
|
||||
factory MemberUser.fromJson(Map<String, dynamic> json) => MemberUser(
|
||||
id: json['id'] as String,
|
||||
email: json['email'] as String,
|
||||
name: json['name'] as String,
|
||||
);
|
||||
|
||||
final String id;
|
||||
|
||||
final String email;
|
||||
|
||||
final String name;
|
||||
|
||||
Map<String, dynamic> toJson() => {'id': id, 'email': email, 'name': name};
|
||||
}
|
||||
|
||||
class Group {
|
||||
const Group({
|
||||
required this.id,
|
||||
required this.workspace,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.members,
|
||||
required this.created,
|
||||
});
|
||||
|
||||
factory Group.fromJson(Map<String, dynamic> json) => Group(
|
||||
id: json['id'] as String,
|
||||
workspace: json['workspace'] as String,
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
members: (json['members'] as num).toInt(),
|
||||
created: json['created'] as String,
|
||||
);
|
||||
|
||||
final String id;
|
||||
|
||||
final String workspace;
|
||||
|
||||
final String name;
|
||||
|
||||
final String description;
|
||||
|
||||
final int members;
|
||||
|
||||
final String created;
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'workspace': workspace,
|
||||
'name': name,
|
||||
'description': description,
|
||||
'members': members,
|
||||
'created': created,
|
||||
};
|
||||
}
|
||||
|
||||
class GroupRequest {
|
||||
const GroupRequest({this.name, this.description});
|
||||
|
||||
factory GroupRequest.fromJson(Map<String, dynamic> json) => GroupRequest(
|
||||
name: json['name'] == null ? null : json['name'] as String,
|
||||
description: json['description'] == null
|
||||
? null
|
||||
: json['description'] as String,
|
||||
);
|
||||
|
||||
final String? name;
|
||||
|
||||
final String? description;
|
||||
|
||||
Map<String, dynamic> toJson() => {'name': ?name, 'description': ?description};
|
||||
}
|
||||
|
||||
class SpaceGroupGroup {
|
||||
const SpaceGroupGroup({required this.id, required this.name});
|
||||
|
||||
factory SpaceGroupGroup.fromJson(Map<String, dynamic> json) =>
|
||||
SpaceGroupGroup(id: json['id'] as String, name: json['name'] as String);
|
||||
|
||||
final String id;
|
||||
|
||||
final String name;
|
||||
|
||||
Map<String, dynamic> toJson() => {'id': id, 'name': name};
|
||||
}
|
||||
|
||||
class SpaceGroup {
|
||||
const SpaceGroup({required this.group, required this.role});
|
||||
|
||||
factory SpaceGroup.fromJson(Map<String, dynamic> json) => SpaceGroup(
|
||||
group: SpaceGroupGroup.fromJson(json['group'] as Map<String, dynamic>),
|
||||
role: SpaceRole.fromJson(json['role'] as String),
|
||||
);
|
||||
|
||||
final SpaceGroupGroup group;
|
||||
|
||||
final SpaceRole role;
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'group': group.toJson(),
|
||||
'role': role.toJson(),
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import vue from "@vitejs/plugin-vue";
|
||||
import { defineConfig } from "vite";
|
||||
|
||||
// DOCWELL_DEV_API points the dev proxy at another docwell-server.
|
||||
// The config runs in Node, but this tsconfig has no Node types.
|
||||
declare const process: { env: Record<string, string | undefined> };
|
||||
const api = process.env.DOCWELL_DEV_API ?? "http://localhost:8080";
|
||||
|
||||
export default defineConfig({
|
||||
|
||||
@@ -36,12 +36,21 @@ func ValidSpaceRole(role string) bool { return spaceRank[role] > 0 }
|
||||
// ValidDefaultRole reports whether role can be an open space's default role.
|
||||
func ValidDefaultRole(role string) bool { return ValidSpaceRole(role) && role != SpaceAdmin }
|
||||
|
||||
// HigherSpaceRole returns the higher of two space roles ("" ranks lowest).
|
||||
func HigherSpaceRole(a, b string) string {
|
||||
if spaceRank[b] > spaceRank[a] {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
// SpaceAtLeast reports whether space role has is at or above need.
|
||||
func SpaceAtLeast(has, need string) bool { return has != "" && spaceRank[has] >= spaceRank[need] }
|
||||
|
||||
// SpaceRole is the effective role in a space for someone with workspace role
|
||||
// wsRole (already resolved by WorkspaceRole) and explicit space role
|
||||
// memberRole ("" when not a space member). "" = no access.
|
||||
// memberRole ("" when not a space member; the highest of the person's own
|
||||
// and their groups' grants, see HigherSpaceRole). "" = no access.
|
||||
func SpaceRole(wsRole, visibility, defaultRole, memberRole string) string {
|
||||
switch {
|
||||
case wsRole == "":
|
||||
@@ -122,3 +131,31 @@ func SetSpaceMember(actorRole, targetWsRole, role string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSpaceGroup checks granting role in a space to a group of the space's
|
||||
// workspace.
|
||||
func SetSpaceGroup(actorRole, role string) error {
|
||||
if !ValidSpaceRole(role) {
|
||||
return apperr.Invalid("role must be admin, editor, commenter or viewer")
|
||||
}
|
||||
return ManageSpace(actorRole)
|
||||
}
|
||||
|
||||
// ---- Groups ----------------------------------------------------------------
|
||||
|
||||
// ManageGroups (create, rename, delete, change members) needs workspace admin.
|
||||
func ManageGroups(wsRole string) error {
|
||||
if !AtLeast(wsRole, WorkspaceAdmin) {
|
||||
return apperr.Forbidden("workspace admin role required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ViewGroups needs workspace membership above guest: guests see only what
|
||||
// was shared with them, not the workspace's structure.
|
||||
func ViewGroups(wsRole string) error {
|
||||
if !AtLeast(wsRole, WorkspaceMember) {
|
||||
return apperr.Forbidden("workspace member role required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -81,3 +81,18 @@ func TestSetSpaceMember(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupRules(t *testing.T) {
|
||||
if HigherSpaceRole(SpaceViewer, SpaceEditor) != SpaceEditor || HigherSpaceRole(SpaceAdmin, "") != SpaceAdmin {
|
||||
t.Error("HigherSpaceRole")
|
||||
}
|
||||
if ManageGroups(WorkspaceAdmin) != nil || ManageGroups(WorkspaceMember) == nil {
|
||||
t.Error("ManageGroups needs workspace admin")
|
||||
}
|
||||
if ViewGroups(WorkspaceMember) != nil || ViewGroups(WorkspaceGuest) == nil {
|
||||
t.Error("guests do not see groups")
|
||||
}
|
||||
if SetSpaceGroup(SpaceAdmin, SpaceViewer) != nil || SetSpaceGroup(SpaceEditor, SpaceViewer) == nil || SetSpaceGroup(SpaceAdmin, "x") == nil {
|
||||
t.Error("SetSpaceGroup")
|
||||
}
|
||||
}
|
||||
@@ -264,6 +264,22 @@ type Error struct {
|
||||
MfaId *string `json:"mfaId,omitempty"`
|
||||
}
|
||||
|
||||
// Group defines model for Group.
|
||||
type Group struct {
|
||||
Created string `json:"created"`
|
||||
Description string `json:"description"`
|
||||
Id string `json:"id"`
|
||||
Members int `json:"members"`
|
||||
Name string `json:"name"`
|
||||
Workspace string `json:"workspace"`
|
||||
}
|
||||
|
||||
// GroupRequest defines model for GroupRequest.
|
||||
type GroupRequest struct {
|
||||
Description *string `json:"description,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
// InstanceSettings defines model for InstanceSettings.
|
||||
type InstanceSettings struct {
|
||||
Auth AuthSettings `json:"auth"`
|
||||
@@ -334,6 +350,13 @@ type LoginRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// MemberUser defines model for MemberUser.
|
||||
type MemberUser struct {
|
||||
Email string `json:"email"`
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// NameRequest defines model for NameRequest.
|
||||
type NameRequest struct {
|
||||
Name string `json:"name"`
|
||||
@@ -481,6 +504,15 @@ type Space struct {
|
||||
// SpaceDefaultRole Role that workspace members (not guests) get in an open space without an explicit role.
|
||||
type SpaceDefaultRole string
|
||||
|
||||
// SpaceGroup defines model for SpaceGroup.
|
||||
type SpaceGroup struct {
|
||||
Group struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"group"`
|
||||
Role SpaceRole `json:"role"`
|
||||
}
|
||||
|
||||
// SpaceMember defines model for SpaceMember.
|
||||
type SpaceMember struct {
|
||||
Role SpaceRole `json:"role"`
|
||||
@@ -685,6 +717,12 @@ type UpdateSpaceJSONBody struct {
|
||||
Visibility *SpaceVisibility `json:"visibility,omitempty"`
|
||||
}
|
||||
|
||||
// SetSpaceGroupJSONBody defines parameters for SetSpaceGroup.
|
||||
type SetSpaceGroupJSONBody struct {
|
||||
Group string `json:"group"`
|
||||
Role SpaceRole `json:"role"`
|
||||
}
|
||||
|
||||
// SetSpaceMemberJSONBody defines parameters for SetSpaceMember.
|
||||
type SetSpaceMemberJSONBody struct {
|
||||
Email openapi_types.Email `json:"email"`
|
||||
@@ -768,6 +806,12 @@ type ConfirmVerificationJSONRequestBody ConfirmVerificationJSONBody
|
||||
// RequestVerificationJSONRequestBody defines body for RequestVerification for application/json ContentType.
|
||||
type RequestVerificationJSONRequestBody = EmailRequest
|
||||
|
||||
// UpdateGroupJSONRequestBody defines body for UpdateGroup for application/json ContentType.
|
||||
type UpdateGroupJSONRequestBody = GroupRequest
|
||||
|
||||
// AddGroupMemberJSONRequestBody defines body for AddGroupMember for application/json ContentType.
|
||||
type AddGroupMemberJSONRequestBody = EmailRequest
|
||||
|
||||
// CreateInviteJSONRequestBody defines body for CreateInvite for application/json ContentType.
|
||||
type CreateInviteJSONRequestBody = CreateInviteRequest
|
||||
|
||||
@@ -789,6 +833,9 @@ type MovePageJSONRequestBody MovePageJSONBody
|
||||
// UpdateSpaceJSONRequestBody defines body for UpdateSpace for application/json ContentType.
|
||||
type UpdateSpaceJSONRequestBody UpdateSpaceJSONBody
|
||||
|
||||
// SetSpaceGroupJSONRequestBody defines body for SetSpaceGroup for application/json ContentType.
|
||||
type SetSpaceGroupJSONRequestBody SetSpaceGroupJSONBody
|
||||
|
||||
// SetSpaceMemberJSONRequestBody defines body for SetSpaceMember for application/json ContentType.
|
||||
type SetSpaceMemberJSONRequestBody SetSpaceMemberJSONBody
|
||||
|
||||
@@ -807,6 +854,9 @@ type CreateWorkspaceJSONRequestBody CreateWorkspaceJSONBody
|
||||
// RenameWorkspaceJSONRequestBody defines body for RenameWorkspace for application/json ContentType.
|
||||
type RenameWorkspaceJSONRequestBody = NameRequest
|
||||
|
||||
// CreateGroupJSONRequestBody defines body for CreateGroup for application/json ContentType.
|
||||
type CreateGroupJSONRequestBody = GroupRequest
|
||||
|
||||
// SetWorkspaceMemberJSONRequestBody defines body for SetWorkspaceMember for application/json ContentType.
|
||||
type SetWorkspaceMemberJSONRequestBody SetWorkspaceMemberJSONBody
|
||||
|
||||
|
||||
@@ -835,16 +835,17 @@ func (s *Service) RemoveMember(ctx context.Context, c access.Caller, wsID, userI
|
||||
if err := s.pb.Delete(ctx, "workspace_members", current.ID); err != nil {
|
||||
return mapPB(err, "member not found")
|
||||
}
|
||||
// Space roles mean nothing without the workspace; drop them (best effort).
|
||||
// Space roles and groups mean nothing without the workspace; drop them (best effort).
|
||||
if spaceMembers, err := s.userSpaceMemberships(ctx, userID, ws.ID); err == nil {
|
||||
for _, m := range spaceMembers {
|
||||
_ = s.pb.Delete(ctx, "space_members", m.ID)
|
||||
}
|
||||
}
|
||||
s.dropGroupMemberships(ctx, userID, ws.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// dropMemberships removes all of a user's workspace and space memberships (best effort).
|
||||
// dropMemberships removes all of a user's workspace, group and space memberships (best effort).
|
||||
func (s *Service) dropMemberships(ctx context.Context, userID string) {
|
||||
res, err := pocketbase.List[memberRecord](ctx, s.pb, "workspace_members", pocketbase.ListOptions{
|
||||
PerPage: 500, Filter: fmt.Sprintf("user = '%s'", userID),
|
||||
@@ -855,6 +856,7 @@ func (s *Service) dropMemberships(ctx context.Context, userID string) {
|
||||
for _, m := range res.Items {
|
||||
_ = s.pb.Delete(ctx, "workspace_members", m.ID)
|
||||
}
|
||||
s.dropGroupMemberships(ctx, userID, "")
|
||||
spaceMembers, err := s.userSpaceMemberships(ctx, userID, "")
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -0,0 +1,445 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package directory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"docwell/server/internal/access"
|
||||
"docwell/server/internal/apperr"
|
||||
"docwell/server/internal/store/pocketbase"
|
||||
)
|
||||
|
||||
// ---- Groups ----------------------------------------------------------------
|
||||
//
|
||||
// Groups belong to a workspace and hold some of its members. Spaces grant
|
||||
// roles to groups like to people (space_members.principal_type = "group").
|
||||
|
||||
type Group struct {
|
||||
ID string `json:"id"`
|
||||
Workspace string `json:"workspace"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Members int `json:"members"`
|
||||
Created string `json:"created"`
|
||||
}
|
||||
|
||||
type groupRecord struct {
|
||||
ID string `json:"id"`
|
||||
Workspace string `json:"workspace"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Created string `json:"created"`
|
||||
}
|
||||
|
||||
type groupMemberRecord struct {
|
||||
ID string `json:"id"`
|
||||
Group string `json:"group"`
|
||||
User string `json:"user"`
|
||||
Expand struct {
|
||||
User *struct {
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
} `json:"user"`
|
||||
} `json:"expand"`
|
||||
}
|
||||
|
||||
// SpaceGroup is a group's role in a space.
|
||||
type SpaceGroup struct {
|
||||
Group GroupRef `json:"group"`
|
||||
Role string `json:"role"`
|
||||
}
|
||||
|
||||
type GroupRef struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// userGroups lists the ids of userID's groups in workspace wsID.
|
||||
func (s *Service) userGroups(ctx context.Context, userID, wsID string) ([]string, error) {
|
||||
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{
|
||||
Filter: fmt.Sprintf("user = '%s' && group.workspace = '%s'", userID, wsID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]string, 0, len(recs))
|
||||
for _, r := range recs {
|
||||
ids = append(ids, r.Group)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// groupGrants lists the space grants of groups; spaceID "" means every space.
|
||||
func (s *Service) groupGrants(ctx context.Context, groups []string, spaceID string) ([]spaceMemberRecord, error) {
|
||||
if len(groups) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
ors := make([]string, len(groups))
|
||||
for i, g := range groups {
|
||||
ors[i] = fmt.Sprintf("principal_id = '%s'", g)
|
||||
}
|
||||
filter := "principal_type = 'group' && (" + strings.Join(ors, " || ") + ")"
|
||||
if spaceID != "" {
|
||||
filter += fmt.Sprintf(" && space = '%s'", spaceID)
|
||||
}
|
||||
return listAll[spaceMemberRecord](ctx, s, "space_members", pocketbase.ListOptions{Filter: filter})
|
||||
}
|
||||
|
||||
// explicitSpaceRole is the highest of userID's own and their groups' roles
|
||||
// in space sp ("" when neither has one).
|
||||
func (s *Service) explicitSpaceRole(ctx context.Context, sp spaceRecord, userID string) (string, error) {
|
||||
member, err := s.spaceMembership(ctx, sp.ID, userID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
role := member.Role
|
||||
groups, err := s.userGroups(ctx, userID, sp.Workspace)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
grants, err := s.groupGrants(ctx, groups, sp.ID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, g := range grants {
|
||||
role = access.HigherSpaceRole(role, g.Role)
|
||||
}
|
||||
return role, nil
|
||||
}
|
||||
|
||||
func (s *Service) groupFor(ctx context.Context, c access.Caller, id string) (groupRecord, string, error) {
|
||||
if !validID(id) {
|
||||
return groupRecord{}, "", apperr.NotFound("group not found")
|
||||
}
|
||||
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", id)
|
||||
if err != nil {
|
||||
return g, "", mapPB(err, "group not found")
|
||||
}
|
||||
_, wsRole, err := s.workspaceFor(ctx, c, g.Workspace)
|
||||
if err != nil {
|
||||
return g, "", apperr.NotFound("group not found")
|
||||
}
|
||||
if err := access.ViewGroups(wsRole); err != nil {
|
||||
return g, "", apperr.NotFound("group not found")
|
||||
}
|
||||
return g, wsRole, nil
|
||||
}
|
||||
|
||||
func (s *Service) groupView(ctx context.Context, g groupRecord) (Group, error) {
|
||||
n, err := s.count(ctx, "group_members", fmt.Sprintf("group = '%s'", g.ID))
|
||||
return Group{ID: g.ID, Workspace: g.Workspace, Name: g.Name, Description: g.Description, Members: n, Created: g.Created}, err
|
||||
}
|
||||
|
||||
func groupNameError(err error) error {
|
||||
var pbErr *pocketbase.Error
|
||||
if errors.As(err, &pbErr) && pbErr.Status == 400 {
|
||||
// The unique (workspace, name) index fails as a generic constraint error.
|
||||
for _, f := range pbErr.Data {
|
||||
if f.Code == "validation_not_unique" {
|
||||
return apperr.Conflict("a group with this name already exists")
|
||||
}
|
||||
}
|
||||
}
|
||||
return mapPB(err, "group not found")
|
||||
}
|
||||
|
||||
// ListGroups lists a workspace's groups (members and above).
|
||||
func (s *Service) ListGroups(ctx context.Context, c access.Caller, wsID string) ([]Group, error) {
|
||||
ws, wsRole, err := s.workspaceFor(ctx, c, wsID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := access.ViewGroups(wsRole); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recs, err := listAll[groupRecord](ctx, s, "groups", pocketbase.ListOptions{
|
||||
Filter: fmt.Sprintf("workspace = '%s'", ws.ID), Sort: "name",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Group, 0, len(recs))
|
||||
for _, g := range recs {
|
||||
v, err := s.groupView(ctx, g)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool { return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name) })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type GroupInput struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
}
|
||||
|
||||
func (in GroupInput) patch() (map[string]any, error) {
|
||||
patch := map[string]any{}
|
||||
if in.Name != nil {
|
||||
name, err := cleanName(*in.Name, "Group")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
patch["name"] = name
|
||||
}
|
||||
if in.Description != nil {
|
||||
d := strings.TrimSpace(*in.Description)
|
||||
if len(d) > 500 {
|
||||
return nil, apperr.InvalidFields(map[string]string{"description": "Description is too long (max 500)."})
|
||||
}
|
||||
patch["description"] = d
|
||||
}
|
||||
return patch, nil
|
||||
}
|
||||
|
||||
// CreateGroup adds a group to a workspace (workspace admin).
|
||||
func (s *Service) CreateGroup(ctx context.Context, c access.Caller, wsID string, in GroupInput) (Group, error) {
|
||||
if in.Name == nil {
|
||||
in.Name = new(string)
|
||||
}
|
||||
patch, err := in.patch()
|
||||
if err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
ws, wsRole, err := s.workspaceFor(ctx, c, wsID)
|
||||
if err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
if err := access.ManageGroups(wsRole); err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
patch["workspace"] = ws.ID
|
||||
g, err := pocketbase.Create[groupRecord](ctx, s.pb, "groups", patch)
|
||||
if err != nil {
|
||||
return Group{}, groupNameError(err)
|
||||
}
|
||||
return s.groupView(ctx, g)
|
||||
}
|
||||
|
||||
// UpdateGroup renames a group or changes its description (workspace admin).
|
||||
func (s *Service) UpdateGroup(ctx context.Context, c access.Caller, id string, in GroupInput) (Group, error) {
|
||||
g, wsRole, err := s.groupFor(ctx, c, id)
|
||||
if err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
if err := access.ManageGroups(wsRole); err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
patch, err := in.patch()
|
||||
if err != nil {
|
||||
return Group{}, err
|
||||
}
|
||||
if len(patch) > 0 {
|
||||
if g, err = pocketbase.Update[groupRecord](ctx, s.pb, "groups", g.ID, patch); err != nil {
|
||||
return Group{}, groupNameError(err)
|
||||
}
|
||||
}
|
||||
return s.groupView(ctx, g)
|
||||
}
|
||||
|
||||
// DeleteGroup deletes a group and its space grants (workspace admin).
|
||||
func (s *Service) DeleteGroup(ctx context.Context, c access.Caller, id string) error {
|
||||
g, wsRole, err := s.groupFor(ctx, c, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := access.ManageGroups(wsRole); err != nil {
|
||||
return err
|
||||
}
|
||||
// space_members.principal_id is plain text, so PocketBase cannot cascade.
|
||||
grants, err := s.groupGrants(ctx, []string{g.ID}, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range grants {
|
||||
if err := s.pb.Delete(ctx, "space_members", m.ID); err != nil && !pocketbase.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return mapPB(s.pb.Delete(ctx, "groups", g.ID), "group not found")
|
||||
}
|
||||
|
||||
// ListGroupMembers lists a group's members (members and above).
|
||||
func (s *Service) ListGroupMembers(ctx context.Context, c access.Caller, id string) ([]MemberUser, error) {
|
||||
g, _, err := s.groupFor(ctx, c, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{
|
||||
Filter: fmt.Sprintf("group = '%s'", g.ID), Expand: "user",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]MemberUser, 0, len(recs))
|
||||
for _, r := range recs {
|
||||
if u := r.Expand.User; u != nil {
|
||||
out = append(out, MemberUser{ID: u.ID, Email: u.Email, Name: u.Name})
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Email < out[j].Email })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// AddGroupMember adds a workspace member (by email) to a group (workspace admin).
|
||||
func (s *Service) AddGroupMember(ctx context.Context, c access.Caller, id, email string) (MemberUser, error) {
|
||||
g, wsRole, err := s.groupFor(ctx, c, id)
|
||||
if err != nil {
|
||||
return MemberUser{}, err
|
||||
}
|
||||
if err := access.ManageGroups(wsRole); err != nil {
|
||||
return MemberUser{}, err
|
||||
}
|
||||
u, err := s.findUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
return MemberUser{}, err
|
||||
}
|
||||
ws, err := s.getWorkspace(ctx, g.Workspace)
|
||||
if err != nil {
|
||||
return MemberUser{}, err
|
||||
}
|
||||
if role, err := s.effective(ctx, u.Caller(), ws); err != nil {
|
||||
return MemberUser{}, err
|
||||
} else if role == "" {
|
||||
return MemberUser{}, apperr.Invalid("that user is not a member of this workspace")
|
||||
}
|
||||
existing, err := pocketbase.List[groupMemberRecord](ctx, s.pb, "group_members", pocketbase.ListOptions{
|
||||
PerPage: 1, Filter: fmt.Sprintf("group = '%s' && user = '%s'", g.ID, u.ID),
|
||||
})
|
||||
if err != nil {
|
||||
return MemberUser{}, err
|
||||
}
|
||||
if len(existing.Items) == 0 {
|
||||
if _, err := pocketbase.Create[groupMemberRecord](ctx, s.pb, "group_members", map[string]any{"group": g.ID, "user": u.ID}); err != nil {
|
||||
return MemberUser{}, mapPB(err, "group not found")
|
||||
}
|
||||
}
|
||||
return MemberUser{ID: u.ID, Email: u.Email, Name: u.Name}, nil
|
||||
}
|
||||
|
||||
// RemoveGroupMember removes someone from a group (workspace admin). Idempotent.
|
||||
func (s *Service) RemoveGroupMember(ctx context.Context, c access.Caller, id, userID string) error {
|
||||
g, wsRole, err := s.groupFor(ctx, c, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := access.ManageGroups(wsRole); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validID(userID) {
|
||||
return apperr.NotFound("member not found")
|
||||
}
|
||||
existing, err := pocketbase.List[groupMemberRecord](ctx, s.pb, "group_members", pocketbase.ListOptions{
|
||||
PerPage: 1, Filter: fmt.Sprintf("group = '%s' && user = '%s'", g.ID, userID),
|
||||
})
|
||||
if err != nil || len(existing.Items) == 0 {
|
||||
return err
|
||||
}
|
||||
return mapPB(s.pb.Delete(ctx, "group_members", existing.Items[0].ID), "member not found")
|
||||
}
|
||||
|
||||
// dropGroupMemberships removes userID from every group of workspace wsID ("" = all; best effort).
|
||||
func (s *Service) dropGroupMemberships(ctx context.Context, userID, wsID string) {
|
||||
filter := fmt.Sprintf("user = '%s'", userID)
|
||||
if wsID != "" {
|
||||
filter += fmt.Sprintf(" && group.workspace = '%s'", wsID)
|
||||
}
|
||||
recs, err := listAll[groupMemberRecord](ctx, s, "group_members", pocketbase.ListOptions{Filter: filter})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, r := range recs {
|
||||
_ = s.pb.Delete(ctx, "group_members", r.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// ListSpaceGroups lists the groups with a role in a space (any space role may look).
|
||||
func (s *Service) ListSpaceGroups(ctx context.Context, c access.Caller, spaceID string) ([]SpaceGroup, error) {
|
||||
a, err := s.spaceFor(ctx, c, spaceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
grants, err := listAll[spaceMemberRecord](ctx, s, "space_members", pocketbase.ListOptions{
|
||||
Filter: fmt.Sprintf("space = '%s' && principal_type = 'group'", a.space.ID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]SpaceGroup, 0, len(grants))
|
||||
for _, m := range grants {
|
||||
if !validID(m.PrincipalID) {
|
||||
continue
|
||||
}
|
||||
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", m.PrincipalID)
|
||||
if err != nil {
|
||||
continue // a deleted group's leftover row
|
||||
}
|
||||
out = append(out, SpaceGroup{Group: GroupRef{ID: g.ID, Name: g.Name}, Role: m.Role})
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return strings.ToLower(out[i].Group.Name) < strings.ToLower(out[j].Group.Name) })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// SetSpaceGroup grants a group of the space's workspace a role in the space (space admin).
|
||||
func (s *Service) SetSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID, role string) (SpaceGroup, error) {
|
||||
a, err := s.spaceFor(ctx, c, spaceID)
|
||||
if err != nil {
|
||||
return SpaceGroup{}, err
|
||||
}
|
||||
if err := access.SetSpaceGroup(a.role, role); err != nil {
|
||||
return SpaceGroup{}, err
|
||||
}
|
||||
if !validID(groupID) {
|
||||
return SpaceGroup{}, apperr.NotFound("group not found")
|
||||
}
|
||||
g, err := pocketbase.Get[groupRecord](ctx, s.pb, "groups", groupID)
|
||||
if err != nil || g.Workspace != a.space.Workspace {
|
||||
return SpaceGroup{}, apperr.NotFound("group not found")
|
||||
}
|
||||
existing, err := pocketbase.List[spaceMemberRecord](ctx, s.pb, "space_members", pocketbase.ListOptions{
|
||||
PerPage: 1, Filter: fmt.Sprintf("space = '%s' && principal_type = 'group' && principal_id = '%s'", a.space.ID, g.ID),
|
||||
})
|
||||
if err != nil {
|
||||
return SpaceGroup{}, err
|
||||
}
|
||||
if len(existing.Items) > 0 {
|
||||
_, err = pocketbase.Update[spaceMemberRecord](ctx, s.pb, "space_members", existing.Items[0].ID, map[string]any{"role": role})
|
||||
} else {
|
||||
_, err = pocketbase.Create[spaceMemberRecord](ctx, s.pb, "space_members", map[string]any{
|
||||
"space": a.space.ID, "principal_type": "group", "principal_id": g.ID, "role": role,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return SpaceGroup{}, mapPB(err, "space not found")
|
||||
}
|
||||
return SpaceGroup{Group: GroupRef{ID: g.ID, Name: g.Name}, Role: role}, nil
|
||||
}
|
||||
|
||||
// RemoveSpaceGroup takes a group's role in a space away (space admin). Idempotent.
|
||||
func (s *Service) RemoveSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID string) error {
|
||||
a, err := s.spaceFor(ctx, c, spaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := access.ManageSpace(a.role); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validID(groupID) {
|
||||
return apperr.NotFound("group not found")
|
||||
}
|
||||
existing, err := pocketbase.List[spaceMemberRecord](ctx, s.pb, "space_members", pocketbase.ListOptions{
|
||||
PerPage: 1, Filter: fmt.Sprintf("space = '%s' && principal_type = 'group' && principal_id = '%s'", a.space.ID, groupID),
|
||||
})
|
||||
if err != nil || len(existing.Items) == 0 {
|
||||
return err
|
||||
}
|
||||
return mapPB(s.pb.Delete(ctx, "space_members", existing.Items[0].ID), "group not found")
|
||||
}
|
||||
@@ -122,13 +122,13 @@ func (s *Service) spaceFor(ctx context.Context, c access.Caller, id string) (spa
|
||||
if err != nil {
|
||||
return spaceAccess{}, err
|
||||
}
|
||||
var member spaceMemberRecord
|
||||
var explicit string
|
||||
if wsRole != "" && !access.AtLeast(wsRole, access.WorkspaceAdmin) {
|
||||
if member, err = s.spaceMembership(ctx, sp.ID, c.ID); err != nil {
|
||||
if explicit, err = s.explicitSpaceRole(ctx, sp, c.ID); err != nil {
|
||||
return spaceAccess{}, err
|
||||
}
|
||||
}
|
||||
a := spaceAccess{space: sp, ws: ws, wsRole: wsRole, role: access.SpaceRole(wsRole, sp.Visibility, sp.DefaultRole, member.Role)}
|
||||
a := spaceAccess{space: sp, ws: ws, wsRole: wsRole, role: access.SpaceRole(wsRole, sp.Visibility, sp.DefaultRole, explicit)}
|
||||
if err := access.ViewSpace(a.role); err != nil {
|
||||
return spaceAccess{}, err
|
||||
}
|
||||
@@ -156,6 +156,17 @@ func (s *Service) ListSpaces(ctx context.Context, c access.Caller, wsID string)
|
||||
for _, m := range mine {
|
||||
explicit[m.Space] = m.Role
|
||||
}
|
||||
groups, err := s.userGroups(ctx, c.ID, ws.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
grants, err := s.groupGrants(ctx, groups, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, g := range grants {
|
||||
explicit[g.Space] = access.HigherSpaceRole(explicit[g.Space], g.Role)
|
||||
}
|
||||
}
|
||||
out := []Space{}
|
||||
for _, sp := range spaces {
|
||||
|
||||
@@ -143,6 +143,7 @@ func (a *api) routes(mux *routeMux) {
|
||||
a.spaceRoutes(mux)
|
||||
a.settingsRoutes(mux)
|
||||
a.inviteRoutes(mux)
|
||||
a.groupRoutes(mux)
|
||||
}
|
||||
|
||||
func (a *api) decode(w http.ResponseWriter, r *http.Request, v any) bool {
|
||||
|
||||
@@ -34,6 +34,7 @@ type DirectoryService interface {
|
||||
|
||||
SpaceService
|
||||
InviteService
|
||||
GroupService
|
||||
}
|
||||
|
||||
func caller(r *http.Request) access.Caller {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"docwell/server/internal/access"
|
||||
"docwell/server/internal/directory"
|
||||
)
|
||||
|
||||
// GroupService is the part of directory.Service that manages groups.
|
||||
type GroupService interface {
|
||||
ListGroups(ctx context.Context, c access.Caller, wsID string) ([]directory.Group, error)
|
||||
CreateGroup(ctx context.Context, c access.Caller, wsID string, in directory.GroupInput) (directory.Group, error)
|
||||
UpdateGroup(ctx context.Context, c access.Caller, id string, in directory.GroupInput) (directory.Group, error)
|
||||
DeleteGroup(ctx context.Context, c access.Caller, id string) error
|
||||
ListGroupMembers(ctx context.Context, c access.Caller, id string) ([]directory.MemberUser, error)
|
||||
AddGroupMember(ctx context.Context, c access.Caller, id, email string) (directory.MemberUser, error)
|
||||
RemoveGroupMember(ctx context.Context, c access.Caller, id, userID string) error
|
||||
ListSpaceGroups(ctx context.Context, c access.Caller, spaceID string) ([]directory.SpaceGroup, error)
|
||||
SetSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID, role string) (directory.SpaceGroup, error)
|
||||
RemoveSpaceGroup(ctx context.Context, c access.Caller, spaceID, groupID string) error
|
||||
}
|
||||
|
||||
func (a *api) groupRoutes(mux *routeMux) {
|
||||
mux.HandleFunc("GET /api/v1/workspaces/{id}/groups", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := a.dir.ListGroups(r.Context(), caller(r), r.PathValue("id"))
|
||||
a.respond(w, list, err)
|
||||
}))
|
||||
mux.HandleFunc("POST /api/v1/workspaces/{id}/groups", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in directory.GroupInput
|
||||
if !a.decode(w, r, &in) {
|
||||
return
|
||||
}
|
||||
g, err := a.dir.CreateGroup(r.Context(), caller(r), r.PathValue("id"), in)
|
||||
a.respondStatus(w, http.StatusCreated, g, err)
|
||||
}))
|
||||
mux.HandleFunc("PATCH /api/v1/groups/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in directory.GroupInput
|
||||
if !a.decode(w, r, &in) {
|
||||
return
|
||||
}
|
||||
g, err := a.dir.UpdateGroup(r.Context(), caller(r), r.PathValue("id"), in)
|
||||
a.respond(w, g, err)
|
||||
}))
|
||||
mux.HandleFunc("DELETE /api/v1/groups/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
a.respondNoContent(w, a.dir.DeleteGroup(r.Context(), caller(r), r.PathValue("id")))
|
||||
}))
|
||||
mux.HandleFunc("GET /api/v1/groups/{id}/members", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := a.dir.ListGroupMembers(r.Context(), caller(r), r.PathValue("id"))
|
||||
a.respond(w, list, err)
|
||||
}))
|
||||
mux.HandleFunc("PUT /api/v1/groups/{id}/members", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct{ Email string }
|
||||
if !a.decode(w, r, &in) {
|
||||
return
|
||||
}
|
||||
m, err := a.dir.AddGroupMember(r.Context(), caller(r), r.PathValue("id"), in.Email)
|
||||
a.respond(w, m, err)
|
||||
}))
|
||||
mux.HandleFunc("DELETE /api/v1/groups/{id}/members/{userId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
a.respondNoContent(w, a.dir.RemoveGroupMember(r.Context(), caller(r), r.PathValue("id"), r.PathValue("userId")))
|
||||
}))
|
||||
|
||||
mux.HandleFunc("GET /api/v1/spaces/{id}/groups", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := a.dir.ListSpaceGroups(r.Context(), caller(r), r.PathValue("id"))
|
||||
a.respond(w, list, err)
|
||||
}))
|
||||
mux.HandleFunc("PUT /api/v1/spaces/{id}/groups", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct{ Group, Role string }
|
||||
if !a.decode(w, r, &in) {
|
||||
return
|
||||
}
|
||||
g, err := a.dir.SetSpaceGroup(r.Context(), caller(r), r.PathValue("id"), in.Group, in.Role)
|
||||
a.respond(w, g, err)
|
||||
}))
|
||||
mux.HandleFunc("DELETE /api/v1/spaces/{id}/groups/{groupId}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
||||
a.respondNoContent(w, a.dir.RemoveSpaceGroup(r.Context(), caller(r), r.PathValue("id"), r.PathValue("groupId")))
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
|
||||
<script setup lang="ts">
|
||||
import type { Group, MemberUser } from "@docwell/api-client";
|
||||
import { DwButton, DwCallout } from "@docwell/ui";
|
||||
import { onMounted, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useApi } from "../api";
|
||||
import { useFormErrors } from "../composables/useFormErrors";
|
||||
|
||||
// Groups of a workspace's members; spaces can grant a role to a whole group.
|
||||
const props = defineProps<{ workspace: string; canManage: boolean }>();
|
||||
const api = useApi();
|
||||
const { t } = useI18n();
|
||||
const errors = useFormErrors();
|
||||
|
||||
const groups = ref<Group[]>([]);
|
||||
const open = ref<string | null>(null);
|
||||
const members = ref<MemberUser[]>([]);
|
||||
const newName = ref("");
|
||||
const email = ref("");
|
||||
const busy = ref(false);
|
||||
|
||||
async function load() {
|
||||
groups.value = await api.workspaces.groups(props.workspace);
|
||||
if (open.value) members.value = await api.groups.members(open.value);
|
||||
}
|
||||
onMounted(() => load().catch(errors.show));
|
||||
|
||||
async function run(action: () => Promise<unknown>) {
|
||||
busy.value = true;
|
||||
errors.reset();
|
||||
try {
|
||||
await action();
|
||||
await load();
|
||||
return true;
|
||||
} catch (e) {
|
||||
errors.show(e);
|
||||
return false;
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function toggle(g: Group) {
|
||||
open.value = open.value === g.id ? null : g.id;
|
||||
email.value = "";
|
||||
members.value = open.value ? await api.groups.members(g.id).catch(() => []) : [];
|
||||
}
|
||||
|
||||
async function create() {
|
||||
if (await run(() => api.workspaces.createGroup(props.workspace, { name: newName.value }))) newName.value = "";
|
||||
}
|
||||
async function addMember(g: Group) {
|
||||
if (await run(() => api.groups.addMember(g.id, email.value))) email.value = "";
|
||||
}
|
||||
const removeMember = (g: Group, m: MemberUser) => run(() => api.groups.removeMember(g.id, m.id));
|
||||
async function removeGroup(g: Group) {
|
||||
if (!confirm(t("groups.confirmDelete", { name: g.name }))) return;
|
||||
if (open.value === g.id) open.value = null;
|
||||
await run(() => api.groups.remove(g.id));
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="dw-card">
|
||||
<h2 class="text-ui-lg">{{ $t("groups.title") }}</h2>
|
||||
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("groups.hint") }}</p>
|
||||
<DwCallout v-if="errors.message.value" tone="danger" size="ui" class="mt-3">{{ errors.message.value }}</DwCallout>
|
||||
|
||||
<form v-if="canManage" class="mt-4 flex flex-wrap gap-2" @submit.prevent="create">
|
||||
<input
|
||||
v-model="newName"
|
||||
required
|
||||
maxlength="120"
|
||||
:placeholder="$t('groups.name')"
|
||||
:aria-label="$t('groups.name')"
|
||||
class="dw-input min-w-48 flex-1"
|
||||
/>
|
||||
<DwButton type="submit" :disabled="busy || !newName.trim()">{{ $t("groups.create") }}</DwButton>
|
||||
</form>
|
||||
<p v-if="errors.fields.value.name" class="mt-1 text-ui-sm text-danger">{{ errors.fields.value.name }}</p>
|
||||
|
||||
<ul class="mt-4 divide-y divide-line">
|
||||
<li v-for="g in groups" :key="g.id" class="py-2" data-testid="group">
|
||||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||||
<button class="min-w-0 text-left" :aria-expanded="open === g.id" @click="toggle(g)">
|
||||
<span class="block truncate text-ui-md font-medium">{{ g.name }}</span>
|
||||
<span class="block text-ui-sm text-ink-muted">{{ $t("groups.memberCount", { n: g.members }) }}</span>
|
||||
</button>
|
||||
<button
|
||||
v-if="canManage"
|
||||
class="text-ui-md text-danger hover:underline"
|
||||
:disabled="busy"
|
||||
@click="removeGroup(g)"
|
||||
>
|
||||
{{ $t("groups.delete") }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-if="open === g.id" class="mt-2 rounded-sm bg-surface-sunken p-3">
|
||||
<form v-if="canManage" class="flex flex-wrap gap-2" @submit.prevent="addMember(g)">
|
||||
<input
|
||||
v-model="email"
|
||||
type="email"
|
||||
required
|
||||
:placeholder="$t('auth.email')"
|
||||
:aria-label="$t('auth.email')"
|
||||
class="dw-input min-w-48 flex-1"
|
||||
/>
|
||||
<DwButton type="submit" size="sm" :disabled="busy || !email">{{ $t("workspace.add") }}</DwButton>
|
||||
</form>
|
||||
<ul class="mt-2 space-y-1">
|
||||
<li v-for="m in members" :key="m.id" class="flex items-center justify-between gap-2 text-ui-md">
|
||||
<span class="min-w-0 truncate"
|
||||
>{{ m.name }} <span class="text-ink-muted">· {{ m.email }}</span></span
|
||||
>
|
||||
<button v-if="canManage" class="text-danger hover:underline" :disabled="busy" @click="removeMember(g, m)">
|
||||
{{ $t("workspace.remove") }}
|
||||
</button>
|
||||
</li>
|
||||
<li v-if="!members.length" class="text-ui-sm text-ink-muted">{{ $t("groups.empty") }}</li>
|
||||
</ul>
|
||||
</div>
|
||||
</li>
|
||||
<li v-if="!groups.length" class="py-2 text-ui-md text-ink-muted">{{ $t("groups.none") }}</li>
|
||||
</ul>
|
||||
</section>
|
||||
</template>
|
||||
@@ -252,5 +252,19 @@
|
||||
"signInFirst": "{email} already has an account. Sign in to accept.",
|
||||
"wrongAccount": "This invitation is for {email}, but you are signed in as {current}.",
|
||||
"switchAccount": "Sign in with another account"
|
||||
},
|
||||
"groups": {
|
||||
"title": "Groups",
|
||||
"hint": "Groups collect workspace members, so a space can give all of them a role at once.",
|
||||
"spaceHint": "Everyone in a group gets its role here. Someone in several groups gets the highest role.",
|
||||
"name": "Group name",
|
||||
"create": "Create group",
|
||||
"delete": "Delete",
|
||||
"confirmDelete": "Delete the group {name}? Its members lose the roles it gave them.",
|
||||
"memberCount": "{n} member | {n} members",
|
||||
"empty": "No members yet.",
|
||||
"none": "No groups yet.",
|
||||
"pick": "Choose a group",
|
||||
"noneToAdd": "This workspace has no groups yet. Create them in the workspace settings."
|
||||
}
|
||||
}
|
||||
@@ -252,5 +252,19 @@
|
||||
"signInFirst": "{email} ma już konto. Zaloguj się, aby przyjąć zaproszenie.",
|
||||
"wrongAccount": "To zaproszenie jest dla {email}, a zalogowano Cię jako {current}.",
|
||||
"switchAccount": "Zaloguj się na inne konto"
|
||||
},
|
||||
"groups": {
|
||||
"title": "Grupy",
|
||||
"hint": "Grupy zbierają członków obszaru roboczego, aby przestrzeń mogła nadać rolę wszystkim naraz.",
|
||||
"spaceHint": "Każdy w grupie dostaje tu jej rolę. Osoba w kilku grupach dostaje najwyższą z ról.",
|
||||
"name": "Nazwa grupy",
|
||||
"create": "Utwórz grupę",
|
||||
"delete": "Usuń",
|
||||
"confirmDelete": "Usunąć grupę {name}? Jej członkowie stracą nadane przez nią role.",
|
||||
"memberCount": "{n} członek | {n} członków | {n} członków",
|
||||
"empty": "Brak członków.",
|
||||
"none": "Brak grup.",
|
||||
"pick": "Wybierz grupę",
|
||||
"noneToAdd": "Ten obszar roboczy nie ma jeszcze grup. Utwórz je w ustawieniach obszaru roboczego."
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
<!-- SPDX-License-Identifier: AGPL-3.0-or-later -->
|
||||
<script setup lang="ts">
|
||||
import type { Space, SpaceDefaultRole, SpaceMember, SpaceRole, SpaceVisibility } from "@docwell/api-client";
|
||||
import type {
|
||||
Group,
|
||||
Space,
|
||||
SpaceDefaultRole,
|
||||
SpaceGroup,
|
||||
SpaceMember,
|
||||
SpaceRole,
|
||||
SpaceVisibility,
|
||||
} from "@docwell/api-client";
|
||||
import { DwButton, DwCallout } from "@docwell/ui";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
@@ -23,6 +31,14 @@ const errors = useFormErrors();
|
||||
|
||||
const space = ref<Space | null>(null);
|
||||
const members = ref<SpaceMember[]>([]);
|
||||
const spaceGroups = ref<SpaceGroup[]>([]);
|
||||
// The workspace's groups that have no role here yet, for the "add group" picker.
|
||||
const allGroups = ref<Group[]>([]);
|
||||
const addableGroups = computed(() =>
|
||||
allGroups.value.filter((g) => !spaceGroups.value.some((sg) => sg.group.id === g.id)),
|
||||
);
|
||||
const newGroup = ref("");
|
||||
const newGroupRole = ref<SpaceRole>("editor");
|
||||
const notFound = ref(false);
|
||||
const busy = ref(false);
|
||||
const form = ref({
|
||||
@@ -58,6 +74,8 @@ async function load() {
|
||||
space.value = s;
|
||||
form.value = { name: s.name, description: s.description, visibility: s.visibility, defaultRole: s.defaultRole };
|
||||
members.value = await api.spaces.members(props.id);
|
||||
spaceGroups.value = await api.spaces.groups(props.id);
|
||||
if (spaceAtLeast(s.role, "admin")) allGroups.value = await api.workspaces.groups(s.workspace).catch(() => []);
|
||||
} catch {
|
||||
notFound.value = !space.value;
|
||||
}
|
||||
@@ -86,6 +104,13 @@ async function addMember() {
|
||||
if (!errors.message.value && !Object.keys(errors.fields.value).length) email.value = "";
|
||||
}
|
||||
|
||||
const setGroupRole = (g: SpaceGroup, role: SpaceRole) => run(() => api.spaces.setGroup(props.id, g.group.id, role));
|
||||
const removeGroup = (g: SpaceGroup) => run(() => api.spaces.removeGroup(props.id, g.group.id));
|
||||
async function addGroup() {
|
||||
await run(() => api.spaces.setGroup(props.id, newGroup.value, newGroupRole.value));
|
||||
newGroup.value = "";
|
||||
}
|
||||
|
||||
async function removeSpace() {
|
||||
const s = space.value;
|
||||
if (!s || !confirm(t("space.confirmDelete", { name: s.name }))) return;
|
||||
@@ -205,6 +230,52 @@ async function removeSpace() {
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section v-if="canManage || spaceGroups.length" class="dw-card">
|
||||
<h2 class="text-ui-lg">{{ $t("groups.title") }}</h2>
|
||||
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("groups.spaceHint") }}</p>
|
||||
|
||||
<form v-if="canManage && addableGroups.length" class="mt-4 flex flex-wrap gap-2" @submit.prevent="addGroup">
|
||||
<select v-model="newGroup" required :aria-label="$t('groups.pick')" class="dw-input min-w-48 flex-1">
|
||||
<option value="" disabled>{{ $t("groups.pick") }}</option>
|
||||
<option v-for="g in addableGroups" :key="g.id" :value="g.id">{{ g.name }}</option>
|
||||
</select>
|
||||
<select v-model="newGroupRole" :aria-label="$t('workspace.role')" class="dw-input">
|
||||
<option v-for="r in roles" :key="r" :value="r">{{ $t(`space.roles.${r}`) }}</option>
|
||||
</select>
|
||||
<DwButton type="submit" :disabled="busy || !newGroup">{{ $t("workspace.add") }}</DwButton>
|
||||
</form>
|
||||
<p v-else-if="canManage && !allGroups.length" class="mt-3 text-ui-sm text-ink-muted">
|
||||
{{ $t("groups.noneToAdd") }}
|
||||
</p>
|
||||
|
||||
<ul class="mt-4 divide-y divide-line">
|
||||
<li
|
||||
v-for="g in spaceGroups"
|
||||
:key="g.group.id"
|
||||
class="flex flex-wrap items-center justify-between gap-2 py-2"
|
||||
data-testid="space-group"
|
||||
>
|
||||
<p class="min-w-0 truncate text-ui-md font-medium">{{ g.group.name }}</p>
|
||||
<div class="flex items-center gap-3 text-ui-md">
|
||||
<select
|
||||
v-if="canManage"
|
||||
:value="g.role"
|
||||
:aria-label="$t('workspace.role')"
|
||||
class="dw-input"
|
||||
:disabled="busy"
|
||||
@change="setGroupRole(g, ($event.target as HTMLSelectElement).value as SpaceRole)"
|
||||
>
|
||||
<option v-for="r in roles" :key="r" :value="r">{{ $t(`space.roles.${r}`) }}</option>
|
||||
</select>
|
||||
<span v-else class="text-ink-muted">{{ $t(`space.roles.${g.role}`) }}</span>
|
||||
<button v-if="canManage" class="text-danger hover:underline" :disabled="busy" @click="removeGroup(g)">
|
||||
{{ $t("workspace.remove") }}
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section v-if="canDelete" class="dw-card">
|
||||
<h2 class="text-ui-lg">{{ $t("org.dangerTitle") }}</h2>
|
||||
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("space.deleteHint") }}</p>
|
||||
|
||||
@@ -7,6 +7,7 @@ import { useI18n } from "vue-i18n";
|
||||
import { useRouter } from "vue-router";
|
||||
import { useApi } from "../../api";
|
||||
import PageShell from "../../components/PageShell.vue";
|
||||
import WorkspaceGroups from "../../components/WorkspaceGroups.vue";
|
||||
import { useFormErrors } from "../../composables/useFormErrors";
|
||||
import { useAuthStore } from "../../stores/auth";
|
||||
import { atLeast, grantableRoles, useWorkspacesStore } from "../../stores/workspaces";
|
||||
@@ -229,6 +230,8 @@ async function removeWorkspace() {
|
||||
</template>
|
||||
</section>
|
||||
|
||||
<WorkspaceGroups v-if="atLeast(myRole, 'member')" :workspace="ws.id" :can-manage="canManage" />
|
||||
|
||||
<section v-if="atLeast(myRole, 'owner')" class="dw-card">
|
||||
<h2 class="text-ui-lg">{{ $t("org.dangerTitle") }}</h2>
|
||||
<p class="mt-1 text-ui-sm text-ink-muted">{{ $t("workspace.deleteHint") }}</p>
|
||||
|
||||
@@ -4,6 +4,8 @@ import tailwindcss from "@tailwindcss/vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
import { defineConfig } from "vitest/config";
|
||||
|
||||
// The config runs in Node, but this tsconfig has no Node types.
|
||||
declare const process: { env: Record<string, string | undefined> };
|
||||
const api = process.env.DOCWELL_DEV_API ?? "http://localhost:8080";
|
||||
|
||||
export default defineConfig({
|
||||
|
||||
@@ -1042,6 +1042,173 @@ paths:
|
||||
"204": { description: Sent }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
# Groups hold some of a workspace's members; spaces grant roles to groups
|
||||
# like to people, and a member gets the highest of the two.
|
||||
/api/v1/workspaces/{id}/groups:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
get:
|
||||
operationId: listGroups
|
||||
tags: [groups]
|
||||
summary: A workspace's groups (members and above)
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: array, items: { $ref: "#/components/schemas/Group" } }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
post:
|
||||
operationId: createGroup
|
||||
tags: [groups]
|
||||
summary: Create a group (workspace admin)
|
||||
security: [{ bearer: [] }]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/GroupRequest" }
|
||||
responses:
|
||||
"201":
|
||||
description: Created
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/Group" }
|
||||
"400": { $ref: "#/components/responses/Error" }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"409": { $ref: "#/components/responses/Error" }
|
||||
/api/v1/groups/{id}:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
patch:
|
||||
operationId: updateGroup
|
||||
tags: [groups]
|
||||
summary: Rename a group or change its description (workspace admin)
|
||||
security: [{ bearer: [] }]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/GroupRequest" }
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/Group" }
|
||||
"400": { $ref: "#/components/responses/Error" }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
delete:
|
||||
operationId: deleteGroup
|
||||
tags: [groups]
|
||||
summary: Delete a group and its space grants (workspace admin)
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"204": { description: Deleted }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
/api/v1/groups/{id}/members:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
get:
|
||||
operationId: listGroupMembers
|
||||
tags: [groups]
|
||||
summary: A group's members
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: array, items: { $ref: "#/components/schemas/MemberUser" } }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
put:
|
||||
operationId: addGroupMember
|
||||
tags: [groups]
|
||||
summary: Add a workspace member to a group by email (workspace admin)
|
||||
security: [{ bearer: [] }]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/EmailRequest" }
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/MemberUser" }
|
||||
"400": { $ref: "#/components/responses/Error" }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
/api/v1/groups/{id}/members/{userId}:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
- { name: userId, in: path, required: true, schema: { type: string } }
|
||||
delete:
|
||||
operationId: removeGroupMember
|
||||
tags: [groups]
|
||||
summary: Remove someone from a group (workspace admin)
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"204": { description: Removed }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
/api/v1/spaces/{id}/groups:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
get:
|
||||
operationId: listSpaceGroups
|
||||
tags: [spaces]
|
||||
summary: Groups with a role in the space
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { type: array, items: { $ref: "#/components/schemas/SpaceGroup" } }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
put:
|
||||
operationId: setSpaceGroup
|
||||
tags: [spaces]
|
||||
summary: Grant a group a role in the space (space admin)
|
||||
security: [{ bearer: [] }]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
required: [group, role]
|
||||
properties:
|
||||
group: { type: string }
|
||||
role: { $ref: "#/components/schemas/SpaceRole" }
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema: { $ref: "#/components/schemas/SpaceGroup" }
|
||||
"400": { $ref: "#/components/responses/Error" }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
/api/v1/spaces/{id}/groups/{groupId}:
|
||||
parameters:
|
||||
- { name: id, in: path, required: true, schema: { type: string } }
|
||||
- { name: groupId, in: path, required: true, schema: { type: string } }
|
||||
delete:
|
||||
operationId: removeSpaceGroup
|
||||
tags: [spaces]
|
||||
summary: Take a group's role in the space away (space admin)
|
||||
security: [{ bearer: [] }]
|
||||
responses:
|
||||
"204": { description: Removed }
|
||||
"403": { $ref: "#/components/responses/Error" }
|
||||
"404": { $ref: "#/components/responses/Error" }
|
||||
components:
|
||||
securitySchemes:
|
||||
bearer:
|
||||
@@ -1408,3 +1575,36 @@ components:
|
||||
invitedBy: { type: string }
|
||||
expires: { type: string }
|
||||
hasAccount: { type: boolean, description: Sign in to accept instead of signing up }
|
||||
MemberUser:
|
||||
type: object
|
||||
required: [id, email, name]
|
||||
properties:
|
||||
id: { type: string }
|
||||
email: { type: string }
|
||||
name: { type: string }
|
||||
Group:
|
||||
type: object
|
||||
required: [id, workspace, name, description, members, created]
|
||||
properties:
|
||||
id: { type: string }
|
||||
workspace: { type: string }
|
||||
name: { type: string }
|
||||
description: { type: string }
|
||||
members: { type: integer }
|
||||
created: { type: string }
|
||||
GroupRequest:
|
||||
type: object
|
||||
properties:
|
||||
name: { type: string, maxLength: 120 }
|
||||
description: { type: string, maxLength: 500 }
|
||||
SpaceGroup:
|
||||
type: object
|
||||
required: [group, role]
|
||||
properties:
|
||||
group:
|
||||
type: object
|
||||
required: [id, name]
|
||||
properties:
|
||||
id: { type: string }
|
||||
name: { type: string }
|
||||
role: { $ref: "#/components/schemas/SpaceRole" }
|
||||
Reference in new issue
Block a user