首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Drupal:对php脚本的访问权限?

Drupal:对php脚本的访问权限?
EN

Stack Overflow用户
提问于 2010-07-14 18:40:22
回答 3查看 1.6K关注 0票数 0

我正在我的Drupal网站上写一个自定义的php代码。我需要从PHP加载特定页面的内容。

这些页面只对经过身份验证的用户可见,而且我似乎无法从php访问它们,即使我以用户身份登录时触发了脚本。

有没有一种方法可以模拟来自php的“登录”用户,这样我就可以访问网站的所有内容了吗?

更新

代码语言:javascript
运行
复制
global $user;
if (user_access('access content')) {

require_once("dompdf/dompdf_config.inc.php");

$html = file_get_contents('http://mywebsite.com/admin/store/orders/45/invoice/print');


$dompdf = new DOMPDF();
$dompdf->load_html($html);

//$dompdf->load_html_file('invoices/' . $file);
$dompdf->render();
$dompdf->stream("sample.pdf");
}

我试过相对路径,这是一样的.

这与模拟管理用户有关。

代码语言:javascript
运行
复制
//access as administrator
global $user;
$original_user = $user;
session_save_session(FALSE); 
$user = user_load(array('uid' => 1));

//generate pdf
require_once("dompdf/dompdf_config.inc.php");
$html = file_get_contents('http://mywebsite/admin/store/orders/45/invoice/print');
$dompdf = new DOMPDF();
$dompdf->load_html($html);
//$dompdf->load_html_file('invoices/' . $file);
$dompdf->render();
$dompdf->stream("sample.pdf");

//logout as administrator
$user = $original_user;
session_save_session(TRUE); 

尽管如此,我还是拒绝了作为结果页面的访问(并生成pdf)。谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-07-15 02:41:56

这些页面只对经过身份验证的用户可见,而且我似乎无法从php访问它们,即使我以用户身份登录时触发了脚本。

Drupal检查用户是否拥有使用全局变量$user查看节点的权限。要做您想要做的事情,如果您不能相信当前登录的用户拥有查看您感兴趣的节点的权限,则应该阅读安全地模拟另一个用户

我不是说你应该这么做。在模拟另一个用户之前,我将验证以下方法是否是唯一可能的方法。例如,如果只需要访问节点中包含的字段,则可以使用node_load(),这不会验证当前用户是否可以查看加载的节点。如果需要显示节点的主体,可以使用以下代码:

代码语言:javascript
运行
复制
$node = node_load($nid);
if ($node) {
  $body = check_markup($node->body, $node->format, FALSE);
}

但是,显示当前用户无法访问的信息被认为是一个安全问题。

更新

代码的问题在于您正在使用file_get_contents('http://mywebsite/admin/store/orders/45/invoice/print');这样做,您将打开到站点的新连接,并且新连接将以匿名用户的身份打开。这就是不返回通过身份验证的用户能够看到的节点的原因。

即使代码能够工作,您所得到的并不是HTML只呈现节点,而是整个页面,包括Drupal通常显示在顶部和左侧/右侧的块。

如果您有兴趣呈现一个节点,那么您应该使用以下代码。(它只是一副骨架,而且还不完整。)

代码语言:javascript
运行
复制
// $nid is the node ID.
// Check the result, in the case the node has been deleted, or there are other errors.
$node = node_load($nid);
if ($node) {
  // The arguments tell the function that you don't want to render a teaser, that the node is
  // rendered as it is the only node in the page, and that you don't want the additional
  // links that are usually rendered after the node content.
  $html = node_view($node, FALSE, TRUE, FALSE);

  // This is your code.
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $dompdf->stream("sample.pdf");
}
票数 1
EN

Stack Overflow用户

发布于 2010-07-14 20:28:53

这样做的守则是:

代码语言:javascript
运行
复制
<?php
if (user_access('access content')) {
  print "You have the permission 'access content'";
}
?>

运行绕过权限系统的代码看起来既简单又容易,但实际上是一个严重的安全漏洞。

但是,既然这就是你所要求的:

代码语言:javascript
运行
复制
<?php
global $user;
if ($user->uid) {
  print "You are a registered user"
}
?>

但是,不要将此用作权限的替换。

票数 2
EN

Stack Overflow用户

发布于 2010-07-15 19:27:49

关于更新的代码。您的file_get_contents将以“匿名用户”的身份输入内容。这就是为什么您的代码不是一个好主意的原因之一:

每当您的代码运行时,它都会打开您自己的站点并解析该代码:结果至少会加载两个"Drupals“:实际上至少有两个页面视图来向用户显示一个页面。但是,这种方法还有更多的问题。

相反,您应该找到在http://mywebsite.com/admin/store/orders/45/invoice/print上创建页面的代码/函数,并将其用作PDF-创建者的输入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3249305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档