Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
v0.0.3 import
Browse files Browse the repository at this point in the history
Ben Sullins committed Apr 15, 2013
1 parent 5c5f3f0 commit 58c8f81
Showing 13 changed files with 372 additions and 128 deletions.
14 changes: 14 additions & 0 deletions src/add_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once('init.php');
$ldap = get_ldap_connection();
include 'tableau_trusted.php';

if(add_tableau_user($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"], get_ldap_cn($_SERVER["PHP_AUTH_USER"]), 'interactor', 'none', '0')) {
$trusted_url = login_tableau($_SERVER["PHP_AUTH_USER"],TABLEAU_SERVER,'projects');
echo '<meta http-equiv="refresh" content="2;url=' . $trusted_url . '">';
} else {
echo "Failed attempt to create user: " . $_SERVER["PHP_AUTH_USER"];
}

?>
4 changes: 1 addition & 3 deletions src/config-local.php-dist
Original file line number Diff line number Diff line change
@@ -19,6 +19,4 @@ define('ADD_TABLEAU_USERS', false);

define('TABLEAU_ADMIN', MyAdminAccount);

define('TABLEAU_ADMIN_PW', MyAdminPassword);

define('TABLEAU_SERVER', "https://mytableauserver.com");
define('TABLEAU_ADMIN_PW', MyAdminPassword);
56 changes: 54 additions & 2 deletions src/css/style.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

* {
margin: 0;
padding: 0;
margin: 5px;
padding: 5px;
font-family: inherit;
font-size: 100%;
font-weight: inherit;
@@ -21,6 +21,58 @@
src: url(fonts/DroidSans-Bold.ttf) format("truetype");
}

/*Tableau LDAP Additions*/

h1 {
font-size: 36px;
}

h2 {
font-size: 32px;
}

h3 {
font-size: 28px;
}

#msg {
display: block;
margin-left: auto;
margin-right: auto;
width:100%;
}

#msg a {
color:#ffffff;
text-decoration: underline;
}

button.moz-tableau-login {
background-color:#e66000;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #dcdcdc;
display:inline-block;
color:#ffffff;
font-family:arial;
font-size:16px;
font-weight:bold;
padding:10px 20px;
text-decoration:none;
}.moz-tableau-login:hover {
background-color:#ff9500;
}.moz-tableau-login:active {
position:relative;
top:1px;
}





/* Phoenbook CSS */

html {
font: 16px "Lucida Grande", "Droid Sans", "Trebuchet MS", sans-serif;
font-weight: normal;
107 changes: 107 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -262,3 +262,110 @@ function escape_ldap_filter_value($values = array())

if ($unwrap) return $values[0]; else return $values;
}

// Tableau LDAP Functions
function get_ldap_cn($user, $debug=0) {

try{

if (!$ds = get_ldap_connection()) { throw new Exception('Unable to connect to LDAP Server');}
$dn = "mail=$user, o=com, dc=mozilla"; //the object itself instead of the top search level as in ldap_search
$filter="(objectclass=inetOrgPerson)"; // this command requires some filter
$justthese = array("cn"); //the attributes to pull, which is much more efficient than pulling all attributes if you don't do this
if (!$sr=ldap_read($ds, $dn, $filter, $justthese)) { throw new Exception('Incorrect Username or filter');}
if (!$entry = ldap_get_entries($ds, $sr)) { throw new Exception('Unable to find LDAP entry for ' . $user);}

if ($debug!=0) {
echo $entry[0]["cn"][0] . " is the name in LDAP for " . $user;
}

ldap_close($ds);
return $entry[0]["cn"][0];

} catch (Exception $e) {
echo 'Oops! I countered the following error: ', $e->getMessage(), "\n";
return $_SERVER["PHP_AUTH_USER"];
}
}

