将ACL转换为平面表达式

时间:2013-12-16 19:38:04

标签: search authorization acl boolean-logic

我想为使用SQL数据库作为存储的Web应用程序创建基于ACL的授权系统。

我的问题是 - 我想使用定义为ACL的授权规则来过滤搜索。为此,我需要将ACL转换为我可以在WHERE子句中使用的平面布尔表达式。

我的ACL系统很简单。每个条目都是ALLOW或DENY,并且会添加一个条件。列表将从上到下进行扫描。匹配条件的第一个条目将适用。所以例如如果我有像这样的ACL:

ALLOW x = 3
ALLOW x = 5
DENY true

我需要使用以下内容进行过滤:x = 3 OR x = 5

如果我有:

DENY x = 3
DENY x = 5
ALLOW true

过滤器将为NOT (x = 3 OR x = 5)

但我仍然不确定如何创建一个适用于任何顺序的DENY和ALLOW规则混合的通用转换方法。但是我们可以假设必须是一个评估为true的规则。我们可以通过ACL继承实现这一点,并将DENY true放在每个列表的末尾。

你能帮我解决这个谜语吗?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。避免之前出现问题的三值逻辑的关键是从列表的末尾开始。算法将如下所示:

group the acl into packs of ALLOW and DENY
discard the last pack (which should be DENY and end with DENY true)
add 'false' to the result
for every pack from the end:
    combine all the conditions with OR
    if pack is ALLOW:
        add "or condition" to the result
    elif pack is DENY:
        add "and not condition" to the result
if result == 'false':
    don't perform search, return empty list
else:
    discard 'false or' from the beginning of the list (redundant)

例如:

DENY x = 7
ALLOW x > 3
DENY true

将成为false OR x > 3 AND NOT (x = 7)