【问题标题】:Row level security + Switch statement行级安全性 + Switch 语句
【发布时间】:2018-10-12 06:52:02
【问题描述】:

我有一个包含大量数据的大表,需要一些 RLS 安全性。

RLS 基于另一个表(用户使用个人资料编号登录)。

我必须根据个人资料编号制定不同的过滤逻辑...

假设如果 userProfile 为 1,他可以看到所有数据。 如果用户配置文件是 2,他只能看到基于 colA 的数据,如果他是 3,则必须检查 colB

例子:

Profile | login               Data | colA |  colB
   2    |  toto               data | toto |  tutu
   3    |  tutu               data | tata |  tutu

我尝试基于profileType 创建一个Switch 语句,但它不起作用。我不知道我们是否可以在 switch 中返回过滤。

我的尝试:

CREATE FUNCTION [dbo].[fn_rls_users](@username AS VARCHAR(50))
RETURNS TABLE 
with schemabinding
AS 
RETURN (
    SELECT Department,ProfileType, 
        CASE 
           WHEN ProfileType = 1 THEN 
           RETURN (
              SELECT 1 AS [fn_rls_users] 
              FROM BIG_TABLE
           )
           WHEN ProfileType = 2 THEN 
           RETURN (
              SELECT 1 AS [fn_rls_users] 
              FROM BIG_TABLE
              WHERE Department = Department
           )
           ELSE (
              SELECT 0 AS [fn_rls_users] 
              FROM BIG_TABLE
           )
        END
    FROM dbo.UserProfiles WHERE UserLogin = @username
)

GO

任何帮助表示赞赏

【问题讨论】:

    标签: sql sql-server-2016 row-level-security


    【解决方案1】:
    DECLARE @ProfileType date = (SELECT ProfileType FROM dbo.UserProfiles WHERE UserLogin = @username)
    
    if @ProfileType = 1
    select isnull(Data, 'NaN') from BIG_TABLE
    
    else if @ProfileType = 2
    select isnull(Data, 'NaN') from BIG_TABLE where colA = @username
    
    else
    select isnull(Data, 'NaN') from BIG_TABLE where colB = @username
    
    End
    

    或者,如果你喜欢使用 str exec

    DECLARE @ProfileType date = (SELECT ProfileType FROM dbo.UserProfiles WHERE UserLogin = @username)
    
    declare @str_sql varchar(1000)
    
    set @str_sql = 'select isnull(Data, "NaN") from BIG_TABLE'
    
    if @ProfileType = 2
    set @str_sql += ' where colA = ' + @username
    
    else if @ProfileType = 3
    set @str_sql += ' where colB = ' + @username
    exec(@str_sql)
    

    【讨论】:

    • 谢谢,您的版本出错了。 No column was specified for column 3 of fn_rls_users
    • @Alexis 我想我误解了你的问题,等等,我只是根据你的例子再回答一次。
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2021-02-02
    • 2014-10-02
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    相关资源
    最近更新 更多