简体   繁体   English

根据 cube.js 中的分层用户 ID 动态过滤数据

[英]Filter data dynamically based on a hierarchical user id in cube.js

I have two tables called Writers and Publications where there is a foreign key publications.writer_id = writers.id我有两个名为 Writers 和 Publications 的表,其中有一个外键publications.writer_id = writers.id

Writers作家

id (int)编号(整数) parent_id (int) parent_id(整数) role (varchar)角色(可变字符) name (varchar)名称(可变字符) path (ltree)路径(ltree)
1 1个 ADMIN行政 Firstname Lastname名字 姓氏
2 2个 1 1个 EDITOR编辑 Anon Anon匿名 匿名 1.2 1.2
3 3个 2 2个 WEB EDITOR WEB 编辑 Maisy Tickles Maisy 挠痒痒 1.2.3 1.2.3
4 4个 2 2个 WEB EDITOR WEB 编辑 Jack Beanstalk杰克豆茎 1.2.4 1.2.4
5 5个 3 3个 WEB PROOFREADER WEB 校对员 Sunny Ray晴天雷 1.2.3.5 1.2.3.5

Publications刊物

id (int)编号(整数) writer_id (FK) writer_id (FK) publication_name (varchar) publication_name (varchar) word_length (int)字长(整数) published (datetime)发布(日期时间)
1 1个 2 2个 My First Magazine我的第一本杂志 6000 6000 2019-09-09 09:00:00 2019-09-09 09:00:00
2 2个 2 2个 My Second Magazine我的第二本杂志 6000 6000 2019-09-16 09:00:00 2019-09-16 09:00:00
3 3个 3 3个 My First Article我的第一篇文章 1000 1000 2019-09-23 09:00:00 2019-09-23 09:00:00
4 4个 4 4个 My First Article我的第一篇文章 1500 1500 2019-09-23 09:00:00 2019-09-23 09:00:00
5 5个 4 4个 My Second Article我的第二篇 600 600 2019-10-01 09:00:00 2019-10-01 09:00:00
6 6个 5 5个 My First Piece我的第一件作品 600 600 2020-10-01 09:00:00 2020-10-01 09:00:00

I want to do a proof of concept in cube.js Developer Playground to show various charts.我想在cube.js Developer Playground 中进行概念验证以显示各种图表。 Is it possible to filter dynamically based on the user_id so that they can only access content that is equal to or in their subtree ie是否可以根据 user_id 动态过滤,以便他们只能访问等于或在其子树中的内容,即

  • If an ADMIN/EDITOR is using, they can see all the publications如果管理员/编辑正在使用,他们可以看到所有的出版物
  • If the WEB EDITOR ( writers.id=4 ) is using the application, they can only see their own articles ( publications.id in (4,5) )如果 WEB EDITOR ( writers.id writers.id=4 ) 正在使用该应用程序,他们只能看到自己的文章 ( publications.id in (4,5) )
  • If the WEB EDITOR ( writers.id=3 ) is using the application, they can see their publication and the WEB PROOFREADER's one ( publications.id in (3,6) )如果 WEB EDITOR ( writers.id writers.id=3 ) 正在使用该应用程序,他们可以看到他们的出版物和 WEB PROOFREADER 的出版物 ( publications.id in (3,6) )
  • The WEB PROOFREADER should only see their publication ( publications.id=6 ) WEB PROOFREADER 应该只能看到他们的出版物 ( publications.id=6 )

These are the models I have set up so far这些是我到目前为止建立的模型

cube(`Writers`, {
  sql: `SELECT * FROM public.writers`,
  
  preAggregations: {},
  
  joins: {

    Publications:{
      sql: `${CUBE}.id = ${Publication}.writer_id`,
      relationship: `hasMany`
    }
    
  },
  
  measures: {
    count: {
      type: `count`,
      drillMembers: [id, name, created]
    }
  },
  
  dimensions: {
    id: {
      sql: `id`,
      type: `number`,
      primaryKey: true
    },

 role: {
          sql: `role`,
          type: `string`,
        },
};





cube(`Publications`, {
  sql: `SELECT * FROM public.publications`,
  
  preAggregations: {},
  
  joins: {

    Writer:{
      sql: `${CUBE}.writer_id = ${Writers}.id`,
      relationship: `hasOne`
    }
    
  },
  
  measures: {
    count: {
      type: `count`,
      drillMembers: [id, name, created]
    }
  },

 dimensions: {
     id: {
          sql: `id`,
          type: `number`,
          primaryKey: true
        },

     wordLength: {
              sql: `word_length`,
              type: `number`,
            },
};

I know there are filters and segments but these appear to be static. Is there any way to pass a writer_id to filter the relevant data dynamically?我知道有filterssegments ,但这些似乎是 static。有什么方法可以传递 writer_id 来动态过滤相关数据吗? (I have no previous knowledge of JS) (我以前对JS一无所知)

I think you can query the cube from your front-end application with a filter of id.我认为您可以使用 id 过滤器从前端应用程序查询多维数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM