【问题标题】:How to use Node-Casbin in Angular?如何在 Angular 中使用 Node-Casbin?
【发布时间】:2020-10-08 17:10:29
【问题描述】:

有一个关于 Node-Casbin 库的问题。我想在前端的 Angular 中使用这个库。不幸的是,我不知道我该怎么做。我想将此库与字符串一起使用。所以我想将策略和数据声明为字符串,而不是文件。

  1. 使用的角度:10.0.0
  2. 使用 Casbin 版本:5.1.6

我对编码所做的一切: 在 package.json 我添加了这段代码:

  "browser": {
    "fs": false,
    "path": false,
    "os": false
  },

我在服务中添加了如下代码:

  async loadEnforcer() {
    const enforcer = (await new Enforcer().initWithString(
      this.policy,
      this.dataSet
    )) as any;
    const sub = 'alice'; // the user that wants to access a resource.
    const obj = '/alice_data/'; // the resource that is going to be accessed.
    const act = 'GET'; // the operation that the user performs on the resource.

    console.log(enforcer);

    console.log(
      'the user permission is : ' + enforcer.getPermissionsForUser('alice')
    );

    if (enforcer.enforce(sub, obj, act) === true) {
      console.log('permit alice to read data1');
    } else {
      console.log('deny the request, show an error');
    }
  }

但我仍然收到来自 Angular 的错误,例如:

core.js:4197 ERROR Error: Uncaught (in promise): TypeError: fs_1.readFileSync is not a function
TypeError: fs_1.readFileSync is not a function
    at Config.parse (config.js:64)
    at Function.newConfig (config.js:29)
    at Model.loadModel (model.js:109)
    at Object.newModel (model.js:291)
    at Enforcer.<anonymous> (enforcer.js:62)
    at Generator.next (<anonymous>)
    at enforcer.js:21
    at new ZoneAwarePromise (zone-evergreen.js:960)
    at push.../../node_modules/casbin/lib/enforcer.js.__awaiter (enforcer.js:17)
    at Enforcer.initWithAdapter (enforcer.js:61)
    at resolvePromise (zone-evergreen.js:798)
    at zone-evergreen.js:705
    at rejected (tslib.es6.js:72)
    at ZoneDelegate.invoke (zone-evergreen.js:364)
    at Object.onInvoke (core.js:27437)
    at ZoneDelegate.invoke (zone-evergreen.js:363)
    at Zone.run (zone-evergreen.js:123)
    at zone-evergreen.js:857
    at ZoneDelegate.invokeTask (zone-evergreen.js:399)
    at Object.onInvokeTask (core.js:27425)

我尝试以与 https://github.com/kowthalganesh/casbin-angular/blob/master/src/app/app.component.ts 相同的方式进行操作 - 但我仍然收到错误。

你能帮我吗? 非常感谢!

【问题讨论】:

标签: javascript angular compiler-errors casbin


【解决方案1】:

您永远不应该在客户端包含这样的逻辑。除此之外,您在浏览器中没有文件系统(库似乎依赖于该文件系统),在前端包含此类内容将是一个高安全性问题。

如果您确实需要在应用程序中包含此类功能,则应仅将其放在后端。

【讨论】:

    猜你喜欢
    • 2020-05-06
    • 2016-10-28
    • 2023-03-23
    • 1970-01-01
    • 2020-02-05
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    相关资源
    最近更新 更多