Drupal 用户身份验证检查
我一直在尝试编写一个函数来检查用户是否经过身份验证,然后调用谷歌分析脚本来生成自定义报告。这是一个很大的麻烦,我相信 Drupal 并没有那么困难。有谁可以帮助或承担该项目吗? 这应该是一件简单而直接的事情,但能够检查用户身份验证并不容易
我尝试使用 php 函数来检查用户是否登录,但我无法从中调用 javascript 函数。
我用 jquery 编写了一个脚本函数,在页面加载时调用脚本,但我无法检查用户是否使用 javascript 登录
I have been trying to write a function to check if user is authenticated and then call a google analytics script for custom report. This has been a big hassle and I believe Drupal is not that difficult. Is there anyone who can help or take up the project?
It should be a simple and straight forward thing to do but being able to check user authentication has not been easy
I've tried to use a php function to check if user is logged in but I was not able to call javascript function from it.
I wrote a script function with jquery calling the script on page load but I was not able to check if user is logged in using javascript
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这不是最好的选择,但是...
您如何看待从 JS 函数调用自定义模块的控制器,如果...则返回
true
$currentUser = \Drupal::currentUser(); $uid = $currentUser->id(); 返回大于
0
的数值,如果返回的值为0
,则返回false
代码>(匿名用户)?该控制器只需以 JSON 格式返回当前用户的 UID,然后可以从 JS 中读取。另一种尝试是在主题的 .theme 文件中编写 YOURTHEMENAME_preprocess_page() 函数来实现上述检查,并将值发送到模板,以便在某些 DOM 中的“数据属性”中呈现该值元素。渲染后,您可以从 JS 访问该值。
这些方法都不是做这些事情的“Drupal 方式”,但是......希望这些对您有所帮助。
干杯。
Maybe it's not the best option, but...
What do you think about calling, from your JS function, a custom module's controller, that return
true
if...$currentUser = \Drupal::currentUser(); $uid = $currentUser->id();
returns a numeric value higher than0
orfalse
if the value returned is0
(anonymous user)? This controller only should had to return the UID of the current user in a JSON format, readable afterwards from your JS.Another try would be to write a YOURTHEMENAME_preprocess_page() function in your theme's .theme file that implement the above check, and send the value to the template, in order to render the value in a 'data-attribute' in some DOM element. Once rendered, you could access the value from your JS.
None of these approachs are 'Drupal way' of doing the stuff, but... hope these help you.
Cheers.