【问题标题】:Drupal: access permissions for php scripts?Drupal:php脚本的访问权限?
【发布时间】:2010-07-14 18:40:22
【问题描述】:

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

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

有没有办法从php模拟“登录”用户,所以我可以访问网站的所有内容?

更新

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");
}

我用相对路径试过了,还是一样...

这是模拟管理员用户

//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)。 谢谢

【问题讨论】:

    标签: drupal drupal-6


    【解决方案1】:

    这样做的代码是:

    <?php
    if (user_access('access content')) {
      print "You have the permission 'access content'";
    }
    ?>
    

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

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

    <?php
    global $user;
    if ($user->uid) {
      print "You are a registered user"
    }
    ?>
    

    但同样,永远不要将其用作权限的替代品。

    【讨论】:

    • 嗨,感谢您的回复。我在 if 语句之间添加了我的代码,但它仍然不起作用。我得到“拒绝访问页面”。如果我复制 url 并使用浏览器访问它,它就可以工作。 (相反,我可以访问它)。
    • 查看更新问题中的代码。我已经尝试过你的两个sn-ps。谢谢
    【解决方案2】:

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

    Drupal 使用全局变量$user 检查用户是否有权查看节点。做你想做的事,如果你不能相信当前登录的用户有权限查看你感兴趣的节点,你应该阅读Safely Impersonating Another User

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

    $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 通常显示在顶部和左侧/右侧的块。

    如果您对渲染节点感兴趣,那么您应该使用以下代码。 (这只是一个骨架,并不完整。)

    // $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");
    }
    

    【讨论】:

    • 我试图冒充管理员用户,但仍然被拒绝访问。我真的不明白为什么,因为我可以从浏览器访问该页面,但不能从 php 脚本访问。我已经用代码更新了问题。
    • 我已经更新了答案,报告方法中的问题以及如何解决它。
    • 我已经用 Ubercart API 解决了获取发票 html $invoice = uc_order_load_invoice(uc_order_load(45));
    【解决方案3】:

    关于更新的代码。 您的 file_get_contents 将以“匿名用户”的身份提取内容。这只是您的代码不是一个好主意的原因之一:

    每当您的代码运行时,它都会打开您自己的站点并解析该代码:导致至少要加载两个“Drupal”:实际上至少有两个综合浏览量才能向用户显示一个页面。但是这种方法可能会出现更多问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 2023-01-10
      • 2020-10-19
      • 2018-03-16
      • 1970-01-01
      相关资源
      最近更新 更多