entityAcl. */ class GlobalRestriction { /** Totally forbidden. */ public const TYPE_FORBIDDEN = 'forbidden'; /** Reading forbidden, writing allowed. */ public const TYPE_INTERNAL = 'internal'; /** Forbidden for non-admin users. */ public const TYPE_ONLY_ADMIN = 'onlyAdmin'; /** Read-only for all users. */ public const TYPE_READ_ONLY = 'readOnly'; /** Read-only for non-admin users. */ public const TYPE_NON_ADMIN_READ_ONLY = 'nonAdminReadOnly'; /** * @var array */ private $fieldTypeList = [ self::TYPE_FORBIDDEN, self::TYPE_INTERNAL, self::TYPE_ONLY_ADMIN, self::TYPE_READ_ONLY, self::TYPE_NON_ADMIN_READ_ONLY, ]; /** * @var array */ private $linkTypeList = [ self::TYPE_FORBIDDEN, self::TYPE_INTERNAL, self::TYPE_ONLY_ADMIN, self::TYPE_READ_ONLY, self::TYPE_NON_ADMIN_READ_ONLY, ]; /** * Types that should also be taken from entityDefs. * @var array */ private array $entityDefsTypeList = [ self::TYPE_READ_ONLY, ]; private ?stdClass $data = null; private string $cacheKey = 'entityAcl'; private Metadata $metadata; private DataCache $dataCache; private FieldUtil $fieldUtil; public function __construct( Metadata $metadata, DataCache $dataCache, FieldUtil $fieldUtil, Config $config ) { $this->metadata = $metadata; $this->dataCache = $dataCache; $this->fieldUtil = $fieldUtil; $useCache = $config->get('useCache'); if ($useCache && $this->dataCache->has($this->cacheKey)) { /** @var stdClass */ $cachedData = $this->dataCache->get($this->cacheKey); $this->data = $cachedData; return; } if (!$this->data) { $this->buildData(); } if ($useCache) { $this->storeCacheFile(); } } private function storeCacheFile(): void { assert($this->data !== null); $this->dataCache->store($this->cacheKey, $this->data); } private function buildData(): void { /** @var string[] */ $scopeList = array_keys($this->metadata->get(['entityDefs']) ?? []); $data = (object) []; foreach ($scopeList as $scope) { /** @var string[] */ $fieldList = array_keys($this->metadata->get(['entityDefs', $scope, 'fields']) ?? []); /** @var string[] */ $linkList = array_keys($this->metadata->get(['entityDefs', $scope, 'links']) ?? []); $isNotEmpty = false; $scopeData = (object) [ 'fields' => (object) [], 'attributes' => (object) [], 'links' => (object) [], ]; foreach ($this->fieldTypeList as $type) { $resultFieldList = []; $resultAttributeList = []; foreach ($fieldList as $field) { $value = $this->metadata->get(['entityAcl', $scope, 'fields', $field, $type]); if (!$value && in_array($type, $this->entityDefsTypeList)) { $value = $this->metadata->get(['entityDefs', $scope, 'fields', $field, $type]); } if (!$value) { continue; } $isNotEmpty = true; $resultFieldList[] = $field; $fieldAttributeList = $this->fieldUtil->getAttributeList($scope, $field); foreach ($fieldAttributeList as $attribute) { $resultAttributeList[] = $attribute; } } $scopeData->fields->$type = $resultFieldList; $scopeData->attributes->$type = $resultAttributeList; } foreach ($this->linkTypeList as $type) { $resultLinkList = []; foreach ($linkList as $link) { if ($this->metadata->get(['entityAcl', $scope, 'links', $link, $type])) { $isNotEmpty = true; $resultLinkList[] = $link; } } $scopeData->links->$type = $resultLinkList; } if ($isNotEmpty) { $data->$scope = $scopeData; } } $this->data = $data; } /** * @param self::TYPE_* $type * @return string[] */ public function getScopeRestrictedFieldList(string $scope, string $type): array { assert($this->data !== null); if (!property_exists($this->data, $scope)) { return []; } if (!property_exists($this->data->$scope, 'fields')) { return []; } if (!property_exists($this->data->$scope->fields, $type)) { return []; } return $this->data->$scope->fields->$type; } /** * @param self::TYPE_* $type * @return string[] */ public function getScopeRestrictedAttributeList(string $scope, string $type): array { assert($this->data !== null); if (!property_exists($this->data, $scope)) { return []; } if (!property_exists($this->data->$scope, 'attributes')) { return []; } if (!property_exists($this->data->$scope->attributes, $type)) { return []; } return $this->data->$scope->attributes->$type; } /** * @param self::TYPE_* $type * @return string[] */ public function getScopeRestrictedLinkList(string $scope, string $type): array { assert($this->data !== null); if (!property_exists($this->data, $scope)) { return []; } if (!property_exists($this->data->$scope, 'links')) { return []; } if (!property_exists($this->data->$scope->links, $type)) { return []; } return $this->data->$scope->links->$type; } }