2
2
3
3
namespace DataKit \DataViews \DataView ;
4
4
5
+ use DataKit \DataViews \ACL \AccessControlManager ;
6
+ use DataKit \DataViews \ACL \Capability ;
5
7
use DataKit \DataViews \Data \DataSource ;
6
8
use DataKit \DataViews \Data \Exception \DataSourceException ;
7
9
use DataKit \DataViews \Data \MutableDataSource ;
@@ -354,7 +356,7 @@ public function get_data( ?DataSource $data_source = null, ?Pagination $paginati
354
356
*/
355
357
$ data = $ data_source ->get_data_by_id ( $ data_id );
356
358
357
- foreach ( $ this ->directory_fields as $ field ) {
359
+ foreach ( $ this ->allowed_fields ( $ this -> directory_fields ) as $ field ) {
358
360
$ data [ $ field ->uuid () ] = $ field ->get_value ( $ data );
359
361
}
360
362
@@ -377,31 +379,32 @@ public function get_data( ?DataSource $data_source = null, ?Pagination $paginati
377
379
* @throws DataSourceException When the data source encounters an issue.
378
380
*/
379
381
public function get_view_data_item ( string $ data_id ): DataItem {
380
- $ data = $ this ->data_source ()->get_data_by_id ( $ data_id );
382
+ $ data = $ this ->data_source ()->get_data_by_id ( $ data_id );
383
+ $ fields = $ this ->allowed_fields ( $ this ->view_fields );
381
384
382
- foreach ( $ this -> view_fields as $ field ) {
385
+ foreach ( $ fields as $ field ) {
383
386
$ data [ $ field ->uuid () ] = $ field ->get_value ( $ data );
384
387
}
385
388
386
389
return DataItem::from_array (
387
390
[
388
- 'fields ' => $ this -> view_fields ,
391
+ 'fields ' => $ fields ,
389
392
'data ' => $ data ,
390
393
],
391
394
);
392
395
}
393
396
394
397
/**
395
- * Returns all the fields for the dictionary view.
398
+ * Returns all the fields for the directory view.
396
399
*
397
400
* @since $ver$
398
401
*
399
402
* @return array[] The fields as arrays.
400
403
*/
401
- private function dictionary_fields (): array {
404
+ private function directory_fields_for_json (): array {
402
405
$ fields = [];
403
406
404
- foreach ( $ this ->directory_fields as $ field ) {
407
+ foreach ( $ this ->allowed_fields ( $ this -> directory_fields ) as $ field ) {
405
408
$ fields [] = array_filter (
406
409
$ field ->to_array (),
407
410
static fn ( $ value ) => ! is_null ( $ value ),
@@ -448,7 +451,7 @@ private function default_layouts(): array {
448
451
private function get_field_ids ( ?callable $ filter = null ): array {
449
452
$ field_ids = [];
450
453
451
- foreach ( $ this ->directory_fields as $ field ) {
454
+ foreach ( $ this ->allowed_fields ( $ this -> directory_fields ) as $ field ) {
452
455
if ( $ filter && ! $ filter ( $ field ) ) {
453
456
continue ;
454
457
}
@@ -542,7 +545,7 @@ public function to_array(): array {
542
545
'defaultLayouts ' => $ this ->default_layouts (),
543
546
'paginationInfo ' => $ this ->pagination ->info ( $ this ->data_source () ),
544
547
'view ' => $ this ->view (),
545
- 'fields ' => $ this ->dictionary_fields (),
548
+ 'fields ' => $ this ->directory_fields_for_json (),
546
549
'data ' => $ this ->get_data (),
547
550
'actions ' => $ this ->actions ? $ this ->actions ->to_array () : [],
548
551
];
@@ -734,4 +737,22 @@ private function get_media_field_id(): string {
734
737
735
738
return $ image_fields ? reset ( $ image_fields ) : '' ;
736
739
}
740
+
741
+ /**
742
+ * Filters out the fields the current user cannot view.
743
+ *
744
+ * @since $ver$
745
+ *
746
+ * @return Field[] The fields.
747
+ */
748
+ private function allowed_fields ( array $ fields ): array {
749
+ return array_filter (
750
+ $ fields ,
751
+ fn ( Field $ field ) => AccessControlManager::current ()->can (
752
+ Capability::view_dataview_field (),
753
+ $ this ,
754
+ $ field
755
+ )
756
+ );
757
+ }
737
758
}
0 commit comments