我正在为每个用户创建用户安全策略。每个用户安全策略都包含一个正则表达式语句。
每个网页都有自己唯一的ID,格式为0到100000.当用户访问网页时,系统会根据用正则表达式表达的用户安全策略检查网页的ID。
例如,除了ID号为2,54, 109到2001 和10521的网页外,用户可以访问所有网页(ID范围为1 - 100000)。我怎样才能写出高效的网页?正则表达式来解决这个问题?
答案 0 :(得分:2)
你会发现这既是维护的程序化噩梦,也是完成任务的重大性能开销。您可能会发现使用列表更有效(而且不那么繁琐)。您可以将这些列表存储在数据库中,甚至可以使用与RESTful api和json进行通信的云服务提供商。
一个用php编写的例子(主要是伪代码),假设已从数据存储中检索到权限:
//user1 is logged in and has access to the following array of allowed pages:
$loggedInUserPerms = array(1,6,99,821,983255);
if (in_array($pageID, $loggedInUserPerms))
{
//the logged in user has access to this page
}
else
{
//the logged in user doesn't, display access denied error
}
您甚至可以扩展此原则并使用多维数组:
$loggedInUserPerms = array(
1=>array("read"),
6=>array("read","write"),
9=>array("read","write"),
821=>array("read","write","delete"),
983255=>array("read")
);
if (in_array($pageID, $loggedInUserPerms))
{
//the logged in user has access to this page
//you can now handle the sub arrays as well
//to determine what level of access the user has.
}
else
{
//the logged in user doesn't, display access denied error
}