如何在Drupal中正确使用user_access($string, $account)?
我想限制某些网址的使用。假设 node/add
和 node/7
(只是随机示例)。我认为最好的方法是使用 user_access
函数。
但由于我们已经习惯了,Drupal 文档并没有多大帮助。当我刚刚使用该函数时,我收到该函数已在使用中的消息。所以我最好的猜测是在自定义模块的自定义函数中使用这个现有函数和我自己的参数。
但这样我需要在加载之前捕获页面。或者我在这里遗漏了一些东西?
编辑:
我已经设置了这个
global $user;
$items['node/add/%']['access callback'] = array('_mymodule_node_access');
$items['node/add/%']['access arguments'] = array(0,2, $user);
,但由于某种原因,Drupal 没有为所有类型选择 %
卡。它仅适用于一种类型(脚本)。其他术语(例如 page 或 fiche)不会被选取... %
是 Drupal 通配符,对吗?
编辑:
我刚刚发现数据库中已经有一些路径。我怎样才能覆盖它们?我需要的是一个选择器,它可以选择所有四种内容类型(文件、页面、脚本和新闻项目)。
I would like to limit the use of some url's. Let's say node/add
and node/7
(just random examples). I'm thinking the best way to do this is to use the user_access
function.
But as we are used to it, the Drupal documentation doesn't help much. When I just use the function, I get the message the function is already in use. So my best guess is to use this existing function with my own arguments in my custom function in my custom module.
But in this way I need to catch the page before loading it. Or I'm I missing something here?
EDIT:
I've set this
global $user;
$items['node/add/%']['access callback'] = array('_mymodule_node_access');
$items['node/add/%']['access arguments'] = array(0,2, $user);
But for some reason, Drupal isn't picking up the %
card for all types. It's just working for one type (script). Other terms like page or fiche aren't getting picked up... %
is a Drupal wildcard right?
EDIT:
I just found out there are already some paths in the database. How can I overwrite them? What I need is one selector which can select all four content types (fiche, page, script and news-item).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为路径定义特定访问函数的方法是在
hook_menu()
中为路径的菜单项设置访问回调
。这对于现有路径略有不同,因为您需要实现hook_menu_alter()
来编辑该路径的现有访问回调
:当我们“我们谈论的是节点页面,因为它们的菜单项是使用通配符
node/%
定义的。这意味着使用hook_menu_alter()
您只能更改所有节点的访问回调。幸运的是,Drupal 有一个
hook_node_access
钩子来救援:希望有帮助
编辑
如果这一切看起来有点麻烦,你可能会在安装路径访问模块,我认为它具有您想要的功能。
另一个编辑
我认为在这种情况下覆盖通配符不起作用的原因是因为节点模块显式定义了每个节点类型的路径,例如
node/ add/page
、node/add/article
等。因为 Drupal 将采用精确匹配 (node/add/page
) 而不是通配符匹配 (<代码>节点/添加/%)您实际上覆盖了错误的菜单项。尝试在
hook_menu_alter()
函数中显式指定路径(请注意,访问回调
应该是一个字符串,而不是您当前拥有的数组):还值得注意的是您传递的
$user
对象始终是清除 Drupal 缓存的登录用户的用户对象(因为重建缓存时会重建菜单项)。如果您希望传递当前登录用户(即访问页面时登录的用户),那完全是另一回事......我建议就此提出另一个问题因为这可能是一个棘手的问题,并且您希望从这里的人们那里获得尽可能多的意见。The way to define a particular access function for a path is to set the
access callback
for the path's menu item inhook_menu()
. This is slightly different for existing paths, in that you need to implementhook_menu_alter()
to edit the existingaccess callback
for that path:This gets a bit more fun when we're talking about node pages as their menu items is defined using a wildcard
node/%
. This means that usinghook_menu_alter()
you can only change the access callback for all nodes.Fortunately Drupal has a
hook_node_access
hook to come to the rescue:Hope that helps
EDIT
If that all seems like a bit much hassle you might get some joy installing the Path Access module, I think it has the functionality you're after.
ANOTHER EDIT
I think the reason overriding the wildcard isn't working in this case is because the node module explicitly defines a path for each node type, e.g.
node/add/page
,node/add/article
, etc. Because Drupal will take an exact match (node/add/page
) over a wildcard match (node/add/%
) you're actually overriding the wrong menu item.Try specifying the path explicitly in your
hook_menu_alter()
function (note that theaccess callback
should be a string and not an array as you currently have):It's also worth noting that the
$user
object you're passing will always be the user object of the logged in user who cleared Drupal's caches (since menu items are rebuilt when the cache is rebuilt). If you're looking to pass the current logged in user (i.e. the one logged in at the time the page is accessed) that's a different thing altogether...I'd advise asking another question on it as it can be a tricky bugger and you want to get as much input as possible from people on here.