Beautiful Open-Source Form Builder
Update UserInviteController authorization and enhance UserInvite mode… (#1124)
Chirag Chhatralaf6aaf992d690
diff --git a/api/app/Http/Controllers/Auth/UserInviteController.php b/api/app/Http/Controllers/Auth/UserInviteController.phpindex 749dfd0f4..7daab5dea 100644--- a/api/app/Http/Controllers/Auth/UserInviteController.php+++ b/api/app/Http/Controllers/Auth/UserInviteController.php@@ -17,7 +17,7 @@ class UserInviteController extends Controllerpublic function listInvites(Request $request, Workspace $workspace){- $this->authorize('view', $workspace);+ $this->authorize('adminAction', $workspace);return (new WorkspaceHelper($workspace))->getAllInvites();}diff --git a/api/app/Models/UserInvite.php b/api/app/Models/UserInvite.phpindex dc6f94457..562d185c3 100644--- a/api/app/Models/UserInvite.php+++ b/api/app/Models/UserInvite.php@@ -17,6 +17,10 @@ class UserInvite extends Modelpublic const PENDING_STATUS = 'pending';public const ACCEPTED_STATUS = 'accepted';+ protected $hidden = [+ 'token',+ ];+protected $fillable = ['email','role',diff --git a/api/tests/Feature/Webhook/UserInviteSecurityTest.php b/api/tests/Feature/Webhook/UserInviteSecurityTest.phpnew file mode 100644index 000000000..50591c6c2--- /dev/null+++ b/api/tests/Feature/Webhook/UserInviteSecurityTest.php@@ -0,0 +1,117 @@+<?php++use App\Models\UserInvite;+use Laravel\Sanctum\Sanctum;++describe('Invite listing authorization', function () {+ it('allows admin to list invites', function () {+ $admin = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertSuccessful();+ });++ it('denies non-admin user role from listing invites', function () {+ $admin = $this->createProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $member = $this->createUser();+ $workspace->users()->attach($member, ['role' => 'user']);+ $this->actingAsUser($member);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertStatus(403);+ });++ it('denies readonly role from listing invites', function () {+ $admin = $this->createProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $readonly = $this->createUser();+ $workspace->users()->attach($readonly, ['role' => 'readonly']);+ $this->actingAsUser($readonly);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertStatus(403);+ });++ it('allows admin token with workspaces-write ability to list invites', function () {+ $admin = $this->createProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ Sanctum::actingAs($admin, ['workspaces-write']);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertSuccessful();+ });++ it('denies admin token with workspace-users-read ability from listing invites', function () {+ $admin = $this->createProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ Sanctum::actingAs($admin, ['workspace-users-read']);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertStatus(403);+ });++ it('denies non-admin token with workspaces-write ability from listing invites', function () {+ $admin = $this->createProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $member = $this->createUser();+ $workspace->users()->attach($member, ['role' => 'user']);+ Sanctum::actingAs($member, ['workspaces-write']);++ $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertStatus(403);+ });+});++describe('Invite token redaction', function () {+ it('does not expose token in invite listing', function () {+ $admin = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $response = $this->getJson(route('open.workspaces.invites.index', $workspace))+ ->assertSuccessful();++ $invites = $response->json();+ expect($invites)->toHaveCount(1);+ expect($invites[0])->toHaveKeys(['id', 'email', 'status']);+ expect($invites[0])->not->toHaveKey('token');+ });++ it('does not expose token when creating an invite', function () {+ $admin = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($admin);++ $response = $this->postJson(route('open.workspaces.users.add', $workspace), [+ 'email' => '[email protected]',+ 'role' => 'user',+ ])->assertSuccessful();++ $data = $response->json();+ if (isset($data['invite'])) {+ expect($data['invite'])->not->toHaveKey('token');+ }+ });++ it('still stores the token in the database', function () {+ $admin = $this->actingAsProUser();+ $workspace = $this->createUserWorkspace($admin);+ UserInvite::inviteUser('[email protected]', 'user', $workspace);++ $invite = UserInvite::latest()->first();+ expect($invite->token)->not->toBeNull();+ expect(strlen($invite->token))->toBe(100);+ });+});diff --git a/client/components/workspaces/settings/Members.vue b/client/components/workspaces/settings/Members.vueindex ba1757a36..0619346b9 100644--- a/client/components/workspaces/settings/Members.vue+++ b/client/components/workspaces/settings/Members.vue@@ -10,7 +10,7 @@<div class="flex shrink-0 items-center gap-2"><UButton- v-if="!workspace.is_readonly"+ v-if="canManageMembers"label="Invite User"icon="i-heroicons-user-plus-20-solid":loading="isLoadingData"@@ -126,6 +126,7 @@</UModal><WorkspacesSettingsInviteUser+ v-if="canManageMembers"v-model="showInviteUserModal"@user-added="handleUserAdded"/>@@ -151,6 +152,7 @@ const auth = useAuth()// Get current userconst { data: user } = auth.user()+const canManageMembers = computed(() => workspace?.value?.is_admin ?? false)// Reactive stateconst showEditUserModal = ref(false)@@ -174,10 +176,12 @@ const columnPinning = ref({// Get workspace users and invites reactivelyconst { data: workspaceUsers, isLoading: isLoadingUsers } = users(workspaceId)-const { data: workspaceInvites, isLoading: isLoadingInvites } = invites(workspaceId)+const { data: workspaceInvites, isLoading: isLoadingInvites } = invites(workspaceId, {+ enabled: computed(() => !!workspaceId.value && canManageMembers.value)+})// Combined loading state-const isLoadingData = computed(() => (isLoadingUsers?.value || isLoadingInvites?.value) ?? false)+const isLoadingData = computed(() => (isLoadingUsers?.value || (canManageMembers.value && isLoadingInvites?.value)) ?? false)// Transform and combine data reactivelyconst combinedUsers = computed(() => {@@ -212,7 +216,6 @@ const combinedUsers = computed(() => {// Table columns configurationconst tableColumns = computed(() => {- const isAdmin = workspace?.value?.is_admin ?? falsereturn [{id: 'name',@@ -229,7 +232,7 @@ const tableColumns = computed(() => {accessorKey: 'role',header: 'Role'},- ...(isAdmin ? [+ ...(canManageMembers.value ? [{id: 'actions',header: '',@@ -239,13 +242,15 @@ const tableColumns = computed(() => {// User management handlersconst editUser = (user) => {+ if (!canManageMembers.value) return+selectedUser.value = usereditUserForm.role = selectedUser.value.pivot?.role || selectedUser.value.roleshowEditUserModal.value = true}const updateUserRole = () => {- if (!workspaceId.value || !selectedUser.value?.id) return+ if (!workspaceId.value || !selectedUser.value?.id || !canManageMembers.value) returnupdateMutation.mutateAsync({userId: selectedUser.value.id,@@ -259,7 +264,7 @@ const updateUserRole = () => {}const removeUserHandler = (user) => {- if (!workspaceId.value) return+ if (!workspaceId.value || !canManageMembers.value) returnalert.confirm("Do you really want to remove " + user.name + " from this workspace?", () => {removeMutation.mutateAsync(user.id).then(() => {@@ -272,7 +277,7 @@ const removeUserHandler = (user) => {// Invite management handlersconst resendInviteHandler = (invite) => {- if (!workspaceId.value) return+ if (!workspaceId.value || !canManageMembers.value) returnalert.confirm("Do you really want to resend invite email to this user?", () => {resendMutation.mutateAsync(invite.id).then(() => {@@ -284,7 +289,7 @@ const resendInviteHandler = (invite) => {}const cancelInviteHandler = (invite) => {- if (!workspaceId.value) return+ if (!workspaceId.value || !canManageMembers.value) returnalert.confirm("Do you really want to cancel this user's invitation to this workspace?", () => {cancelMutation.mutateAsync(invite.id).then(() => {diff --git a/docs/api-reference/changelog.mdx b/docs/api-reference/changelog.mdxindex 4563b082e..2bcd8e5d2 100644--- a/docs/api-reference/changelog.mdx+++ b/docs/api-reference/changelog.mdx@@ -3,6 +3,22 @@ title: "Changelog"description: "Notable updates to the OpnForm API."---+## 2026-05-27 – Workspace invite listing requires admin access++The workspace invite listing endpoint now requires workspace admin access:++* `GET /open/workspaces/{workspaceId}/invites`++### What changed++- Non-admin workspace members can no longer list pending or accepted workspace invites.+- Personal Access Tokens must include `workspaces-write` and belong to a workspace admin.+- Invite responses do not expose invite tokens.++### Migration guide++If your integration lists workspace invites, use an admin-owned token with the `workspaces-write` ability. Continue using `GET /open/workspaces/{workspaceId}/users` with `workspace-users-read` when you only need accepted workspace members.+## 2025-01-02 – Optimized form list endpointThe form list endpoint now returns **lightweight form summaries** for improved performance:diff --git a/docs/api-reference/openapi.yaml b/docs/api-reference/openapi.yamlindex 4146c94b4..57df43793 100644--- a/docs/api-reference/openapi.yaml+++ b/docs/api-reference/openapi.yaml@@ -278,7 +278,7 @@ paths:tags:- Workspace Userssummary: List Workspace Invites- description: "Requires `workspace-users-read`. Lists pending & accepted invites."+ description: "Requires workspace admin access. Personal Access Tokens must include `workspaces-write` and belong to a workspace admin. Lists pending & accepted invites."security:- bearerAuth: []parameters:diff --git a/docs/api-reference/workspace-users/list-workspace-invites.mdx b/docs/api-reference/workspace-users/list-workspace-invites.mdxindex 85531ad1b..7eda47c62 100644--- a/docs/api-reference/workspace-users/list-workspace-invites.mdx+++ b/docs/api-reference/workspace-users/list-workspace-invites.mdx@@ -8,7 +8,9 @@ Retrieve all pending or accepted invitations for a workspace.## Authentication & Scope-Requires `workspace-users-read` ability. The requester must be a member of the workspace.+Requires workspace admin access.++For Personal Access Tokens, the token must include the `workspaces-write` ability and the token owner must be an admin of the workspace. Non-admin workspace members and tokens with only `workspace-users-read` cannot list invites.## Request@@ -40,4 +42,4 @@ Authorization: Bearer <token>]```-`403 Forbidden` – Token lacks `workspace-users-read` or you lack access.\ No newline at end of file+`403 Forbidden` – You are not a workspace admin, or your token lacks `workspaces-write`.\ No newline at end of file