Mondrian Cube主SQL查询动态获取登录用户实现数据过滤
在Mondrian Schema的Cube主查询中动态获取登录用户过滤数据
方法1:直接使用Mondrian内置用户上下文变量
Mondrian自带内置上下文变量${user.name},可以直接在<View>的SQL语句里引用,它会自动替换为当前登录的用户名。修改你的代码如下:
<Cube> <Annotations> </Annotations> <View> <SQL dialect="generic"> select a.* from xxxxx a where a.username = '${user.name}' </SQL> </View> </Cube>
注意:这个变量生效的前提是你的BI工具(如Pentaho、Saiku等)已经正确传递了用户会话信息,确保Mondrian能获取到当前登录用户的名称。
方法2:定义Schema级参数复用
如果需要在Schema的多个地方复用当前用户变量,可以先在Schema根节点定义参数,再在Cube的SQL中引用:
<Schema name="YourSchemaName"> <!-- 定义全局参数,默认值关联内置用户变量 --> <Parameter name="currentUser" type="String" default="${user.name}"/> <Cube> <Annotations> </Annotations> <View> <SQL dialect="generic"> select a.* from xxxxx a where a.username = '${currentUser}' </SQL> </View> </Cube> </Schema>
这种方式更灵活,后续如果需要调整参数来源,只需修改<Parameter>的default值即可。
注意事项
- 若用户名包含单引号等特殊字符,需确保你的BI平台会自动对变量值进行SQL转义,避免注入风险。
- 部分BI工具可能需要额外配置,确保用户认证信息能传递到Mondrian引擎中。
内容的提问来源于stack exchange,提问作者iagos
相关产品推荐
相关产品推荐

