Home » Authentication » LiveUser » Bug #931
[feature request] define names as arrays
Details
| Submitted | 2004-03-01 19:36 UTC |
|---|---|
| From | smith at backendmedia dot com |
| Assigned | lsmith |
| Status | Closed |
| Package | LiveUser |
| PHP Version | Irrelevant |
| OS | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2004-03-01 19:36 UTC] smith at backendmedia dot com
Description:
------------
I remember that we discussed this point a while back and decided to go with having to pass the right id to LiveUser instead of some unique name instead. This was done for efficiency reasons both on the caching side, the actually comparing etc.
I guess we could provide an alternative script that doesnt generate constants, but instead generates an array with key (define name) and value (right id) pairs. So instead of having to use get_defined_constants() which could result in a rather large array you would only have a much smaller array containing only the right id from LU (or a given application or area).
[2004-03-01 20:31 UTC] simon dot hamilton at ntlworld dot com
This patch might do the trick. It adds the ability in the outputRightsConstants() method in MDB_Simple to output the constants as an array. You can pass in $varName to give it a name for the output. If the $type='array' and the mode is not 'file' the method will return the array.
Here's the patch:
--- MDB_Simple.php 2004-03-01 20:30:04.786052500 +0000
+++ MDB_Simple.php.new.php 2004-03-01 20:34:25.864177500 +0000
@@ -1684,9 +1684,9 @@
* @access public
* @param array options for constants generation
* @param string output mode desired
- * @return mixed boolean or DB Error object
+ * @return mixed boolean, array or DB Error object
*/
- function outputRightsConstants($options = array(), $mode = 'file')
+ function outputRightsConstants($options = array(), $mode = 'file', $type = 'constant', $varName = '')
{
$opt = array();
@@ -1750,13 +1750,27 @@
$strDef = "<?php\n";
foreach ($generate as $k => $v) {
- if ($mode == 'file') {
+ if ($mode == 'file' && $type == 'constant') {
$strDef .= sprintf("define('%s', %s);\n",
strtoupper($v),
$k
);
- } else { // if not file then constant definition is wanted
+
+ } elseif ($type == 'array') {
+ $export[strtoupper($v)] = $k;
+ } else {
define(strtoupper($v), $k);
+ }
+ }
+
+ if (isset($export)) {
+ if ($mode == 'file') {
+ $strDef .= sprintf("\$%s = %s;\n",
+ $varName,
+ var_export($export)
+ );
+ } else {
+ return $export;
}
}
$strDef .= "?>";
Hope it works :)
--
Simon H
[2004-03-01 23:06 UTC] simon dot hamilton at ntlworld dot com
Here's an updated patch. It seems cleaner and I've tested it to return an array of rights with the define_name as the key name.
I can then dynamically check rights like so:
function outputRights()
{
$luAdmin = new
LiveUser_Admin_Perm_Container_MDB_Complex(
$conf['permContainer']);
$luAdmin->setCurrentLanguage('EN');
$res = $luAdmin->outputRightsConstants(array(
'naming' => 3), 'other', 'array');
return $res;
}
$rights = outputRights();
if (!$lu->checkRight($rights[$pagePerm])) {
$page = 'error403'; // Permission Denied
}
--- MDB_Simple.php 2004-03-01 23:05:35.067302500 +0000
+++ MDB_Simple.php.new.php 2004-03-01 23:06:53.879802500 +0000
@@ -1686,7 +1686,7 @@
* @param string output mode desired
* @return mixed boolean or DB Error object
*/
- function outputRightsConstants($options = array(), $mode = 'file')
+ function outputRightsConstants($options = array(), $mode = 'file', $type = 'constant', $varName = '')
{
$opt = array();
@@ -1749,14 +1749,32 @@
}
$strDef = "<?php\n";
+ if ($type == 'array') $export = array();
+
foreach ($generate as $k => $v) {
- if ($mode == 'file') {
- $strDef .= sprintf("define('%s', %s);\n",
- strtoupper($v),
+ $v = strtoupper($v);
+ if ($type == 'constant') {
+ if ($mode == 'file') {
+ $strDef .= sprintf("define('%s', %s);\n",
+ $v,
$k
);
- } else { // if not file then constant definition is wanted
- define(strtoupper($v), $k);
+ } else {
+ define($v, $k);
+ }
+ } else {
+ $export[$v] = $k;
+ }
+ }
+
+ if ($type == 'array') {
+ if ($mode == 'file') {
+ $strDef .= sprintf("\$%s = %s;\n",
+ $varName,
+ var_export($export)
+ );
+ } else {
+ return $export;
}
}
$strDef .= "?>";
--
Simon H
[2004-03-02 07:50 UTC] simon dot hamilton at ntlworld dot com
For some reason I couldn't get var_export($export) to output properly in this patch, so arrays won't output to a file properly until fixed. It seems like var_export() doesn't like sprintf or something. Any ideas?
--
Simon H
[2004-03-02 19:23 UTC] simon dot hamilton at ntlworld dot com
I've done some tests with var_export:
1. My machine supports it as I can get an output ok...Good start. (PHP 4.3.3).
2. I removed the sprintf() completely just trying to output the results of var_export to the file...no joy.
I'm all out of ideas, other than to revert to my version that didn't contain var_export but wrote out lines in the loop like:
<?php
$export['APP_AREA_CAN_READ'] = 4;
$export['APP_AREA_CAN_WRITE'] = 3;
?>
Anyone got an answer? You and your bright ideas Lukas...lol.
--
Simon H
[2004-03-03 20:30 UTC] simon dot hamilton at ntlworld dot com
Third time lucky :-)! OK, I deserve abuse for this one, but hey, its now fixed and working with var_export...every feel really stooopid?
I've tested it:
- returning arrays
- writing a formatted array to a file
- writing defines to a file (as usual)
Here's da patch -
--- MDB_Simple.php 2004-03-01 23:05:35.067302500 +0000
+++ MDB_Simple.php.new.php 2004-03-03 20:32:48.765625000 +0000
@@ -1686,7 +1686,7 @@
* @param string output mode desired
* @return mixed boolean or DB Error object
*/
- function outputRightsConstants($options = array(), $mode = 'file')
+ function outputRightsConstants($options = array(), $mode = 'file', $type = 'constant', $varName = '')
{
$opt = array();
@@ -1749,14 +1749,32 @@
}
$strDef = "<?php\n";
+ if ($type == 'array') $export = array();
+
foreach ($generate as $k => $v) {
- if ($mode == 'file') {
- $strDef .= sprintf("define('%s', %s);\n",
- strtoupper($v),
+ $v = strtoupper($v);
+ if ($type == 'constant') {
+ if ($mode == 'file') {
+ $strDef .= sprintf("define('%s', %s);\n",
+ $v,
$k
);
- } else { // if not file then constant definition is wanted
- define(strtoupper($v), $k);
+ } else {
+ define($v, $k);
+ }
+ } else {
+ $export[$v] = $k;
+ }
+ }
+
+ if ($type == 'array') {
+ if ($mode == 'file') {
+ $strDef .= sprintf("\$%s = %s;\n",
+ $varName,
+ var_export($export, true)
+ );
+ } else {
+ return $export;
}
}
$strDef .= "?>";
Could someone please apply this as soon as possible...its proving (at least to me) to be a very useful feature addition.
--
Simon H
[2004-03-06 18:22 UTC] simon dot hamilton at ntlworld dot com
Your patch seems ok apart from 2 things:
1. You made the same mistake I was making with var_export(). It needs to be var_export($generate, true) - The true makes var_export return a variable value rather than outputting it.
2. You have used strtoupper() in the define only an not for the array option. Perhaps it would be good to have a final foreach when building the $generate array just to do teh strtoupper rather than adding it to each naming bit, then both arrays and defines will be in upper case, just for convention.
--
Simon H
[2004-03-06 23:20 UTC] simon dot hamilton at ntlworld dot com
Yup!
With the fix to var_export, I've successfully tested the array output to file.
The strToUpper isnt that important to me as I have used caps in my define names anyway. It was just to keep the output similar to defines for convention, and maybe even portability if necessary..not a problem though.
--
Simon H