Skip to content

Commit

Permalink
Merge branch 'develop-patch' into release/4.4.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	composer.lock
#	panel/dist/js/index.min.js
#	vendor/composer/installed.php
  • Loading branch information
distantnative committed Aug 29, 2024
2 parents 4d3213f + 576306d commit 3fc91cf
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 123 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions config/areas/languages/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
return App::instance()->option('languages.variables', true) !== false;
},
'action' => function (string $code) {
$kirby = App::instance();
$language = Find::language($code);
$link = '/languages/' . $language->code();
$strings = [];
$foundation = App::instance()->defaultLanguage()->translations();
$foundation = $kirby->defaultLanguage()->translations();
$translations = $language->translations();

// TODO: update following line and adapt for update and delete options
// when new `languageVariables.*` permissions available
$canUpdate = $kirby->user()?->role()->permissions()->for('languages', 'update') === true;

ksort($foundation);

foreach ($foundation as $key => $value) {
Expand All @@ -26,13 +31,14 @@
'value' => $translations[$key] ?? null,
'options' => [
[
'click' => 'update',
'icon' => 'edit',
'text' => I18n::translate('edit'),
'click' => 'update',
'disabled' => $canUpdate === false,
'icon' => 'edit',
'text' => I18n::translate('edit'),
],
[
'click' => 'delete',
'disabled' => $language->isDefault() === false,
'disabled' => $canUpdate === false || $language->isDefault() === false,
'icon' => 'trash',
'text' => I18n::translate('delete'),
]
Expand Down
3 changes: 3 additions & 0 deletions i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,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 on the \"{field}\" field in block {blockIndex} using the \"{fieldset}\" block type in layout {layoutIndex}",
"error.layout.validation.settings": "There's an error in layout {index} settings",
Expand Down
28 changes: 26 additions & 2 deletions panel/src/components/Views/Languages/LanguageView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<k-prev-next :prev="prev" :next="next" />
</template>

<k-header :editable="true" @edit="update()">
<k-header :editable="canUpdate" @edit="update()">
{{ name }}

<template #buttons>
Expand All @@ -18,6 +18,7 @@
variant="filled"
/>
<k-button
:disabled="!canUpdate"
:title="$t('settings')"
icon="cog"
size="sm"
Expand All @@ -26,6 +27,7 @@
/>
<k-button
v-if="deletable"
:disabled="!$panel.permissions.languages.delete"
:title="$t('delete')"
icon="trash"
size="sm"
Expand All @@ -42,8 +44,12 @@

<k-section
:buttons="[
/**
* @todo update disabled prop when new `languageVariables.*` permissions available
*/
{
click: createTranslation,
disabled: !canUpdate,
icon: 'add',
text: $t('add')
}
Expand All @@ -63,13 +69,14 @@
mobile: true
}
}"
:disabled="!canUpdate"
:rows="translations"
@cell="updateTranslation"
@option="option"
/>
</template>
<template v-else>
<k-empty icon="translate" @click="createTranslation">
<k-empty :disabled="!canUpdate" icon="translate" @click="createTranslation">
{{ $t("language.variables.empty") }}
</k-empty>
</template>
Expand All @@ -95,11 +102,24 @@ export default {
translations: Array,
url: String
},
computed: {
canUpdate() {
return this.$panel.permissions.languages.update;
}
},
methods: {
createTranslation() {
if (!this.canUpdate) {
return;
}
this.$dialog(`languages/${this.id}/translations/create`);
},
option(option, row) {
if (!this.canUpdate) {
return;
}
// for the compatibility of the encoded url in different environments,
// it is also encoded with base64 to reduce special characters
this.$dialog(
Expand All @@ -121,6 +141,10 @@ export default {
});
},
updateTranslation({ row }) {
if (!this.canUpdate) {
return;
}
// for the compatibility of the encoded url in different environments,
// it is also encoded with base64 to reduce special characters
this.$dialog(
Expand Down
18 changes: 15 additions & 3 deletions panel/src/components/Views/Languages/LanguagesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<k-button-group slot="buttons">
<k-button
:disabled="!$panel.permissions.languages.create"
:text="$t('language.create')"
icon="add"
size="sm"
Expand All @@ -24,14 +25,23 @@
v-if="secondaryLanguages.length"
:items="secondaryLanguages"
/>
<k-empty v-else icon="translate" @click="$dialog('languages/create')">
<k-empty
v-else
icon="translate"
:disabled="!$panel.permissions.languages.create"
@click="$dialog('languages/create')"
>
{{ $t("languages.secondary.empty") }}
</k-empty>
</k-section>
</template>

<template v-else-if="languages.length === 0">
<k-empty icon="translate" @click="$dialog('languages/create')">
<k-empty
icon="translate"
:disabled="!$panel.permissions.languages.create"
@click="$dialog('languages/create')"
>
{{ $t("languages.empty") }}
</k-empty>
</template>
Expand Down Expand Up @@ -79,12 +89,14 @@ export default {
{
icon: "cog",
text: this.$t("settings"),
disabled: !this.$panel.permissions.languages.update,
click: () => this.$dialog(`languages/${language.id}/update`)
},
{
when: language.deletable,
icon: "trash",
text: this.$t("delete"),
disabled: language.deletable === false,
disabled: !this.$panel.permissions.languages.delete,
click: () => this.$dialog(`languages/${language.id}/delete`)
}
]
Expand Down
31 changes: 29 additions & 2 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\LogicException;
use Kirby\Exception\PermissionException;
use Kirby\Filesystem\F;
use Kirby\Toolkit\Locale;
use Kirby\Toolkit\Str;
Expand Down Expand Up @@ -145,8 +146,17 @@ public function code(): string
*/
public static function create(array $props): static
{
$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
Expand Down Expand Up @@ -204,8 +214,16 @@ public static function create(array $props): static
public function delete(): bool
{
$kirby = App::instance();
$user = $kirby->user();
$code = $this->code();

if (
$user === null ||
$user->role()->permissions()->for('languages', 'delete') === false
) {
throw new PermissionException(['key' => 'language.delete.permission']);
}

if ($this->isDeletable() === false) {
throw new Exception('The language cannot be deleted');
}
Expand Down Expand Up @@ -497,13 +515,22 @@ public function url(): string
*/
public function update(array $props = null): static
{
$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']);

// make sure the slug is nice and clean
$props['slug'] = Str::slug($props['slug'] ?? null);

$kirby = App::instance();
$updated = $this->clone($props);

if (isset($props['translations']) === true) {
Expand Down
3 changes: 2 additions & 1 deletion src/Cms/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Permissions
],
'languages' => [
'create' => true,
'delete' => true
'delete' => true,
'update' => true
],
'pages' => [
'access' => true,
Expand Down
Loading

0 comments on commit 3fc91cf

Please sign in to comment.