Express Session 中的存储能力?

问题描述

我已经看到了快速示例,其中一个能力通过中间件存储在 req 对象中。然后使用以下方法评估权限:

  _startFilePicker() async {
  InputElement uploadInput = FileUploadInputElement();
  uploadInput.click();
 uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
  final file = files[0];
  FileReader reader = FileReader();
  reader.onLoadEnd.listen((e) {
    print("on load set state");
   return [reader.result,Icons.done,Colors.green[500]];
  });

  reader.onError.listen((fileEvent) {
    print("error");        //"Some Error occured while reading the file";
  });
  reader.readAsArrayBuffer(file);
}});

}
// then pass your value like this...


 renderr() async {
 attachmentOne = _startFilePicker()[0];
 firstAttachment = _startFilePicker()[1];
 firstAttachmentColor = _startFilePicker()[2];
 setState() {};
 }

我想实现类似的目标。我的想法是将能力保存在与 socket io websockets 共享的快速会话中。通过分享Microsoft.Net.Component.4.targetingPack = ForbiddenError.from(req.ability).throwUnlessCan('read',article); 。我的方法如下,我从前端应用程序请求获取规则以更新前端的能力。后端保存express session内的能力:

req.session
socket.handshake.session

然后当 websocket 请求发生时,我想在后端检查用户是否有权执行该操作:

// abilities.js file
import { Ability } from '@casl/ability';

export const defineAbilitiesFor = (rules) => {
  return new Ability(rules);
};

export default defineAbilitiesFor;

但是,这会引发以下错误

// handler for express route to get permissions from the frontend
export const getPermissions = async (req,res) => {
...
  rules.push({
    action: ['view'],subject: views,});
  // manage all own processes
  rules.push({
    action: ['manage'],subject: 'Process',conditions: {
      userId: req.kauth.grant.access_token.content.sub,},});
// store ability in session
  req.session.rules = defineAbilitiesFor(rules);
  const token = jwt.sign({ token: packRules(rules) },'secret');
  if (token) {
    return res.status(200).json(token);
  } else {
    return res.status(400).json('Error');
  }
...

会话对象似乎具有正确的能力对象。当我 console.log ForbiddenError.from(socket.handshake.session.rules).throwUnlessCan('view','Process'); 时,我得到以下输出

TypeError: this.ability.relevantRuleFor is not a function
    at ForbiddenError.throwUnlessCan

还有罐头功能和我尝试过的其他一切都不起作用。我认为将普通规则存储为会话中的对象,然后在每个请求生效之前更新能力类,但我不想这样做。我想将能力直接存储在会话中,这样我只需执行 throwUnlessCan 或 can 函数

这甚至可能吗?如果可能,你会怎么做?

到目前为止谢谢。

解决方法

无需存储整个 #!/bin/sh #SBATCH -N 1 #SBATCH -n 1 #SBATCH --time=0:15:00 ##hh:mm:ss for n in $(seq 0 5) do for m in $(seq 0 5) do for v in $(seq 0 5) do mkdir b$n-k$m-a$v cd b$n-k$m-a$v cp ~/home/b01-k1-a01/* . cat >test.ctrl <<EOF Beta=$n Kappa=$m Alpha=0 $v EOF mpirun soft.x test.ctrl sleep 5 cd .. done done done 实例,您只需存储其规则! rules 是一个普通的 js 对象数组,因此可以轻松序列化。因此,将代码更改为:

Ability

要在其他处理程序中使用 export const getPermissions = async (req,res) => { ... rules.push({ action: ['view'],subject: views,}); // manage all own processes rules.push({ action: ['manage'],subject: 'Process',conditions: { userId: req.kauth.grant.access_token.content.sub,},}); // store ability RULES in session req.session.rules = rules; const token = jwt.sign({ token: packRules(rules) // packRules accepts an array of RawRule! not an Ability instance },'secret'); if (token) { return res.status(200).json(token); } else { return res.status(400).json('Error'); } ,请添加一个中间件:

Ability