Skip to content

Commit f88153b

Browse files
committed
Making available extension listed in the instance structure (view).
1 parent 893c561 commit f88153b

File tree

7 files changed

+62
-30
lines changed

7 files changed

+62
-30
lines changed

app/V1Module/presenters/InstancesPresenter.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
class InstancesPresenter extends BasePresenter
2727
{
28-
2928
/**
3029
* @var Instances
3130
* @inject
@@ -94,7 +93,9 @@ function (Instance $instance) {
9493
return $instance->isAllowed();
9594
}
9695
);
97-
$this->sendSuccessResponse($this->instanceViewFactory->getInstances($instances));
96+
$this->sendSuccessResponse(
97+
$this->instanceViewFactory->getInstances($instances, $this->getCurrentUserOrNull())
98+
);
9899
}
99100

100101
public function checkCreateInstance()
@@ -109,7 +110,8 @@ public function checkCreateInstance()
109110
* @POST
110111
* @Param(type="post", name="name", validation="string:2..", description="Name of the instance")
111112
* @Param(type="post", name="description", required=false, description="Description of the instance")
112-
* @Param(type="post", name="isOpen", validation="bool", description="Should the instance be open for registration?")
113+
* @Param(type="post", name="isOpen", validation="bool",
114+
* description="Should the instance be open for registration?")
113115
* @throws ForbiddenRequestException
114116
*/
115117
public function actionCreateInstance()
@@ -129,7 +131,7 @@ public function actionCreateInstance()
129131
$this->instances->persist($instance->getRootGroup(), false);
130132
$this->instances->persist($localizedRootGroup, false);
131133
$this->instances->persist($instance);
132-
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance), IResponse::S201_CREATED);
134+
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance, $user), IResponse::S201_CREATED);
133135
}
134136

135137
public function checkUpdateInstance(string $id)
@@ -144,7 +146,8 @@ public function checkUpdateInstance(string $id)
144146
/**
145147
* Update an instance
146148
* @POST
147-
* @Param(type="post", name="isOpen", validation="bool", required=false, description="Should the instance be open for registration?")
149+
* @Param(type="post", name="isOpen", validation="bool", required=false,
150+
* description="Should the instance be open for registration?")
148151
* @param string $id An identifier of the updated instance
149152
*/
150153
public function actionUpdateInstance(string $id)
@@ -159,7 +162,7 @@ public function actionUpdateInstance(string $id)
159162

160163
$instance->setIsOpen($isOpen);
161164
$this->instances->persist($instance);
162-
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance));
165+
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance, $this->getCurrentUser()));
163166
}
164167

165168
public function checkDeleteInstance(string $id)
@@ -208,7 +211,7 @@ public function checkDetail(string $id)
208211
public function actionDetail(string $id)
209212
{
210213
$instance = $this->instances->findOrThrow($id);
211-
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance));
214+
$this->sendSuccessResponse($this->instanceViewFactory->getInstance($instance, $this->getCurrentUser()));
212215
}
213216

214217
public function checkLicences(string $id)
@@ -268,9 +271,12 @@ public function checkUpdateLicence(string $licenceId)
268271
/**
269272
* Update an existing license for an instance
270273
* @POST
271-
* @Param(type="post", name="note", validation="string:2..255", required=false, description="A note for users or administrators")
272-
* @Param(type="post", name="validUntil", validation="string", required=false, description="Expiration date of the license")
273-
* @Param(type="post", name="isValid", validation="bool", required=false, description="Administrator switch to toggle licence validity")
274+
* @Param(type="post", name="note", validation="string:2..255", required=false,
275+
* description="A note for users or administrators")
276+
* @Param(type="post", name="validUntil", validation="string", required=false,
277+
* description="Expiration date of the license")
278+
* @Param(type="post", name="isValid", validation="bool", required=false,
279+
* description="Administrator switch to toggle licence validity")
274280
* @param string $licenceId Identifier of the licence
275281
* @throws NotFoundException
276282
*/

