|
1
|
|
|
<?php |
|
2
|
|
|
namespace SilverStripe\AssetAdmin\GraphQL; |
|
3
|
|
|
|
|
4
|
|
|
use SilverStripe\Assets\File; |
|
5
|
|
|
use SilverStripe\Assets\Folder; |
|
6
|
|
|
use GraphQL\Type\Definition\UnionType; |
|
7
|
|
|
use SilverStripe\GraphQL\Pagination\PaginatedQueryCreator; |
|
8
|
|
|
use SilverStripe\GraphQL\Pagination\Connection; |
|
9
|
|
|
use SilverStripe\ORM\Versioning\Versioned; |
|
10
|
|
|
|
|
11
|
|
|
class ReadFileQueryCreator extends PaginatedQueryCreator |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function attributes() |
|
15
|
|
|
{ |
|
16
|
|
|
return [ |
|
17
|
|
|
'name' => 'readFiles' |
|
18
|
|
|
]; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function createConnection() |
|
22
|
|
|
{ |
|
23
|
|
|
return Connection::create('readFiles') |
|
24
|
|
|
->setConnectionType(function () { |
|
25
|
|
|
$unionType = new UnionType([ |
|
26
|
|
|
'name' => 'Result', |
|
27
|
|
|
'types' => [ |
|
28
|
|
|
$this->manager->getType('File'), |
|
29
|
|
|
$this->manager->getType('Folder') |
|
30
|
|
|
], |
|
31
|
|
View Code Duplication |
'resolveType' => function ($object) { |
|
|
|
|
|
|
32
|
|
|
if ($object instanceof Folder) { |
|
33
|
|
|
return $this->manager->getType('Folder'); |
|
34
|
|
|
} |
|
35
|
|
|
if ($object instanceof File) { |
|
36
|
|
|
return $this->manager->getType('File'); |
|
37
|
|
|
} |
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
]); |
|
41
|
|
|
return $unionType; |
|
42
|
|
|
}) |
|
43
|
|
|
->setArgs(function () { |
|
44
|
|
|
return [ |
|
45
|
|
|
'filter' => [ |
|
46
|
|
|
'type' => $this->manager->getType('FileFilterInput') |
|
47
|
|
|
] |
|
48
|
|
|
]; |
|
49
|
|
|
}) |
|
50
|
|
|
->setSortableFields(['ID', 'Title', 'Created', 'LastEdited']) |
|
51
|
|
|
->setConnectionResolver(array($this, 'resolveConnection')); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function resolveConnection($object, array $args, $context, $info) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$filter = (!empty($args['filter'])) ? $args['filter'] : []; |
|
57
|
|
|
|
|
58
|
|
|
// Permission checks |
|
59
|
|
|
$parent = Folder::singleton(); |
|
60
|
|
|
if (isset($filter['parentId']) && $filter['parentId'] !== 0) { |
|
61
|
|
|
$parent = Folder::get()->byID($filter['parentId']); |
|
62
|
|
|
if (!$parent) { |
|
63
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
64
|
|
|
'%s#%s not found', |
|
65
|
|
|
Folder::class, |
|
66
|
|
|
$filter['parentId'] |
|
67
|
|
|
)); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
if (!$parent->canView($context['currentUser'])) { |
|
71
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
72
|
|
|
'%s#%s view access not permitted', |
|
73
|
|
|
Folder::class, |
|
74
|
|
|
$parent->ID |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (isset($filter['recursive']) && $filter['recursive']) { |
|
79
|
|
|
throw new \InvalidArgumentException(( |
|
80
|
|
|
'The "recursive" flag can only be used for the "children" field' |
|
81
|
|
|
)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Filter list |
|
85
|
|
|
$list = Versioned::get_by_stage(File::class, Versioned::DRAFT); |
|
86
|
|
|
$filterInputType = new FileFilterInputTypeCreator($this->manager); |
|
87
|
|
|
$list = $filterInputType->filterList($list, $filter); |
|
88
|
|
|
|
|
89
|
|
|
// Permission checks |
|
90
|
|
|
$list = $list->filterByCallback(function (File $file) use ($context) { |
|
91
|
|
|
return $file->canView($context['currentUser']); |
|
92
|
|
|
}); |
|
93
|
|
|
|
|
94
|
|
|
return $list; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|