frontendNonAdminHiddenPathList

This commit is contained in:
Yuri Kuznetsov
2023-07-18 20:44:40 +03:00
parent d3f768975d
commit acc8eaa4e2
4 changed files with 94 additions and 75 deletions
+7 -73
View File
@@ -165,11 +165,11 @@ class Metadata
/**
* Get all metadata.
*
* @param bool $isJSON
* @param bool $reload
* @return array<string, mixed>|string
* @/param bool $isJSON
* @/param bool $reload
* @/return array<string, mixed>|string
*/
public function getAll(bool $isJSON = false, bool $reload = false)
/*public function getAll(bool $isJSON = false, bool $reload = false)
{
if ($reload) {
$this->init($reload);
@@ -182,7 +182,7 @@ class Metadata
}
return $this->data;
}
}*/
private function objInit(bool $reload = false): void
{
@@ -236,75 +236,9 @@ class Metadata
return Util::getValueByKey($objData, $key, $default);
}
/**
* This method modified loaded metadata object.
* Only to be used in short living request processes.
*/
public function getAllForFrontend(): stdClass
public function getAll(): stdClass
{
$data = $this->getObjData();
$frontendHiddenPathList = $this->get(['app', 'metadata', 'frontendHiddenPathList'], []);
foreach ($frontendHiddenPathList as $row) {
$this->removeDataByPath($row, $data);
}
return $data;
}
/**
*
* @param string[] $row
* @param stdClass $data
*/
private function removeDataByPath($row, &$data): void
{
$p = &$data;
$path = [&$p];
foreach ($row as $i => $item) {
if (is_array($item)) {
break;
}
if ($item === self::ANY_KEY) {
foreach (get_object_vars($p) as &$v) {
$this->removeDataByPath(
array_slice($row, $i + 1),
$v
);
}
return;
}
if (!property_exists($p, $item)) {
break;
}
if ($i == count($row) - 1) {
unset($p->$item);
$o = &$p;
for ($j = $i - 1; $j > 0; $j--) {
if (is_object($o) && !count(get_object_vars($o))) {
$o = &$path[$j];
$k = $row[$j];
unset($o->$k);
}
else {
break;
}
}
}
else {
$p = &$p->$item;
$path[] = &$p;
}
}
return $this->getObjData();
}
/**
@@ -35,10 +35,12 @@
["app", "recordId"],
["app", "currencyConversion"],
["selectDefs"],
["recordDefs"],
["pdfDefs"],
["aclDefs"],
["notificationDefs"],
["authenticationMethods", "__ANY__", "implementationClassName"]
],
"frontendNonAdminHiddenPathList": [
["recordDefs"]
]
}
+69 -1
View File
@@ -38,6 +38,8 @@ use stdClass;
class MetadataService
{
private const ANY_KEY = '__ANY__';
public function __construct(
private Acl $acl,
private MetadataUtil $metadata,
@@ -47,12 +49,24 @@ class MetadataService
public function getDataForFrontend(): stdClass
{
$data = $this->metadata->getAllForFrontend();
$data = $this->metadata->getAll();
$hiddenPathList = $this->metadata->get(['app', 'metadata', 'frontendHiddenPathList'], []);
foreach ($hiddenPathList as $row) {
$this->removeDataByPath($row, $data);
}
if ($this->user->isAdmin()) {
return $data;
}
$hiddenPathList = $this->metadata->get(['app', 'metadata', 'frontendNonAdminHiddenPathList'], []);
foreach ($hiddenPathList as $row) {
$this->removeDataByPath($row, $data);
}
/** @var string[] $scopeList */
$scopeList = array_keys($this->metadata->get(['entityDefs'], []));
@@ -196,4 +210,58 @@ class MetadataService
return $data;
}
/**
*
* @param string[] $row
* @param stdClass $data
*/
private function removeDataByPath($row, &$data): void
{
$p = &$data;
$path = [&$p];
foreach ($row as $i => $item) {
if (is_array($item)) {
break;
}
if ($item === self::ANY_KEY) {
foreach (get_object_vars($p) as &$v) {
$this->removeDataByPath(
array_slice($row, $i + 1),
$v
);
}
return;
}
if (!property_exists($p, $item)) {
break;
}
if ($i == count($row) - 1) {
unset($p->$item);
$o = &$p;
for ($j = $i - 1; $j > 0; $j--) {
if (is_object($o) && !count(get_object_vars($o))) {
$o = &$path[$j];
$k = $row[$j];
unset($o->$k);
}
else {
break;
}
}
}
else {
$p = &$p->$item;
$path[] = &$p;
}
}
}
}
+15
View File
@@ -20,6 +20,21 @@
]
}
},
"frontendNonAdminHiddenPathList": {
"type": "array",
"description": "Sections of metadata (defined as paths) to be hidden from the front-end for non-admin users.",
"items": {
"anyOf": [
{"const": "__APPEND__"},
{
"type": "array",
"items": {
"type": "string"
}
}
]
}
},
"aclDependencies": {
"type": "object",
"description": "Rules making a metadata sections available for a user when they don't have access to a scope.",