在mongodb上构建多权限组的最佳方法是什么



我启动了一个复杂的项目管理应用程序,我面临着为不同类型的用户配置文件构建资源权限管理的挑战。

我的挑战是:

用户故事

  • John是一个拥有通用用户配置文件的用户
  • John在应用程序中创建了一个项目
  • John创建了几个任务并将它们添加到项目中
  • John添加了一个负责每个任务的用户
  • 添加的用户必须有权访问他们所添加的项目和任务
  • John创建了一个特定任务,并将其作为子任务添加到项目的一个任务中
  • 在这个子任务中,John自动添加了一个用户作为负责人,该用户必须有权访问子任务、任务和项目
  • 在任何时候,John都可以限制对项目资源的访问,例如定义特定用户只能查看任务

我开始的方式。我为每个用例创建了一个规范模式,在其中我通知变量,它返回一个正确或错误的答案。

然而,我必须要求每种资源,在我看来,这不是表演性的。我提到的是最简单的情况之一,还有其他更复杂的情况。


canEditTaskOnProject(): boolean {
if (!this.project) {
console.error(
`Project not provided on ${TaskPermission.name}.${this.canEditTaskOnProject.name}`
);
return false;
}
return new ProjectLeader(this.project, this.userId)
.or(new Creator(this.task, this.userId))
.or(new FullAccessTaskPermission(this.project, this.userId))
.or(new TaskResponsible(this.task, this.userId))
.or(
new RestrictTaskPermission(this.project, this.userId).and(
new Creator(this.task, this.userId).or(
new TaskResponsible(this.task, this.userId)
)
)
)
.or(
new ReadAndWriteTaskPermission(this.project, this.userId).and(
new TaskResponsible(this.task, this.userId)
)
)
.isSatisfiedBy(this.userId);
}

我非常希望那些已经做过类似事情的有经验的人提出建议。我是这个领域的初学者,在我工作的公司里,没有前辈。

提前谢谢!

我找到了使用casl:的最佳方法

import { defineAbility } from '@casl/ability';
export default defineAbility((can, cannot) => {
can('read', 'Article'); 
cannot('read', 'Article', { published: false }); // inverted rule
});

相关内容

最新更新