app/V1Module/presenters/UsersPresenter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,10 @@ public function actionInstances(string $id)
655655
{
656656
$user = $this->users->findOrThrow($id);
657657

658-
$this->sendSuccessResponse($this->instanceViewFactory->getInstances($user->getInstances()->toArray()));
658+
$this->sendSuccessResponse($this->instanceViewFactory->getInstances(
659+
$user->getInstances()->toArray(),
660+
$this->getCurrentUser()
661+
));
659662
}
660663

661664
public function checkSetRole(string $id)

app/commands/PlagiarismDetectionAccessToken.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Console;
44

5-
use App\Model\Entity\User;
65
use App\Model\Repository\Users;
76
use App\Security\AccessManager;
87
use App\Security\TokenScope;

app/config/config.local.neon.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ parameters:
8282
en: "English Caption"
8383
url: "https://extetrnal.domain.com/recodex/extension?token={token}&locale={locale}" # '{token}' and '{locale}' are placeholders
8484
token: # generated from tmp tokens passed via URL so the ext. tool can access ReCodEx API
85-
scope: master # scope of generated tokens (to be used by the extension)
85+
scopes: [ 'master', 'refresh' ] # list of scopes for generated tokens (to be used by the extension)
8686
user: null # user override (ID) for generating tokens (if null, the token will be generated for logged-in user)
8787
instances: [] # array of instances where this extension is enabled (empty array = all)
8888
user: # filters applied to determine, whether logged-in user can access the extension

app/helpers/Extensions/ExtensionConfig.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Model\Entity\Instance;
66
use App\Model\Entity\User;
77
use App\Exceptions\ConfigException;
8+
use App\Security\TokenScope;
89
use Nette;
910
use Nette\Utils\Arrays;
1011

@@ -34,9 +35,10 @@ class ExtensionConfig
3435
private string $url;
3536

3637
/**
37-
* A scope that will be set to (full) access tokens generated after tmp-token verification.
38+
* List of scopes that will be set to (full) access tokens generated after tmp-token verification.
39+
* @var string[]
3840
*/
39-
private string $tokenScope;
41+
private array $tokenScopes;
4042

