设计类似Google Docs的权限在Prisma中如何实现?

huangapple go评论59阅读模式
英文:

Designing Google Docs-Like Permissions in Prisma?

问题

我正在构建一个使用 Prisma 的应用程序,并需要设计数据库,使其具有类似于 Google Docs 的权限架构。

所谓 "类似于 Google Docs",是指该应用程序允许用户访问多个 "项目",要么是因为他们是这些项目的创建者/作者,要么是因为其他用户授权他们访问他们创建的项目。

如果作为用户你可以访问另一个用户的项目,那么你将拥有一定级别的权限,这些权限由该用户授予你。这是应用程序与 Google Docs 模型不同的地方。三种权限级别分别是编辑者、审阅者和查看者。

以下是我 Prisma 模式的一部分...

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  projects      Project[]
}

model Project {
  id        String   @id @default(cuid())
  name      String?
  things    Thing[]
  User      User     @relation(fields: [userId], references: [id])
  userId    String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

我无法弄清楚 UserProject 之间的正确关系,以实现拥有一个 Project、成为一个 Project 的协作者,或两者混合的情况。

英文:

I'm building an application that uses Prisma and need to design the database to have a Google Docs-like permissions architecture.

By "Google Docs-like" I mean that the application allows a user to have access to multiple "Projects", either by being the creator/author of those projects, or by another user giving them access to their created projects.

If as a user you have access to another user's projects, then you have a certain level of permissions granted to you by that user. This is where the application diverges from the Google Docs model. The three permissions levels are Editor, Reviewer and Viewer.

Here's part of my Prisma schema so far...

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  projects      Project[]
}

model Project {
  id        String   @id @default(cuid())
  name      String?
  things    Thing[]
  User      User     @relation(fields: [userId], references: [id])
  userId    String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

What I can't figure out is the correct relation between User and Project to achieve either owning a Project, being a collaborator on a Project or a mix of both.

答案1

得分: 1

你可以使用明确的多对多关系来建模这种关系:

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  userProjects  UserProject[]
}

model Project {
  id        String   @id @default(cuid())
  name      String?
  things    Thing[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  userProjects UserProject[]
}

model UserProject {
  user   User   @relation(fields: [userId], references: [id])
  userId String
  project   Project   @relation(fields: [projectId], references: [id])
  projectId String
  role      Role
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@id([userId, projectId])
}

enum Role {
  EDITOR
  REVIEWER
  VIEWER
}

UserProject 通过 UserProject 模型相互关联。这个模型代表了用户和项目之间的多对多关系。每个 UserProject 代表了用户与项目的关联,它包括一个可以是 EDITORREVIEWERVIEWER 的角色字段。这个模式允许用户成为项目的所有者、协作者或同时担任两者。

英文:

You can use explicit many-to-many relations to model this relationship:

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  userProjects  UserProject[]
}

model Project {
  id        String   @id @default(cuid())
  name      String?
  things    Thing[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  userProjects UserProject[]
}

model UserProject {
  user   User   @relation(fields: [userId], references: [id])
  userId String
  project   Project   @relation(fields: [projectId], references: [id])
  projectId String
  role      Role
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@id([userId, projectId])
}

enum Role {
  EDITOR
  REVIEWER
  VIEWER
}

User and Project are connected through the UserProject model. This model represents the many-to-many relationship between users and projects. Each UserProject represents a user's relationship to a project. It includes a role field that can be EDITOR, REVIEWER, or VIEWER. This schema allows for a user to be the owner of a project, a collaborator on a project, or both.

huangapple
  • 本文由 发表于 2023年7月31日 21:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定