使用AJAX生成tableau受信任票证

时间:2013-07-01 22:51:38

标签: php ajax tableau

我们正在尝试使用Ajax从Tableau Server获取受信任的票证。 Tableau团队提供对php,java,sharepoint,ruby的支持,但不支持Ajax。

我们正在运作的代码是:

<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/tableau_v8.js"></script>
<script type="text/javascript" src="jsonp.js"></script>
<script type="text/javascript">

$(document).ready(function () {
    $("#tableau").each(function () {

        var workbook = $(this).attr("Presents");
        var view = $(this).attr("MyDashboard");
        var username = $(this).attr("userx");
        var ticket;

        $.ajax({
            type: 'POST',
            url: 'http://serverurl/trusted',
            contentType: 'application/json',
            dataType: 'jsonp',
                data: { 
            username:'registeredusername',
            server:'url},
            success: function (result) {
                alert('Ok');
                ticket = result;
            },
            error: function () { 
                //console.log('Erro');
                alert('Error'); 
            }
        });
        var url = "http://serverurl/trusted/" + ticket + "/views/" + workbook + "/" + view;
        var options = {
            width: this.offsetWidth,
            height: this.offsetHeight,
            hideTabs: true,
            hideToolbar: false,
            onFirstInteractive: function () {
            workbook = viz.getWorkbook();
            activeSheet = workbook.getActiveSheet();
            }

            };
        viz = new tableauSoftware.Viz(this, url, options);
    });

  });
</script>

为了给你一个想法,请在下面找到它在php中的工作原理:

TABLEAU_TRUSTED.php

<?php

// Returns a trusted URL for a view on a server for the
// given user.  For example, if the URL of the view is:
//    http://tabserver/views/MyWorkbook/MyView
//
// Then:
//   $server = "tabserver";
//   $view_url = "views/MyWorkbook/MyView";
//
function get_trusted_url($user,$server,$view_url) {
  $params = ':embed=yes&:toolbar=yes';


  $ticket = get_trusted_ticket($server, $user, $_SERVER['REMOTE_ADDR']);
  if($ticket > 0) {
    return "http://$server/trusted/$ticket/$view_url?$params";
  }
  else 
    return 0;
}

// Note that this function requires the pecl_http extension. 
// See: http://pecl.php.net/package/pecl_http

// the client_ip parameter isn't necessary to send in the POST unless you have
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
Function get_trusted_ticket($wgserver, $user, $remote_addr) {
  $params = array(
    'username' => $user,
    'client_ip' => $remote_addr
  );

  return http_parse_message(http_post_fields("http://$wgserver/trusted", $params))->body;
}

?>

TABLEAU_SAMPLE_VIEW_PAGE.php

    <p>An embedded view appears below:</p>

<?php

// This user-provided library should define get_user(), which returns the 
// name of the user currently logged into this application.
//
include 'auth.php';

// Tableau-provided functions for doing trusted authentication
include 'tableau_trusted.php';

?>

<iframe src="<?php echo get_trusted_url(get_user(),'localhost','views/Date-Time/DateCalcs')?>"
        width="400" height="400">
</iframe>

<p>
This was created using trusted authentication.
</p>

任何想法这个PHP如何在javascript和ajax中工作? 任何想法我们如何改进我们的代码?

谢谢,加布里埃尔

1 个答案:

答案 0 :(得分:3)

Gabriel - 正如Tableau Software论坛上的帖子所警告的那样,您不希望仅使用(客户端)JavaScript / AJAX来请求受信任的票证。这样做会让您遇到很多麻烦,主要是打开Tableau Server,以便任何人都可以为其他人请求票证。以这种方式实施,你很快就会失业:)

你可以使用AJAX来调用服务器端的PHP函数,即使这样做是一种kludge。但是,由于页面将存在于您信任的单个服务器上,因此您是安全的。

以下是您的代码的外观:

首先,修改tableau_trusted.php,添加一两个函数:

function generateTicket()
{

       $ticket= get_trusted_ticket_direct($_POST['server'], $_POST['user'], $_POST['targetsite']);
       echo $ticket;

}

if ($_POST['toDo'] == 'generateTicket') {
    generateTicket();
}

以下是您可以调用该页面的方式:

// variables to feed trusted ticket retrieval
    var phpScript = "http://someserver/tableau_trusted.php",
        userName = "someuser",
        serverURL = "tableauserverlocation";

   // variable to hold trusted ticket
        var incomingTicket;

   $.post(phpScript, {
    toDo: 'generateTicket',
        user: userName,
        server: serverURL,
        targetsite: ''
    }, function(response) {
        // do something with response (the ticket) right here
        incomingTicket = response;
    });

我猜你正在尝试重复使用其中一个Tableau示例而不是编写自己的代码,这很好。但是,你所经历的体操可能比写一个简单的网络服务(或Windows服务,或其他)为你做这项工作更麻烦,然后从你的门户页面调用它。无论如何,这将是一个更清洁的设计模式。

祝你好运!