|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Helpers; |
| 4 | + |
| 5 | +use App\Model\Entity\Instance; |
| 6 | +use App\Model\Entity\User; |
| 7 | +use App\Exceptions\ConfigException; |
| 8 | +use Nette; |
| 9 | +use Nette\Utils\Arrays; |
| 10 | + |
| 11 | +/** |
| 12 | + * This holds a configuration and help handle tokens for a single extension. |
| 13 | + */ |
| 14 | +class ExtensionConfig |
| 15 | +{ |
| 16 | + use Nette\SmartObject; |
| 17 | + |
| 18 | + /** |
| 19 | + * Internal identifier. |
| 20 | + */ |
| 21 | + private string $id; |
| 22 | + |
| 23 | + /** |
| 24 | + * Caption as a string or localized strings (array locale => caption). |
| 25 | + * @var string|string[] |
| 26 | + */ |
| 27 | + private string|array $caption; |
| 28 | + |
| 29 | + /** |
| 30 | + * URL template for the external service. The template may hold the following placeholders: |
| 31 | + * - {token} - will be replaced with URL-encoded temporary token |
| 32 | + * - {locale} - will be replaced with a language identifier ('en', 'cs', ...) based on currently selected language |
| 33 | + */ |
| 34 | + private string $url; |
| 35 | + |
| 36 | + /** |
| 37 | + * A scope that will be set to (full) access tokens generated after tmp-token verification. |
| 38 | + */ |
| 39 | + private string $tokenScope; |
| 40 | + |
| 41 | + /** |
| 42 | + * User override for (full) access tokens. This user will be used instead of user ID passed in tmp token. |
| 43 | + * This is a way how to safely provide more powerful full tokens (without compromising tmp tokens). |
| 44 | + * If null, the (logged in) user from tmp token is passed to the full token. |
| 45 | + */ |
| 46 | + private string|null $tokenUserId = null; |
| 47 | + |
| 48 | + /** |
| 49 | + * List of instances in which the extension should appear. |
| 50 | + * Empty list = all instances. |
| 51 | + * @var string[] |
| 52 | + */ |
| 53 | + private array $instances = []; |
| 54 | + |
| 55 | + /** |
| 56 | + * List of user roles for which this extensions should appear. |
| 57 | + * Empty list = all roles. |
| 58 | + * @var string[] |
| 59 | + */ |
| 60 | + private array $userRoles = []; |
| 61 | + |
| 62 | + /** |
| 63 | + * List of eligible user external login types. A user must hava at least one of these logins to see the extension. |
| 64 | + * Empty list = no external logins are required. |
| 65 | + */ |
| 66 | + private array $userExternalLogins = []; |
| 67 | + |
| 68 | + public function __construct(array $config) |
| 69 | + { |
| 70 | + $this->id = (string)Arrays::get($config, "id"); |
| 71 | + |
| 72 | + $this->caption = Arrays::get($config, "caption"); |
| 73 | + if (is_array($this->caption)) { |
| 74 | + foreach ($this->caption as $locale => $caption) { |
| 75 | + if (!is_string($locale) || !is_string($caption)) { |
| 76 | + throw new ConfigException("Invalid extension caption format."); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + $this->url = Arrays::get($config, "url"); |
| 82 | + $this->tokenScope = Arrays::get($config, ["token", "scope"], "master"); |
| 83 | + $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"], []); |
| 87 | + } |
| 88 | + |
| 89 | + public function getId(): string |
| 90 | + { |
| 91 | + return $this->id; |
| 92 | + } |
| 93 | + |
| 94 | + public function getCaption(): string|array |
| 95 | + { |
| 96 | + return $this->caption; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get formatted URL. A template is injected a token and current locale. |
| 101 | + * @param string $token already serialized JWT |
| 102 | + * @param string $locale language identification ('en', 'cs', ...) |
| 103 | + * @return string an instantiated URL template |
| 104 | + */ |
| 105 | + public function getUrl(string $token, string $locale): string |
| 106 | + { |
| 107 | + $url = $this->url; |
| 108 | + $url = str_replace('{token}', urlencode($token), $url); |
| 109 | + $url = str_replace('{locale}', urlencode($locale), $url); |
| 110 | + return $url; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Check whether this extension is accessible by given user in given instance. |
| 115 | + * @param Instance $instance |
| 116 | + * @param User $user |
| 117 | + * @return bool true if the extension is accessible |
| 118 | + */ |
| 119 | + public function isAccessible(Instance $instance, User $user): bool |
| 120 | + { |
| 121 | + if ($this->instances && !in_array($instance->getId(), $this->instances)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + if ($this->userRoles && !in_array($user->getRole(), $this->userRoles)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if ($this->userExternalLogins) { |
| 130 | + $logins = $user->getConsolidatedExternalLogins(); |
| 131 | + foreach ($this->userExternalLogins as $service) { |
| 132 | + if (array_key_exists($service, $logins)) { |
| 133 | + return true; |
| 134 | + } |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + return true; |
| 140 | + } |
| 141 | +} |
0 commit comments