【问题标题】:what is the best way to build multiple permission group on mongodb?在 mongodb 上构建多个权限组的最佳方法是什么?
【发布时间】:2021-04-23 17:08:56
【问题描述】:

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

我的挑战是:

用户故事

  • 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);
  }

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

提前谢谢你!

【问题讨论】:

    标签: node.js mongodb typescript mongoose-schema user-permissions


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2012-01-21
      • 2014-12-26
      • 2011-09-03
      • 2021-03-16
      • 2017-04-21
      相关资源
      最近更新 更多