4143
/**
4244
* User override for (full) access tokens. This user will be used instead of user ID passed in tmp token.
@@ -79,11 +81,15 @@ public function __construct(array $config)
7981
}
8082

8183
$this->url = Arrays::get($config, "url");
82-
$this->tokenScope = Arrays::get($config, ["token", "scope"], "master");
84+
$this->tokenScopes = Arrays::get(
85+
$config,
86+
["token", "scopes"],
87+
[ TokenScope::MASTER, TokenScope::REFRESH ]
88+
) ?? [];
8389
$this->tokenUserId = Arrays::get($config, ["token", "user"], null);
84-
$this->instances = Arrays::get($config, "instances", []);
85-
$this->userRoles = Arrays::get($config, ["user", "roles"], []);
86-
$this->userExternalLogins = Arrays::get($config, ["user", "externalLogins"], []);
90+
$this->instances = Arrays::get($config, "instances", []) ?? [];
91+
$this->userRoles = Arrays::get($config, ["user", "roles"], []) ?? [];
92+
$this->userExternalLogins = Arrays::get($config, ["user", "externalLogins"], []) ?? [];
8793
}
8894

8995
public function getId(): string
@@ -113,15 +119,20 @@ public function getUrl(string $token, string $locale): string
113119
/**
114120
* Check whether this extension is accessible by given user in given instance.
115121
* @param Instance $instance
116-
* @param User $user
122+
* @param User|null $user (if null, the extension must be accessible by all users)
117123
* @return bool true if the extension is accessible
118124
*/
119-
public function isAccessible(Instance $instance, User $user): bool
125+
public function isAccessible(Instance $instance, ?User $user): bool
120126
{
121127
if ($this->instances && !in_array($instance->getId(), $this->instances)) {
122128
return false;
123129
}
124130

131+
if (!$user) {
132+
// test accessibility for all users (no user filters must be present)
133+
return !$this->userRoles && !$this->userExternalLogins;
134+
}
135+
125136
if ($this->userRoles && !in_array($user->getRole(), $this->userRoles)) {
126137
return false;
127138
}

app/helpers/Extensions/Extensions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public function getExtension(string $id): ?ExtensionConfig
4040
/**
4141
* Filter out extensions that are accessible by given user in given instance.
4242
* @param Instance $instance
43-
* @param User $user
43+
* @param User|null $user (if null, only extensions available to all users are listed)
4444
* @return ExtensionConfig[] array indexed by extension IDs
4545
*/
46-
public function getAccessibleExtensions(Instance $instance, User $user): array
46+
public function getAccessibleExtensions(Instance $instance, ?User $user): array
4747
{
4848
$res = [];
4949
foreach ($this->extensions as $id => $extension) {

app/model/view/InstanceViewFactory.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace App\Model\View;
44

5-
use App\Helpers\Localizations;
65
use App\Model\Entity\Instance;
76
use App\Model\Entity\LocalizedGroup;
7+
use App\Model\Entity\User;
8+
use App\Helpers\Localizations;
9+
use App\Helpers\Extensions;
810

911
/**
1012
* Factory for instance views which somehow do not fit into json serialization
@@ -15,21 +17,30 @@ class InstanceViewFactory
1517
/** @var GroupViewFactory */
1618
private $groupViewFactory;
1719

18-
public function __construct(GroupViewFactory $groupViewFactory)
20+
/** @var Extensions */
21+
private $extensions;
22+
23+
public function __construct(GroupViewFactory $groupViewFactory, Extensions $extensions)
1924
{
2025
$this->groupViewFactory = $groupViewFactory;
26+
$this->extensions = $extensions;
2127
}
2228

2329

2430
/**
2531
* Get as much instance detail info as your permissions grants you.
2632
* @param Instance $instance
33+
* @param User|null $loggedUser (to better target available extensions)
2734
* @return array
2835
*/
29-
public function getInstance(Instance $instance): array
36+
public function getInstance(Instance $instance, ?User $loggedUser = null): array
3037
{
3138
/** @var LocalizedGroup|null $localizedRootGroup */
3239
$localizedRootGroup = Localizations::getPrimaryLocalization($instance->getRootGroup()->getLocalizedTexts());
40+
$extensions = [];
41+
foreach ($this->extensions->getAccessibleExtensions($instance, $loggedUser) as $ext) {
42+
$extensions[$ext->getId()] = $ext->getCaption();
43+
}
3344

3445
return [
3546
"id" => $instance->getId(),
@@ -43,21 +54,23 @@ public function getInstance(Instance $instance): array
4354
"deletedAt" => $instance->getDeletedAt() ? $instance->getDeletedAt()->getTimestamp() : null,
4455
"adminId" => $instance->getAdmin() ? $instance->getAdmin()->getId() : null,
4556
"rootGroup" => $this->groupViewFactory->getGroup($instance->getRootGroup()),
46-
"rootGroupId" => $instance->getRootGroup()->getId()
57+
"rootGroupId" => $instance->getRootGroup()->getId(),
58+
"extensions" => $extensions,
4759
];
4860
}
4961

5062
/**
5163
* Get instance data.
5264
* @param Instance[] $instances
65+
* @param User|null $loggedUser (to better target available extensions)
5366
* @return array
5467
*/
55-
public function getInstances(array $instances): array
68+
public function getInstances(array $instances, ?User $loggedUser = null): array
5669
{
5770
$instances = array_values($instances);
5871
return array_map(
59-
function (Instance $instance) {
60-
return $this->getInstance($instance);
72+
function (Instance $instance) use ($loggedUser) {
73+
return $this->getInstance($instance, $loggedUser);
6174
},
6275
$instances
6376
);

0 commit comments

Comments
 (0)