|
LiveUser_Admin example 1
LiveUser_Admin Example 1 comprises the following files:
- index.php
- The top level script.
- config.inc.php
- Configuration script.
- demodata.xml
- This XML file gets used to create the database by the MDB2_Schema class.
There is also one *.php file for each main LiveUser table -- the index.php navigates to each of these.
- Application.php
- Area_Admin_Areas.php
- Area.php
- Group.php
- GroupRights?.php
- ImplyRights?.php
- OutputRightsConstants?.php
- Rights.php
- Subgroups.php
- Translation.php
- UserGroup?.php
- User.php
- UserRights?.php
index.php
This is just the top level script of the example, and contains links to each of the test scripts that can be run in turn.
<?php
/*
TODO:
ImplyRight
*/
require_once 'config.inc.php';
include_once 'Var_Dump.php';
Var_Dump::displayInit(
array('display_mode' => 'XHTML_Text'),
array('mode' => 'normal', 'offset' => 4)
);
?>
<html>
<style>
a {
color: #006600;
text-decoration: none;
}
a:visisted {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* style for XHTML_Text */
table.var_dump { border-collapse:separate; border:1px solid black; border-spacing:0; }
table.var_dump tr { color:#006600; background:#F8F8F8; vertical-align:top; }
table.var_dump tr.alt { color:#006600; background:#E8E8E8; }
table.var_dump th { padding:4px; color:black; background:#CCCCCC; text-align:left; }
table.var_dump td { padding:4px; }
table.var_dump caption { caption-side:top; color:white; background:#339900; }
table.var_dump i { color: #000000; background: transparent; font-style: normal; }
/* style for XHTML_Text */
pre.var_dump { line-height:1.8em; }
pre.var_dump span.type { color:#006600; background:transparent; }
pre.var_dump span.value { padding:2px; color:#339900; background:#F0F0F0; border: 1px dashed #CCCCCC; }
</style>
<body>
<form action="?" method="get">
<select name="storage">
<?php
foreach ($backends as $backend => $row) {
$selected = $storage == $backend ? 'selected="selected"' : '';
echo '<option value="'.$backend.'" '.$selected.'>'.$backend.'</option>';
}
?>
</select>
<input type="submit" id="go" value="Go!" />
Make sure you read README in the examples root folder to setup the database.
</form>
<?php
$qstring = array_key_exists('QUERY_STRING', $_SERVER) && !array_key_exists('del', $_GET) ? '?'.$_SERVER['QUERY_STRING'] : '';
echo '
<a href="Application.php'.$qstring.'">Application</a> |
<a href="Area.php'.$qstring.'">Area</a> |
<a href="Group.php'.$qstring.'">Group</a> |
<a href="GroupRights.php'.$qstring.'">GroupRights</a> |
<a href="ImplyRights.php'.$qstring.'">ImplyRights</a> |
<a href="User.php'.$qstring.'">User</a> |
<a href="Rights.php'.$qstring.'">Rights</a> |
<a href="Subgroups.php'.$qstring.'">Subgroups</a> |
<a href="UserGroup.php'.$qstring.'">UserGroup</a> |
<a href="UserRights.php'.$qstring.'">UserRights</a> |
<a href="OutputRightsConstants.php'.$qstring.'">OutputRightsConstants</a> |
<a href="Translation.php'.$qstring.'">Translation</a> |
<a href="test.php'.$qstring.'">Test</a><br />';
?>
So that these test will run you have to have <a href="http://pear.php.net/package/Var_Dump">Var_Dump</a> installed<br /><br />
<?php
if (array_key_exists('del', $_GET)) {
$db->expectError(MDB2_ERROR_NOSUCHTABLE);
$db->query('DELETE FROM liveuser_applications');
$db->query('DROP TABLE liveuser_applications_seq');
$db->query('DELETE FROM liveuser_area_admin_areas');
$db->query('DELETE FROM liveuser_areas');
$db->query('DROP TABLE liveuser_areas_seq');
$db->query('DELETE FROM liveuser_group_subgroups');
$db->query('DELETE FROM liveuser_grouprights');
$db->query('DELETE FROM liveuser_groups');
$db->query('DROP TABLE liveuser_groups_seq');
$db->query('DELETE FROM liveuser_groupusers');
$db->query('DROP TABLE liveuser_groupusers_seq');
$db->query('DELETE FROM liveuser_perm_users');
$db->query('DROP TABLE liveuser_perm_users_seq');
$db->query('DELETE FROM liveuser_right_implied');
$db->query('DELETE FROM liveuser_rights');
$db->query('DROP TABLE liveuser_rights_seq');
$db->query('DELETE FROM liveuser_userrights');
$db->query('DROP TABLE liveuser_userrights_seq');
$db->query('DELETE FROM liveuser_users');
$db->query('DROP TABLE liveuser_users_seq');
$db->query('DELETE FROM liveuser_translations');
$db->query('DROP TABLE liveuser_translations_seq');
$db->popExpect();
echo 'Reseted the database';
exit;
} else {
echo '<a href="?del=1">Reset the database</a><br /><br />';
}
?>
</body>
</html>
?>
config.inc.php
In this example, config.inc.php is used to set up the entire LiveUser structure, as was done in Example3.
<?php
function php_error_handler($errno, $errstr, $errfile, $errline)
{
if (error_reporting() && $errno != 2048) {
var_dump('error_msg', "<b>$errfile ($errline)</b><br />$errstr");
}
}
set_error_handler('php_error_handler');
require_once 'LiveUser/Admin.php';
//
// Database configuration.
// Please configure the following file according to your environment
//
$GLOBALS['_LIVEUSER_DEBUG'] = true;
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
$db_name = 'liveuser_admin_test_example1';
$dsn = "mysql://$db_user:$db_pass@$db_host/$db_name";
//
// Define three possible backends to use for LiveUser.
//
$backends = array(
'DB' => array(
'options' => array()
),
'MDB' => array(
'options' => array()
),
'MDB2' => array(
'options' => array(
'debug' => true,
'debug_handler' => 'echoQuery',
)
)
);
//
// Fetch a back end based on the value of "storage" in the URL. If no
// value is set then pick the default MDB2 storage container.
//
if (!array_key_exists('storage', $_GET)) {
$storage = 'MDB2';
} elseif (isset($backends[$_GET['storage']])) {
$storage = strtoupper($_GET['storage']);
} else {
exit('storage Backend not found.');
}
//
// Build the storage back-end object.
//
require_once $storage.'.php';
function echoQuery(&$db, $scope, $message)
{
Var_Dump::display($scope.': '.$message);
}
$dummy = new $storage;
$db = $dummy->connect($dsn, $backends[$storage]['options']);
if (PEAR::isError($db)) {
echo $db->getMessage() . ' ' . $db->getUserInfo();
die();
}
$db->setFetchMode(constant($storage.'_FETCHMODE_ASSOC'));
//
// Set up the LiveUser configuration, choosing the back end that we
// selected earlier.
//
$conf =
array(
'debug' => true,
'session' => array(
'name' => 'PHPSESSION',
'varname' => 'ludata'
),
'login' => array(
'force' => false,
),
'logout' => array(
'destroy' => true,
),
//
// We are only going to have one authentication container. This time
// it will be a container of whatever type we asked for earlier, which
// is now in the $storage variable.
//
'authContainers' => array(
'DB_Local' => array(
'type' => $storage,
'expireTime' => 3600,
'idleTime' => 1800,
'allowDuplicateHandles' => false,
'storage' => array(
'connection' => $db,
'dsn' => $dsn,
'prefix' => 'liveuser_',
'tables' => array(
'users' => array(
'fields' => array(
'name' => false,
'email' => false,
),
),
),
'fields' => array(
'name' => 'text',
'email' => 'text',
),
'alias' => array(
'name' => 'name',
'email' => 'email',
'auth_user_id' => 'user_id',
),
// 'force_seq' => false
),
)
),
//
// We set up one permission container, with much the same parameters
// as the storage container. This is going to use the "Complex" complexity
// permissions model, which gives us access to the full set of permissions
// available in LiveUser. See the E-R diagram here:
// http://oss.backendmedia.com/LiveUser/External
//
'permContainer' => array(
'type' => 'Complex',
'alias' => array(),
'storage' => array(
$storage => array(
'connection' => $db,
'dsn' => $dsn,
'prefix' => 'liveuser_',
'tables' => array(),
'fields' => array(),
'alias' => array(),
// 'force_seq' => false
),
),
),
);
//
// Build the LiveUser_Admin object.
//
$admin =& LiveUser_Admin::factory($conf);
$logconf = array('mode' => 0666, 'timeFormat' => '%X %x');
$logger = &Log::factory('file', 'liveuser_test.log', 'ident', $logconf);
$admin->log->addChild($logger);
$admin->init();
?>
demodata.xml
This file is loaded into the database by the script demodata.php, which I was only able to find in LiveUser CVS. When you have this script you can use the following command to load the demodata.xml file into your database:
php demodata.php -d mysql://root:ROOTPASSWORD@localhost/liveuser_test -f demodata.xml
If you want to see the file, see FullTextOfDemodataForExample1
If you want to see an SQL representation of the file it creates (in case you don't want to use MDB2, or don't have the demodata.php script), see MySQLDumpOfDemodataForExample1.
Other scripts
I won't bore you to tears with the remaining scripts in this example, however I will include the following examples:
User.php
This script creates 10 random users and 10 random admin users, and then runs a series of permissions checks against them. It illustrates the process of creating a user, and checking permissions.
<?php
require_once 'index.php';
echo '<h3>User</h3>';
// Add
echo 'Make 10 normal users and 10 admins<br />';
for ($i = 1; $i < 21; $i++) {
$data = array(
'handle' => 'johndoe' . rand(),
'passwd' => 'dummypass',
'name' => 'asdf'.$i,
'email' => 'fleh@example.com'.$i
);
if ($i > 10) {
$level = 3;
} else {
$level = 1;
}
$user_id = $admin->addUser($data, $level);
if ($user_id === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} else {
echo 'Created User Id <strong>' . $user_id . '</strong><br />';
}
}
// Get
// Group of users
echo 'All the users:<br />';
$users = $admin->getUsers('auth');
if ($users === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($users)) {
echo 'No users were found, thus we\'ve halted the rest of the test<br />';
} else {
Var_Dump::display($users);
echo '<br />';
$id = array_rand($users);
// single user
echo 'This user will be removed:<br />';
$user = $admin->getUsers('perm', array('perm_user_id' => $users[$id]['perm_user_id']));
if ($user === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($user)) {
echo 'No user was found.<br />';
} else {
Var_Dump::display($user);
echo '<br />';
}
// Remove
$removed = $admin->removeUser($users[$id]['perm_user_id']);
if ($removed === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} else {
echo '<strong>' . $id . '</strong> was deleted<br />';
unset($users[$id]);
}
// Update
$id = array_rand($users);
$updateUser = $users[$id]['perm_user_id'];
$data = array(
'handle' => 'updated_user'.rand(),
'passwd' => 'foo',
);
$updated = $admin->updateUser($updateUser, $data);
if ($updated === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} else {
echo '<strong>' . $updateUser . '</strong> was updated<br />';
$user = $admin->getUsers('perm', array('perm_user_id' => $updateUser));
if ($user === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($user)) {
echo 'No user was found.<br />';
} else {
Var_Dump::display($user);
echo '<br />';
}
}
// Get
echo 'All the users:<br />';
$users = $admin->getUsers('auth');
if ($users === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($users)) {
echo 'No users were found.<br />';
} else {
Var_Dump::display($users);
echo '<br />';
}
$user = array_rand($users);
echo 'Test fetching auth_user_id AND perm_user_id with PERM getUsers()<br />';
echo 'Auth<br />';
$params = array('filters' => array('auth_user_id' => $users[$user]['auth_user_id']));
$user = $admin->auth->getUsers($params);
if ($user === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($user)) {
echo 'No user was found.';
} else {
Var_Dump::display($user);
echo '<br />';
}
unset($user);
echo 'Perm<br />';
$filter = array('filters' => array('perm_user_id' => '3'));
$user = $admin->perm->getUsers($filter);
if ($user === false) {
echo '<strong>Error on line: '.__LINE__.'</strong><br />';
print_r($admin->getErrors());
} elseif (empty($user)) {
echo 'No user was found.<br />';
} else {
Var_Dump::display($user);
echo '<br />';
}
}
echo '<hr />';
?>
LiveUser:LiveUserAdminExample1 (211.30.65.216)
Sat, 21 Jan 2006, 10:01
|
[ Links | Source | History | RSS ]
|
This site powered by YaWiki 0.22 beta.
|
|