fix(company): enforce workspace membership and roles via CompanyMembership instead of accepted Invitations

オープン
#38 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
postgresql, typescript

調査の方向性

Start with packages/prisma/schema.prisma and the listed methods in apps/api/src/company/company.service.ts; trace how workspace creation, invite acceptance, account listing, switching, role updates, and removal use membership data. Review the migration and existing tests for those flows. Done means CompanyMembership is authoritative, removed members cannot regain access, and demoted roles remain in effect after switching workspaces.

索引モデルが issue の本文から書いたものです。

説明

backend bug database
Problem Description

In apps/api/src/company/company.service.ts, workspace access and roles during multi-workspace switching rely on historical accepted Invitation records:

  1. Removed members can rejoin:
    removeTeamMember clears user.companyId = null and resets user.role = 'VIEWER', but does not delete or revoke the user's accepted Invitation rows. Because getUserAccounts and switchAccount authorize access to any company matching invitations: { some: { email: userEmail, acceptedAt: { not: null } } }, a removed member can still see the workspace and call POST /api/accounts/switch to rejoin.

  2. Demoted members can escalate their role:
    updateTeamMemberRole updates user.role, but leaves the accepted Invitation.role unchanged. When switchAccount runs, it sets targetRole = company.invitations[0].role, allowing a demoted member (e.g., ADMIN -> VIEWER) to restore their original ADMIN role via POST /api/accounts/switch.


Expected Behavior
  1. Workspace membership and per-workspace roles must have a single, authoritative source of truth (CompanyMembership).
  2. Invitation records must only represent pending/consumed invites and must never be used to authorize getUserAccounts or switchAccount or to determine a user's active role.
  3. Removing a member must immediately revoke their workspace access across all endpoints; demoting a member must persist across workspace switches.

Where to Look
  • packages/prisma/schema.prisma (User, Company, Invitation)
  • apps/api/src/company/company.service.ts (getUserAccounts, switchAccount, updateTeamMemberRole, removeTeamMember, acceptInvitation)

Solution / Implementation Guide (Recommended Enterprise Plan)
  1. Add CompanyMembership model (packages/prisma/schema.prisma):

    • Create CompanyMembership with { id, userId, companyId, role, createdAt, updatedAt }, @@unique([userId, companyId]), and onDelete: Cascade relations to User and Company.
    • Backfill existing User.companyId + User.role and valid active Invitation relationships in the Prisma migration.
  2. Use CompanyMembership as the single source of truth (company.service.ts):

    • Accepting an invite / creating a company: Upsert a CompanyMembership row for (userId, companyId, role) when a user creates a workspace or accepts an Invitation.
    • getUserAccounts & switchAccount: Query CompanyMembership for userId (instead of invitations) to list accessible workspaces and resolve targetRole from membership.role.
    • updateTeamMemberRole: Update CompanyMembership.role (and user.role if user.companyId === companyId) inside a transaction.
    • removeTeamMember: Delete the CompanyMembership row for (userId, companyId) (and clear user.companyId / reset user.role if that was their active workspace).
主要言語
TypeScript
スター
2
フォーク
1
平均マージ
6分
マージ済み PR(30日)
27

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

sumamakhan761/tryinstance のほかの issue

sumamakhan761/tryinstance の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。