function add_tableau_user ($username, $pwd, $name, $level, $admin, $publisher, $debug=0) {

try {

//create file
$filename = "users";
$users = getcwd() . '/'.$filename.'.csv';
//create new file
$filehandle = fopen($users, 'w') or die("can't open file");
fclose($filehandle);
$fp = fopen($users, 'w');

//add data to file
$csv_fields = array();
$csv_fields[0] = array();
$csv_fields[0][] = $username;
$csv_fields[0][] = $pwd;
$csv_fields[0][] = $name;
$csv_fields[0][] = $level;
$csv_fields[0][] = $admin;
$csv_fields[0][] = $publisher;


foreach ($csv_fields as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

//run tabcmd to create user
if(!$login = shell_exec('./tabcmdexe login --server ' . TABLEAU_SERVER . ' --username ' . TABLEAU_ADMIN . ' --password ' . TABLEAU_ADMIN_PW)) {
throw new Exception('Unable to login to Tableau Server: ' . $login);
}

if($debug!=0){ echo "<h1>Login to Tableau</h1><pre>" . $login . "</pre>"; }

//create user
if(!$createusers = shell_exec('./tabcmdexe createusers "' . $users . '"')) {
throw new Exception('Unable to create users: ' . $createusers);
}

if($debug!=0){ echo "<h1>Creating User</h1><pre>" . $createusers . "</pre>"; }

//create ldap group (could be switched off by config)
if(!$creategroup = shell_exec('./tabcmdexe creategroup "ldap"')) {
throw new Exception('Unable to create group "ldap" because: ' . $creategroup);
}

//add user to ldap group
if(!$addusers = shell_exec('./tabcmdexe addusers "ldap" --users "' . $users .'"')) {
throw new Exception('Unable to add users to "ldap" group: ' . $addusers);
}

if($debug!=0){ echo "<h1>Add User to group</h1><pre>" . $createusers . "</pre>"; }

//delete file
unlink($users);

return true;

} catch (Exception $e) {
echo "Oops! Ran into a speed bump: " . $e;
//delete file
unlink($users);
}
}

function login_tableau ($user, $host, $home) {

$host = str_replace("https://", "", $host);
$host = str_replace("http://", "", $host);

if(!$trusted_url=get_default_url($user, $host, $home)){
return false;
} else {
return $trusted_url;
}
}



43 changes: 43 additions & 0 deletions src/get_cn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
require_once("init.php");
require_once("config.php");
require_once('templates/header.php');


$auth = new MozillaAuthAdapter();
$search = new MozillaSearchAdapter($ldapconn);

function get_ldap_cn($user, $debug=0) {

try{

if (!$ds = get_ldap_connection()) { throw new Exception('Unable to connect to LDAP Server');}
$dn = "mail=$user, o=com, dc=mozilla"; //the object itself instead of the top search level as in ldap_search
$filter="(objectclass=inetOrgPerson)"; // this command requires some filter
$justthese = array("cn"); //the attributes to pull, which is much more efficient than pulling all attributes if you don't do this
if (!$sr=ldap_read($ds, $dn, $filter, $justthese)) { throw new Exception('Incorrect Username or filter');}
if (!$entry = ldap_get_entries($ds, $sr)) { throw new Exception('Unable to find LDAP entry for ' . $user);}

if ($debug!=0) {
echo $entry[0]["cn"][0] . " is the name in LDAP for " . $user;
}

ldap_close($ds);
return $entry[0]["cn"][0];

} catch (Exception $e) {
echo 'Oops! I countered the following error: ', $e->getMessage(), "\n";
}
}

get_ldap_cn("[email protected]");

require_once('templates/footer.php');

?>






Binary file added src/img/Thumbs.db
Binary file not shown.
Binary file added src/img/ajax-loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 7 additions & 123 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -2,152 +2,36 @@
require_once('init.php');
$ldap = get_ldap_connection();
require_once('templates/header.php');

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

// Check if the user is in a specific LDAP group, not quite working...
// $search = new MozillaSearchAdapter($ldap);
// $sr = $search->query_users("cn=*sullins*");
// print_r($sr);



function get_ldap_cn($user, $debug=0) {

try{

if (!$ds = get_ldap_connection()) { throw new Exception('Unable to connect to LDAP Server');}
$dn = "mail=$user, o=com, dc=mozilla"; //the object itself instead of the top search level as in ldap_search
$filter="(objectclass=inetOrgPerson)"; // this command requires some filter
$justthese = array("cn"); //the attributes to pull, which is much more efficient than pulling all attributes if you don't do this
if (!$sr=ldap_read($ds, $dn, $filter, $justthese)) { throw new Exception('Incorrect Username or filter');}
if (!$entry = ldap_get_entries($ds, $sr)) { throw new Exception('Unable to find LDAP entry for ' . $user);}

if ($debug!=0) {
echo $entry[0]["cn"][0] . " is the name in LDAP for " . $user;
}

ldap_close($ds);
return $entry[0]["cn"][0];

} catch (Exception $e) {
echo 'Oops! I countered the following error: ', $e->getMessage(), "\n";
return $_SERVER["PHP_AUTH_USER"];
}
}

function add_tableau_user ($username, $pwd, $name, $level, $admin, $publisher, $debug=0) {

try {

//create file
$filename = "users";
$users = getcwd() . '/'.$filename.'.csv';
//create new file
$filehandle = fopen($users, 'w') or die("can't open file");
fclose($filehandle);
$fp = fopen($users, 'w');

//add data to file
$csv_fields = array();
$csv_fields[0] = array();
$csv_fields[0][] = $username;
$csv_fields[0][] = $pwd;
$csv_fields[0][] = $name;
$csv_fields[0][] = $level;
$csv_fields[0][] = $admin;
$csv_fields[0][] = $publisher;


foreach ($csv_fields as $fields) {
fputcsv($fp, $fields);
}

fclose($fp);

//run tabcmd to create user
if(!$login = shell_exec('./tabcmdexe login --server ' . TABLEAU_SERVER . ' --username ' . TABLEAU_ADMIN . ' --password ' . TABLEAU_ADMIN_PW)) {
throw new Exception('Unable to login to Tableau Server: ' . $login);
}

if($debug!=0){ echo "<h1>Login to Tableau</h1><pre>" . $login . "</pre>"; }

//create user
if(!$createusers = shell_exec('./tabcmdexe createusers "' . $users . '"')) {
throw new Exception('Unable to create users: ' . $createusers);
}

if($debug!=0){ echo "<h1>Creating User</h1><pre>" . $createusers . "</pre>"; }

//add user to ldap group

if(!$addusers = shell_exec('./tabcmdexe addusers "ldap" --users "' . $users .'"')) {
throw new Exception('Unable to add users to "ldap" group: ' . $addusers);
}

if($debug!=0){ echo "<h1>Add User to group</h1><pre>" . $createusers . "</pre>"; }

//delete file
unlink($users);

return true;

} catch (Exception $e) {
echo "Oops! Ran into a speed bump: " . $e;
//delete file
unlink($users);
}
}

function login_tableau ($user, $host, $home) {

$host = str_replace("https://", "", $host);
$host = str_replace("http://", "", $host);

if(!$trusted_url=get_default_url($user, $host, $home)){
return false;
} else {
return $trusted_url;
}
}

?>

<?php


//Log the user in or add them to the server if the ADD_TABLEAU_USERS bit is set to TRUE in the config-local.php file
if (!$trusted_url=login_tableau($_SERVER["PHP_AUTH_USER"],TABLEAU_SERVER,'projects')) {

//add user to the server if config-local.php has bit flipped
if (ADD_TABLEAU_USERS) {

if (add_tableau_user($_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"], get_ldap_cn($_SERVER["PHP_AUTH_USER"]), 'interactor', 'none', '0')){

//get url
$trusted_url = login_tableau($_SERVER["PHP_AUTH_USER"],TABLEAU_SERVER,'projects');

//login
echo '<div id="success-msg" style="width=300px; padding-top:40px; text-align:center; vertical-align:baseline; "><h1>Success! Your account in Tableau has been created using your LDAP credentials. You will be redirected there in ~3 seconds. If nothing happens feel free to visit Tableau directly at: ' . TABLEAU_SERVER . '. Have fun seeing your data like never before! <meta http-equiv="refresh" content="7;url=' . $trusted_url . '"></h1></div>';

} else {
// mail('bsullins@mozilla.com', 'Error logging in for: ' . $_SERVER["PHP_AUTH_USER"], 'Invalid login;');
echo "Oops! Something went wrong, the Tableau administrators have been notified. If this continues to happen please submit a bug.";
}

echo '<div id="msg"><h1>Need a Tableau Account?&nbsp;&nbsp;<button class="moz-tableau-login">Create Account</button></h1></div><h3>What is Tableau?</h3><iframe width="560" height="315" src="http://www.youtube.com/embed/OaQdWeFpov8" frameborder="0" allowfullscreen></iframe>';

} else {

//send error email. Probably good to log this somewhere instead of just emailing
// mail('bsullins@mozilla.com', 'Error logging in for: ' . $_SERVER["PHP_AUTH_USER"], 'Invalid login;');

//print error message with links to fix
echo '<h1>Oops! I couldn\'t log you in. Do you have an account with Tableau already? If so, please submit a bug, otherwise follow these instructions for getting an account setup: <a href="https://mana.mozilla.org/wiki/display/METRICS/Getting+Access+to+Tableau+Server">link</a></h1>';

}

} else {
echo 'Tableau should load shortly...<meta http-equiv="refresh" content="1;url=' . $trusted_url . '">';
echo '<h1>Tableau should load shortly...</h1><meta http-equiv="refresh" content="0;url=' . $trusted_url . '">';
}

echo '</div>';

?>

<?php require_once('templates/footer.php'); ?>
5 changes: 5 additions & 0 deletions src/js/jquery-1.9.1.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/output-json.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

function output_json($entries) {
header("Content-Type: application/json");
print json_encode($entries);
die;
}

82 changes: 82 additions & 0 deletions src/preprocessors-attr.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

function get_attr_preprocessors() {
return array(
"employeetype" => "employee_status",
"manager" => "get_manager",
"mobile" => "mobile_normalizer",
"im" => "mobile_normalizer"
);
}

function employee_status($status) {
global $orgs, $emp_type;
$current_org = $current_emp_type = "";
if ($status != "") {
$current_org = $status[0];
$current_emp_type = $status[1];
}
if ($status == "DISABLED") {
return array('DISABLED');
} else {
if (array_key_exists($current_org, $orgs) &&
array_key_exists($current_emp_type, $emp_type)) {
return array($orgs[$current_org], $emp_type[$current_emp_type]);
} else {
return array('Unknown');
}
}
}

function get_manager($manager_dn) {
global $ldapconn, $memcache_on, $memcache;
if ($memcache_on && ($manager = $memcache->get(MEMCACHE_PREFIX . $manager_dn))) {
return $manager;
}
$manager_search = @ldap_search($ldapconn, $manager_dn, '(mail=*)', array('cn','mail'));
if (ldap_errno($ldapconn) == 32) { // No manager found
return NULL;
}
if ($manager_search) {
$entry = ldap_first_entry($ldapconn, $manager_search);
if ($entry) {
$attrs = ldap_get_attributes($ldapconn, $entry);
$manager = array(
"cn" => $attrs['cn'][0],
"dn" => $manager_dn
);
} else {
$manager = NULL;
}
if ($memcache_on) {
$memcache->set(MEMCACHE_PREFIX . $manager_dn, $manager);
}
return $manager;
}
}

function mobile_normalizer($m) {
return is_array($m) ? $m : array($m);
}

function wikilinks($string) {
$matches = array();
$string = nl2br(htmlspecialchars($string));
if (preg_match_all('/\[(.+?)(?:\s(.+?))?\]/', $string, $matches)) {
foreach ($matches[1] as $key => $value) {
if (!empty($matches[2][$key])) {
$title = $matches[2][$key];
} else {
$title = $value;
}
$string = str_replace(
$matches[0][$key],
'<a href="'. $value .'">'. $title .'</a>',
$string
);
}
}
return $string;
}


33 changes: 33 additions & 0 deletions src/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require_once("init.php");
require_once("config.php");
require_once("preprocessors-attr.inc");

$auth = new MozillaAuthAdapter();
$search = new MozillaSearchAdapter($ldapconn);
$keyword = isset($_GET["query"]) ? $_GET["query"] : $_SERVER["PHP_AUTH_USER"];
$entries = normalize($search->search_users($keyword));
$attr_preps = get_attr_preprocessors();

$preprocess_attr_functions = array();
foreach ($entries as &$entry) {
foreach ($entry as $name => $attribute) {
$prep = isset($attr_preps[$name]) ? $attr_preps[$name] : NULL;
if (!isset($preprocess_attr_functions[$prep])) {
$preprocess_attr_functions[$prep] = function_exists($prep);
}
if ($preprocess_attr_functions[$prep]) {
$entry[$name] = call_user_func($prep, $attribute);
}
}
$search->preprocess_entry($entry);
}

$format = isset($_GET["format"]) ? $_GET["format"] : "json";
if (!in_array($format, $output_formats) || !file_exists("output-$format.inc")) {
$format = "json";
}
require_once("output-$format.inc");
$function = "output_$format";
$dn = $auth->user_to_dn($_SERVER["PHP_AUTH_USER"]);
call_user_func($function, $entries, $auth->is_phonebook_admin($ldapconn, $dn));
18 changes: 18 additions & 0 deletions src/templates/header.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,24 @@
<link rel="shortcut icon" type="image/x-icon" href="./favicon.ico" />
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script> // add user to tableau ajax call
$(document).ready(function(){
$("button").click(function(event){
// alert("starting ajax call");
$("#msg").html('<h3>Creating account <img src="img/ajax-loader.gif" style="vertical-align:bottom;"/></h3>');
$.post(
"add_user.php",
function(msg){
$("#msg").html('<h3>Account created! Refresh this page to login whenever you are ready.</h3>');
});
});
}); </script>
</head>
<body>

0 comments on commit 58c8f81

Please sign in to comment.