File size: 11,523 Bytes
4d2a9bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | diff --git a/i18n/translations/en.json b/i18n/translations/en.json
index 1b54fa899d..1500d5907a 100644
--- a/i18n/translations/en.json
+++ b/i18n/translations/en.json
@@ -100,9 +100,12 @@
"error.form.notSaved": "The form could not be saved",
"error.language.code": "Please enter a valid code for the language",
+ "error.language.create.permission": "You are not allowed to create a language",
+ "error.language.delete.permission": "You are not allowed to delete the language",
"error.language.duplicate": "The language already exists",
"error.language.name": "Please enter a valid name for the language",
"error.language.notFound": "The language could not be found",
+ "error.language.update.permission": "You are not allowed to update the language",
"error.layout.validation.block": "There's an error in block {blockIndex} in layout {layoutIndex}",
"error.layout.validation.settings": "There's an error in layout {index} settings",
diff --git a/panel/src/components/Views/LanguagesView.vue b/panel/src/components/Views/LanguagesView.vue
index 9ec4605e9d..4ac4007c05 100644
--- a/panel/src/components/Views/LanguagesView.vue
+++ b/panel/src/components/Views/LanguagesView.vue
@@ -7,6 +7,7 @@
<k-button-group slot="left">
<k-button
:text="$t('language.create')"
+ :disabled="!$permissions.languages.create"
icon="add"
@click="$dialog('languages/create')"
/>
@@ -30,14 +31,23 @@
v-if="secondaryLanguages.length"
:items="secondaryLanguages"
/>
- <k-empty v-else icon="globe" @click="$dialog('languages/create')">
+ <k-empty
+ v-else
+ icon="globe"
+ :disabled="!$permissions.languages.create"
+ @click="$dialog('languages/create')"
+ >
{{ $t("languages.secondary.empty") }}
</k-empty>
</section>
</template>
<template v-else-if="languages.length === 0">
- <k-empty icon="globe" @click="$dialog('languages/create')">
+ <k-empty
+ icon="globe"
+ :disabled="!$permissions.languages.create"
+ @click="$dialog('languages/create')"
+ >
{{ $t("languages.empty") }}
</k-empty>
</template>
@@ -66,12 +76,17 @@ export default {
icon: "globe"
},
link: () => {
+ if (!this.$permissions.languages.update) {
+ return null;
+ }
+
this.$dialog(`languages/${language.id}/update`);
},
options: [
{
icon: "edit",
text: this.$t("edit"),
+ disabled: !this.$permissions.languages.update,
click() {
this.$dialog(`languages/${language.id}/update`);
}
@@ -79,7 +94,10 @@ export default {
{
icon: "trash",
text: this.$t("delete"),
- disabled: language.default && this.languages.length !== 1,
+ disabled: (
+ (language.default && this.languages.length !== 1) ||
+ !this.$permissions.languages.delete
+ ),
click() {
this.$dialog(`languages/${language.id}/delete`);
}
diff --git a/src/Cms/Language.php b/src/Cms/Language.php
index d15eb7c5ed..50adfa4bc8 100644
--- a/src/Cms/Language.php
+++ b/src/Cms/Language.php
@@ -201,8 +201,17 @@ protected static function converter(string $from, string $to): bool
*/
public static function create(array $props)
{
+ $kirby = App::instance();
+ $user = $kirby->user();
+
+ if (
+ $user === null ||
+ $user->role()->permissions()->for('languages', 'create') === false
+ ) {
+ throw new PermissionException(['key' => 'language.create.permission']);
+ }
+
$props['code'] = Str::slug($props['code'] ?? null);
- $kirby = App::instance();
$languages = $kirby->languages();
// make the first language the default language
@@ -238,10 +247,18 @@ public static function create(array $props)
public function delete(): bool
{
$kirby = App::instance();
+ $user = $kirby->user();
$languages = $kirby->languages();
$code = $this->code();
$isLast = $languages->count() === 1;
+ if (
+ $user === null ||
+ $user->role()->permissions()->for('languages', 'delete') === false
+ ) {
+ throw new PermissionException(['key' => 'language.delete.permission']);
+ }
+
if (F::remove($this->root()) !== true) {
throw new Exception('The language could not be deleted');
}
@@ -648,6 +665,16 @@ public function url(): string
*/
public function update(array $props = null)
{
+ $kirby = App::instance();
+ $user = $kirby->user();
+
+ if (
+ $user === null ||
+ $user->role()->permissions()->for('languages', 'update') === false
+ ) {
+ throw new PermissionException(['key' => 'language.update.permission']);
+ }
+
// don't change the language code
unset($props['code']);
diff --git a/src/Cms/Permissions.php b/src/Cms/Permissions.php
index 4a0137965b..b27fc74a0e 100644
--- a/src/Cms/Permissions.php
+++ b/src/Cms/Permissions.php
@@ -44,7 +44,8 @@ class Permissions
],
'languages' => [
'create' => true,
- 'delete' => true
+ 'delete' => true,
+ 'update' => true
],
'pages' => [
'changeSlug' => true,
diff --git a/tests/Cms/Languages/LanguageTest.php b/tests/Cms/Languages/LanguageTest.php
index f867243542..648aa02b6e 100644
--- a/tests/Cms/Languages/LanguageTest.php
+++ b/tests/Cms/Languages/LanguageTest.php
@@ -3,6 +3,7 @@
namespace Kirby\Cms;
use Kirby\Data\Data;
+use Kirby\Exception\PermissionException;
use Kirby\Filesystem\Dir;
use Kirby\Filesystem\F;
use PHPUnit\Framework\TestCase;
@@ -471,6 +472,8 @@ public function testBaseUrl($kirbyUrl, $url, $expected)
public function testCreate()
{
+ $this->app->impersonate('kirby');
+
$language = Language::create([
'code' => 'en'
]);
@@ -482,8 +485,53 @@ public function testCreate()
$this->assertSame('/en', $language->url());
}
+ /**
+ * @covers ::create
+ */
+ public function testCreateNoPermissions()
+ {
+ $app = $this->app->clone([
+ 'blueprints' => [
+ 'users/editor' => [
+ 'name' => 'editor',
+ 'permissions' => [
+ 'languages' => [
+ 'create' => false
+ ]
+ ]
+ ],
+ ],
+ 'users' => [
+ ['email' => 'test@getkirby.com', 'role' => 'editor']
+ ]
+ ]);
+
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to create a language');
+
+ $app->impersonate('test@getkirby.com');
+ Language::create([
+ 'code' => 'en'
+ ]);
+ }
+
+ /**
+ * @covers ::create
+ */
+ public function testCreateWithoutLoggedUser()
+ {
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to create a language');
+
+ Language::create([
+ 'code' => 'en'
+ ]);
+ }
+
public function testDelete()
{
+ $this->app->impersonate('kirby');
+
$language = Language::create([
'code' => 'en'
]);
@@ -491,10 +539,58 @@ public function testDelete()
$this->assertTrue($language->delete());
}
+ /**
+ * @covers ::delete
+ */
+ public function testDeleteNoPermissions()
+ {
+ $app = $this->app->clone([
+ 'blueprints' => [
+ 'users/editor' => [
+ 'name' => 'editor',
+ 'permissions' => [
+ 'languages' => [
+ 'create' => true,
+ 'delete' => false
+ ]
+ ]
+ ],
+ ],
+ 'users' => [
+ ['email' => 'test@getkirby.com', 'role' => 'editor']
+ ]
+ ]);
+
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to delete the language');
+
+ $app->impersonate('test@getkirby.com');
+ $language = Language::create(['code' => 'en']);
+ $language->delete();
+ }
+
+ /**
+ * @covers ::delete
+ */
+ public function testDeleteWithoutLoggedUser()
+ {
+ $this->app->impersonate('kirby');
+ $language = Language::create(['code' => 'en']);
+
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to delete the language');
+
+ // unimpersonate and test the method
+ $this->app->impersonate();
+ $language->delete();
+ }
+
public function testUpdate()
{
Dir::make($contentDir = $this->fixtures . '/content');
+ $this->app->impersonate('kirby');
+
$language = Language::create([
'code' => 'en'
]);
@@ -503,4 +599,51 @@ public function testUpdate()
$this->assertSame('English', $language->name());
}
+
+ /**
+ * @covers ::update
+ */
+ public function testUpdateNoPermissions()
+ {
+ $app = $this->app->clone([
+ 'blueprints' => [
+ 'users/editor' => [
+ 'name' => 'editor',
+ 'permissions' => [
+ 'languages' => [
+ 'create' => true,
+ 'update' => false
+ ]
+ ]
+ ],
+ ],
+ 'users' => [
+ ['email' => 'test@getkirby.com', 'role' => 'editor']
+ ]
+ ]);
+
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to update the language');
+
+ $app->impersonate('test@getkirby.com');
+
+ $language = Language::create(['code' => 'en']);
+ $language->update(['name' => 'English']);
+ }
+
+ /**
+ * @covers ::update
+ */
+ public function testUpdateWithoutLoggedUser()
+ {
+ $this->app->impersonate('kirby');
+ $language = Language::create(['code' => 'en']);
+
+ $this->expectException(PermissionException::class);
+ $this->expectExceptionMessage('You are not allowed to update the language');
+
+ // unimpersonate and test the method
+ $this->app->impersonate();
+ $language->update(['name' => 'English']);
+ }
}
diff --git a/tests/Cms/Languages/LanguagesTest.php b/tests/Cms/Languages/LanguagesTest.php
index e7a5dc06ff..df1cb8fd3a 100644
--- a/tests/Cms/Languages/LanguagesTest.php
+++ b/tests/Cms/Languages/LanguagesTest.php
@@ -108,6 +108,8 @@ public function testMultipleDefault()
public function testCreate()
{
+ $this->app->impersonate('kirby');
+
$language = $this->app->languages()->create([
'code' => 'tr'
]);
|