ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
DeleteFile.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\File\Activities;
22
26use ILIAS\Data\Factory as DataFactory;
33use ILIAS\UI\Component\Input\Factory as InputFactory;
35
45{
46 public function __construct(
47 private readonly RepositoryProvider $repository,
48 private readonly Parameter $parameter,
49 private readonly DataFactory $data = new DataFactory(),
50 ) {
51 }
52
53 public function getType(): ActivityType
54 {
56 }
57
59 {
60 return $this->data->text()->markdown()->simpleDocument(
61 "Deletes a file object, exactly like a deletion in the user interface.\n\n"
62 . "If the trash of the installation is enabled, the object is moved there and can be "
63 . "restored by an administrator; otherwise it is removed for good. `in_trash` tells which "
64 . "of the two happened. The user needs the permission to delete the object."
65 );
66 }
67
68 public function getInputDescription(FieldFactory $f): FormInput
69 {
70 return $f->section(
71 [
72 'ref_id' => $f->numeric('File', 'Reference id of the file object.')
73 ->withRequired(true)
74 ->withDedicatedName('ref_id'),
75 ],
76 'Delete a file'
77 );
78 }
79
81 {
82 $text = $this->data->text()->markdown();
83
84 return $f->object(
85 $text->simpleDocument('What has been deleted.'),
86 [
87 'ref_id' => $f->int($text->simpleDocument('Reference id of the deleted object.')),
88 'title' => $f->string($text->simpleDocument('Title of the deleted object.')),
89 'in_trash' => $f->bool($text->simpleDocument(
90 'Whether the object went to the trash and can be restored; false means it is gone for good.'
91 )),
92 ]
93 );
94 }
95
99 public function isAllowedToPerform(int $usr_id, mixed $parameters): bool
100 {
101 // the legacy code doing the work acts as the user of the session, so
102 // the check has to be about that one - see RepositoryProvider::isActingUser()
103 if (!AccessCheck::isKnownUser($usr_id) || !$this->repository->isActingUser($usr_id)) {
104 return false;
105 }
106
107 $ref_id = (int) $parameters['ref_id'];
108
109 if ($this->repository->lookupType($ref_id) !== 'file') {
110 return false;
111 }
112
113 return $this->repository->mayDelete($usr_id, $ref_id, 'file');
114 }
115
119 public function perform(mixed $parameters): DeletedObject
120 {
121 $ref_id = (int) $parameters['ref_id'];
122
123 $entry = $this->repository->getFileEntry($ref_id);
124
125 if ($entry === null) {
126 throw new \OutOfBoundsException("There is no file object {$ref_id}.", 404);
127 }
128
129 $in_trash = $this->repository->delete($ref_id);
130
131 return new DeletedObject($ref_id, $entry->title, $in_trash);
132 }
133
137 public function maybePerformAs(
138 InputFactory $input_factory,
139 int $usr_id,
140 array $raw_parameters
141 ): Result {
142 try {
143 $parameters = $this->toParameters($raw_parameters);
144
145 if (!$this->isAllowedToPerform($usr_id, $parameters)) {
146 return $this->data->error(
147 new \DomainException('You are not allowed to delete this file.', 403)
148 );
149 }
150
151 return $this->data->ok($this->perform($parameters));
152 } catch (\Throwable $e) {
153 return $this->data->error(Failure::of($e));
154 }
155 }
156
162 private function toParameters(array $raw_parameters): array
163 {
164 $ref_id = $this->parameter->id($raw_parameters, 'ref_id', 'A file object is required.');
165
166 return ['ref_id' => $ref_id];
167 }
168}
Basic Implementation for Activities.
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
Builds data types.
Definition: Factory.php:36
The permission rule the file activities share: a file may only be handled by a user the system knows,...
Definition: AccessCheck.php:32
What the file activities hand to Data\Factory::error() when something failed: the result only takes e...
Definition: Failure.php:39
static of(\Throwable $e)
Definition: Failure.php:40
Reads the raw parameters of an activity strictly, with the Refinery.
Definition: Parameter.php:42
Delete a file object.
Definition: DeleteFile.php:45
getInputDescription(FieldFactory $f)
Definition: DeleteFile.php:68
isAllowedToPerform(int $usr_id, mixed $parameters)
Definition: DeleteFile.php:99
toParameters(array $raw_parameters)
Definition: DeleteFile.php:162
maybePerformAs(InputFactory $input_factory, int $usr_id, array $raw_parameters)
Definition: DeleteFile.php:137
__construct(private readonly RepositoryProvider $repository, private readonly Parameter $parameter, private readonly DataFactory $data=new DataFactory(),)
Definition: DeleteFile.php:46
getOutputDescription(Description\Factory $f)
Definition: DeleteFile.php:80
What a deleting activity answers with: enough to tell the user what is gone, and where it went.
The bits of the repository the file activities need: the tree to check where a file may go and access...
try
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is how a factory for inputs looks like.
Definition: Factory.php:27
This is what a factory for input fields looks like.
Definition: Factory.php:31
$data
Definition: storeScorm.php:45
$text
Definition: xapiexit.php:21