Skip to content

Commit e41dc58

Browse files
committed
Entitlements logic improve - final.
1 parent 0672abc commit e41dc58

6 files changed

Lines changed: 75 additions & 75 deletions

File tree

modules/quanthub_core/quanthub_core.module

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Drupal\Core\Form\FormStateInterface;
1313
use Drupal\Core\Session\AccountInterface;
1414
use Drupal\Core\Url;
1515
use Drupal\node\NodeInterface;
16+
use Drupal\quanthub_core\AllowedContentManager;
1617
use Drupal\quanthub_core\Form\PowerBiMediaDirectoriesForm;
1718
use Drupal\quanthub_core\Form\PowerBIMediaForm;
1819

@@ -23,7 +24,7 @@ function quanthub_core_node_access(NodeInterface $node, $operation, AccountInter
2324
if (
2425
$operation !== 'view' ||
2526
\Drupal::currentUser()->id() !== $account->id() ||
26-
!$node->hasField('field_quanthub_urn') ||
27+
!$node->hasField(AllowedContentManager::URN_FIELD) ||
2728
getenv('WSO_IGNORE') === 'TRUE'
2829
) {
2930
return AccessResult::neutral();
@@ -37,7 +38,7 @@ function quanthub_core_node_access(NodeInterface $node, $operation, AccountInter
3738
$allowed_datasets = \Drupal::service('allowed_content_manager')
3839
->getAllowedDatasetList();
3940
$node_datasets = [];
40-
$field = $node->get('field_quanthub_urn');
41+
$field = $node->get(AllowedContentManager::URN_FIELD);
4142
if ($field instanceof CacheableDependencyInterface) {
4243
$cacheable_metadata->addCacheableDependency($field);
4344
}
@@ -61,6 +62,7 @@ function quanthub_core_node_access(NodeInterface $node, $operation, AccountInter
6162
* Implements hook_views_data_alter().
6263
*/
6364
function quanthub_core_views_data_alter(array &$data) {
65+
$field_name = AllowedContentManager::URN_FIELD;
6466
$base = [
6567
'title' => t('Allowed Content Filter'),
6668
'help' => t('Content Filter By Dataset Urn'),
@@ -71,21 +73,21 @@ function quanthub_core_views_data_alter(array &$data) {
7173
],
7274
];
7375

74-
if (!empty($data['node__field_quanthub_urn']['field_quanthub_urn_value'])) {
75-
$table = 'node__field_quanthub_urn';
76+
if (!empty($data["node__$field_name"]["{$field_name}_value"])) {
77+
$table = "node__$field_name";
7678
$data[$table]['allowed_content_filter'] = $base;
7779
$data[$table]['allowed_content_filter']['filter']['id'] = 'allowed_content_filter';
78-
$data[$table]['allowed_content_filter']['filter'] += $data[$table]['field_quanthub_urn_value']['filter'];
80+
$data[$table]['allowed_content_filter']['filter'] += $data[$table]["{$field_name}_value"]['filter'];
7981
}
8082

8183
foreach (array_keys($data) as $table) {
82-
if (!str_starts_with($table, 'search_api_index') || empty($data[$table]['field_quanthub_urn'])) {
84+
if (!str_starts_with($table, 'search_api_index') || empty($data[$table][$field_name])) {
8385
continue;
8486
}
8587
$data[$table]['allowed_content_filter'] = $base;
86-
$data[$table]['allowed_content_filter']['real field'] = 'field_quanthub_urn';
88+
$data[$table]['allowed_content_filter']['real field'] = $field_name;
8789
$data[$table]['allowed_content_filter']['filter']['id'] = 'allowed_content_filter_search_api';
88-
$data[$table]['allowed_content_filter']['filter'] += $data[$table]['field_quanthub_urn']['filter'];
90+
$data[$table]['allowed_content_filter']['filter'] += $data[$table][$field_name]['filter'];
8991
}
9092
}
9193

@@ -277,12 +279,3 @@ function quanthub_core_language_switch_links_alter(array &$links, $type, Url $ur
277279
}
278280
}
279281
}
280-
281-
/**
282-
* Workaround to fix the CodeSniffer.
283-
*
284-
* @todo review how to fix the circular dependency without it.
285-
*/
286-
function quanthub_core_container() {
287-
return \Drupal::getContainer();
288-
}

modules/quanthub_core/quanthub_core.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
# Managing allowed content in user data. Work sdmx_client.
2525
allowed_content_manager:
2626
class: '\Drupal\quanthub_core\AllowedContentManager'
27-
arguments: ['@current_user', '@cache.default', '@language_manager', '@datetime.time']
27+
arguments: ['@current_user', '@cache.default', '@language_manager', '@datetime.time', '@entity_type.manager']
2828

2929
# Event subscriber for getting and storing user info attributes for authenticated user.
3030
user_info_attributes_subscriber:

modules/quanthub_core/src/AllowedContentManager.php

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Drupal\quanthub_core;
44

55
use Drupal\Component\Datetime\Time;
6-
use Drupal\Component\DependencyInjection\ContainerInterface;
76
use Drupal\Core\Cache\CacheBackendInterface;
7+
use Drupal\Core\Entity\EntityTypeManagerInterface;
88
use Drupal\Core\Language\LanguageManager;
99
use Drupal\Core\Session\AccountProxy;
1010

@@ -16,29 +16,15 @@
1616
class AllowedContentManager implements QuanthubCoreInterface {
1717

1818
/**
19-
* Name of user data argument for datasets.
19+
* Dataset URN field name.
2020
*/
21-
const USER_DATA_DATASETS = 'allowed_datasets';
21+
const URN_FIELD = 'field_quanthub_urn';
2222

2323
/**
2424
* The 15 minutes cache time.
2525
*/
2626
const CACHE_TIME = 900;
2727

28-
/**
29-
* The (lazy loaded) dependency injection (DI) container.
30-
*
31-
* @var ?\Drupal\Component\DependencyInjection\ContainerInterface
32-
*/
33-
protected ?ContainerInterface $container;
34-
35-
/**
36-
* The (lazy loaded) SDMX client.
37-
*
38-
* @var ?\Drupal\quanthub_core\QuanthubSdmxClient
39-
*/
40-
protected ?QuanthubSdmxClient $quanthubSdmxClient;
41-
4228
/**
4329
* The time service.
4430
*
@@ -68,11 +54,18 @@ class AllowedContentManager implements QuanthubCoreInterface {
6854
protected $languageManager;
6955

7056
/**
71-
* Dataset List.
57+
* The node storage.
58+
*
59+
* @var \Drupal\node\NodeStorageInterface
60+
*/
61+
protected $nodeStorage;
62+
63+
/**
64+
* The prohibited datasets cache.
7265
*
7366
* @var array
7467
*/
75-
protected $datasets = [];
68+
private $datasets = [];
7669

7770
/**
7871
* {@inheritDoc}
@@ -82,11 +75,13 @@ public function __construct(
8275
CacheBackendInterface $cache,
8376
LanguageManager $language_manager,
8477
Time $time,
78+
EntityTypeManagerInterface $entity_type_manager,
8579
) {
8680
$this->currentUser = $current_user;
8781
$this->cache = $cache;
8882
$this->languageManager = $language_manager;
8983
$this->time = $time;
84+
$this->nodeStorage = $entity_type_manager->getStorage('node');
9085
}
9186

9287
/**
@@ -97,32 +92,59 @@ public function getAllowedDatasetList() {
9792
// Check that dataset list is not already saved to cache.
9893
if ($cache = $this->cache->get($this->getCacheCid())) {
9994
if (!empty($cache->data)) {
100-
$this->datasets = $cache->data;
95+
$datasets = $cache->data;
10196
}
10297
else {
103-
$this->datasets = [];
98+
$datasets = [];
10499
}
105100
}
106101
else {
107-
$this->datasets = $this->getUserDatasetList();
102+
$datasets = $this->getUserDatasetList();
108103

109104
// Support latest version.
110-
foreach ($this->datasets as $dataset) {
105+
foreach ($datasets as $dataset) {
111106
$latest_dataset = preg_replace('/\(.+\)$/', '(~)', $dataset);
112107
if ($latest_dataset !== $dataset) {
113-
$this->datasets[] = $latest_dataset;
108+
$datasets[] = $latest_dataset;
114109
}
115110
}
116111

117112
// Update datasets in cache.
118113
$this->cache->set(
119114
$this->getCacheCid(),
120-
$this->datasets,
115+
$datasets,
121116
$this->time->getCurrentTime() + $this::CACHE_TIME
122117
);
123118
}
124119

125-
return $this->datasets;
120+
return $datasets;
121+
}
122+
123+
/**
124+
* Gets prohibited datasets list.
125+
*
126+
* The list is calculated from DB data based on URN default field
127+
* or user specified one.
128+
*/
129+
public function getProhibitedDatasetList($field_name = self::URN_FIELD) {
130+
if (!isset($this->datasets[$field_name])) {
131+
$datasets = [];
132+
$result = $this->nodeStorage->getAggregateQuery()
133+
->accessCheck(FALSE)
134+
->condition('status', 1)
135+
->exists($field_name)
136+
->groupby($field_name)
137+
->execute();
138+
foreach ($result as $item) {
139+
if (!empty($item[$field_name])) {
140+
$datasets[] = $item[$field_name];
141+
}
142+
}
143+
144+
$this->datasets[$field_name] = array_diff($datasets, $this->getAllowedDatasetList());
145+
}
146+
147+
return $this->datasets[$field_name];
126148
}
127149

128150
/**
@@ -139,20 +161,7 @@ public function getCacheCid() {
139161
* Get User's Dataset List.
140162
*/
141163
public function getUserDatasetList() {
142-
return $this->quanthubSdmxClient()->getDatasetList();
143-
}
144-
145-
/**
146-
* Get Dependency Injection container.
147-
*
148-
* @return \Drupal\Component\DependencyInjection\ContainerInterface
149-
* Current Dependency Injection container.
150-
*/
151-
protected function getContainer(): ContainerInterface {
152-
if (!isset($this->container)) {
153-
$this->container = quanthub_core_container();
154-
}
155-
return $this->container;
164+
return self::quanthubSdmxClient()->getDatasetList();
156165
}
157166

158167
/**
@@ -161,11 +170,8 @@ protected function getContainer(): ContainerInterface {
161170
* @return \Drupal\quanthub_core\QuanthubSdmxClient
162171
* QuanthubSdmxClient service.
163172
*/
164-
protected function quanthubSdmxClient(): QuanthubSdmxClient {
165-
if (!isset($this->quanthubSdmxClient)) {
166-
$this->quanthubSdmxClient = $this->getContainer()->get('sdmx_client');
167-
}
168-
return $this->quanthubSdmxClient;
173+
protected static function quanthubSdmxClient(): QuanthubSdmxClient {
174+
return \Drupal::service('sdmx_client');
169175
}
170176

171177
}

modules/quanthub_core/src/Plugin/Field/FieldType/QuantHubUrnComputedList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
99
use Drupal\Core\Field\FieldItemList;
1010
use Drupal\Core\TypedData\ComputedItemListTrait;
11+
use Drupal\quanthub_core\AllowedContentManager;
1112

1213
/**
1314
* Computed field to proxy QuantHub URN from references.
@@ -37,14 +38,14 @@ protected function computeValue() {
3738
foreach ($references->referencedEntities() as $referencedEntity) {
3839
if (
3940
!$referencedEntity instanceof FieldableEntityInterface ||
40-
!$referencedEntity->hasField('field_quanthub_urn')
41+
!$referencedEntity->hasField(AllowedContentManager::URN_FIELD)
4142
) {
4243
continue;
4344
}
4445

4546
$this->addCacheableDependency($referencedEntity);
4647

47-
$field = $referencedEntity->get('field_quanthub_urn');
48+
$field = $referencedEntity->get(AllowedContentManager::URN_FIELD);
4849
if ($field instanceof CacheableDependencyInterface) {
4950
$this->addCacheableDependency($field);
5051
}

modules/quanthub_core/src/Plugin/views/filter/AllowedContentFilter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,18 @@ protected function getNodeTypesMapping() {
165165
/** @var \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping */
166166
$table_mapping = $this->entityTypeManager->getStorage('node')->getTableMapping();
167167
$bundles = $this->entityFieldManager->getFieldMap()['node']['nid']['bundles'];
168+
$field_name = $this->definition['field_name'] ?? $this->allowedContentManager::URN_FIELD;
168169

169170
foreach ($bundles as $bundle) {
170171
$definitions = $this->entityFieldManager->getFieldDefinitions('node', $bundle);
171-
if (empty($definitions['field_quanthub_urn'])) {
172+
if (empty($definitions[$field_name])) {
172173
continue;
173174
}
174-
if (!$definitions['field_quanthub_urn']->isComputed()) {
175+
if (!$definitions[$field_name]->isComputed()) {
175176
$mapping['_root']['bundles'][$bundle] = $bundle;
176177
continue;
177178
}
178-
$base_field = $definitions['field_quanthub_urn']->getSetting('field_reference_name');
179+
$base_field = $definitions[$field_name]->getSetting('field_reference_name');
179180
if ($base_field && $definitions[$base_field]->getFieldStorageDefinition()->getMainPropertyName() === 'target_id') {
180181
$mapping[$base_field]['table'] = $table_mapping->getFieldTableName($base_field);
181182
$mapping[$base_field]['data_table'] = $table_mapping->getDataTable() ?? $table_mapping->getBaseTable();

modules/quanthub_core/src/Plugin/views/filter/AllowedContentFilterSearchApi.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ public function query() {
2929

3030
$this->ensureMyTable();
3131

32-
$conditions = $this->getQuery()->createConditionGroup('OR');
33-
$conditions->addCondition($this->realField, NULL);
34-
35-
if ($datasets = $this->allowedContentManager->getAllowedDatasetList()) {
36-
$conditions->addCondition($this->realField, $datasets, 'IN');
32+
if ($this->allowedContentManager->getAllowedDatasetList()) {
33+
$datasets = $this->allowedContentManager->getProhibitedDatasetList();
34+
$this->query->addWhere($this->options['group'], $this->realField, $datasets, 'NOT IN');
35+
}
36+
else {
37+
$this->query->addWhere($this->options['group'], $this->realField, NULL, 'IS NULL');
3738
}
38-
39-
$this->query->addConditionGroup($conditions, $this->options['group']);
4039
}
4140

4241
}

0 commit comments

Comments
 (0)