Drupal 7 hook_node_access 有条件地阻止节点访问
对于所有用户,我需要有条件地阻止对“消息”类型节点的访问。用户能够查看这些消息节点的唯一方法是成功提交表单。
我是这样开始的:
function mymodule_node_access($node, $op, $account) {
if ($op == 'view' && $node->type == 'message') {
return NODE_ACCESS_DENY;
}
}
但是,我想在成功提交表单后允许查看对该类型的各个节点的访问:
function form_submit($form, &$form_state) {
// some logic here
$form_state['redirect'] = 'node/255';
}
因此节点 255 的类型为“消息”,并且我想“解除”此特定节点的 NODE_ACCESS_DENY和这个用户(+在大多数情况下这将是匿名用户)
关于实现此目的的不同方法有什么建议吗?
For all users I need to conditionally block access to nodes of type 'message'. The only way users should be able to view these message nodes is by successfully submitting a form.
I've started like this:
function mymodule_node_access($node, $op, $account) {
if ($op == 'view' && $node->type == 'message') {
return NODE_ACCESS_DENY;
}
}
However, I want to allow view access to individual nodes of this type upon successful submission of form:
function form_submit($form, &$form_state) {
// some logic here
$form_state['redirect'] = 'node/255';
}
so node 255 is of type 'message', and I want to 'lift' the NODE_ACCESS_DENY for this particular node and this user (+ in most cases this will be an anonymous user)
Any suggestions on different ways to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一的方法是在表单提交处理程序中设置一个值,然后由
hook_node_access()
检查该值;您可以使用 Drupal 变量或保存在数据库表中的值。您需要存储访问该表单的用户的用户 ID 以及已提交该表单的每个节点的节点 ID。
假设您使用 Drupal 变量,您可以使用类似于以下代码的代码:
请注意,此代码仅允许用户访问节点一次;如果用户第二次尝试访问同一节点,用户将收到“访问被拒绝”错误。如果不希望这样,那么第二个函数应该重写如下:
我使用 Drupal 变量来编写简单的代码;在这种情况下,如果可以创建该内容类型节点的用户很少,则应该使用 Drupal 变量;如果有很多用户可以创建这些节点,那么使用数据库表会更好。
此外,当使用 Drupal 变量时,Drupal 使用数据库表;不同之处在于该数据库表的内容始终加载到内存中。如果您需要存储大量数据,则不应使用 Drupal 变量。
The only way you can do that is to set a value in the form submission handler that is then checked by
hook_node_access()
; you could use a Drupal variable, or a value saved in a database table.You need to store the user ID of the user that accessed the form, and the node ID of every node for which such form has been submitted.
Supposing you use a Drupal variable, you could use code similar to the following one:
To notice that this code allows a user to access a node only once; if the user would try to access the same node the second time, the user would get an "access denied" error. If that is not desired, then the second function should be re-written as follows:
I used a Drupal variable to write simple code; using a Drupal variable, in this case, should be done if the users that can create nodes of that content type are few; if there are many users who can create those nodes, then using a database table is better.
Also when using Drupal variables, Drupal is using a database table; the difference is that the content of that database table is always loaded in memory. If you need to store many data, you should not use Drupal variables.
修改后的解决方案使用 $_SESSION 因为我主要与匿名用户一起工作:
Modified solution to use $_SESSION as I'm working mostly with anonymous users: