Skip to content

Commit

Permalink
Merge pull request #6646 from getkirby/v5/fix/language-buttons
Browse files Browse the repository at this point in the history
Language view buttons (incl. permission check)
  • Loading branch information
distantnative authored Sep 12, 2024
2 parents 0038364 + cf04e9c commit 8407d88
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 51 deletions.
36 changes: 11 additions & 25 deletions config/areas/languages/buttons.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
<?php

use Kirby\Cms\Language;
use Kirby\Panel\Ui\Buttons\LanguageCreateButton;
use Kirby\Panel\Ui\Buttons\LanguageDeleteButton;
use Kirby\Panel\Ui\Buttons\LanguageSettingsButton;
use Kirby\Panel\Ui\Buttons\PreviewButton;
use Kirby\Panel\Ui\Buttons\ViewButton;
use Kirby\Toolkit\I18n;

return [
'languages.add' => function () {
return new ViewButton(
dialog: 'languages/create',
icon: 'add',
text: I18n::translate('language.create'),
);
},
'language.preview' => function (Language $language) {
return new PreviewButton(link: $language->url());
},
'language.settings' => function (Language $language) {
return new ViewButton(
dialog: 'languages/' . $language->id() . '/update',
icon: 'cog',
title: I18n::translate('settings'),
);
},
'language.remove' => function (Language $language) {
'languages.create' => fn () =>
new LanguageCreateButton(),
'language.preview' => fn (Language $language) =>
new PreviewButton(link: $language->url()),
'language.settings' => fn (Language $language) =>
new LanguageSettingsButton($language),
'language.delete' => function (Language $language) {
if ($language->isDeletable() === true) {
return new ViewButton(
dialog: 'languages/' . $language->id() . '/delete',
icon: 'trash',
title: I18n::translate('delete'),
);
return new LanguageDeleteButton($language);
}
}
];
8 changes: 4 additions & 4 deletions config/areas/languages/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
$foundation = $kirby->defaultLanguage()->translations();
$translations = $language->translations();

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

ksort($foundation);
Expand Down Expand Up @@ -76,7 +76,7 @@
'props' => [
'buttons' => fn () =>
ViewButtons::view('language')
->defaults('preview', 'settings', 'remove')
->defaults('preview', 'settings', 'delete')
->bind(['language' => $language])
->render(),
'deletable' => $language->isDeletable(),
Expand Down Expand Up @@ -121,7 +121,7 @@
'props' => [
'buttons' => fn () =>
ViewButtons::view('languages')
->defaults('add')
->defaults('create')
->render(),
'languages' => $kirby->languages()->values(fn ($language) => [
'deletable' => $language->isDeletable(),
Expand Down
4 changes: 2 additions & 2 deletions config/areas/site/buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Panel\Ui\Buttons\LanguagesButton;
use Kirby\Panel\Ui\Buttons\LanguagesDropdown;
use Kirby\Panel\Ui\Buttons\PageStatusButton;
use Kirby\Panel\Ui\Buttons\PreviewButton;
use Kirby\Panel\Ui\Buttons\SettingsButton;
Expand All @@ -25,7 +25,7 @@
// `languages` button needs to be in site area, as languages area itself
// is only loaded when in multilang setup
'languages' => function () {
return new LanguagesButton();
return new LanguagesDropdown();
},

// file buttons
Expand Down
2 changes: 1 addition & 1 deletion config/areas/users/buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Kirby\Toolkit\I18n;

return [
'users.add' => function (User $user, string|null $role = null) {
'users.create' => function (User $user, string|null $role = null) {
return new ViewButton(
dialog: 'users/create?role=' . $role,
disabled: $user->role()->permissions()->for('users', 'create') !== true,
Expand Down
2 changes: 1 addition & 1 deletion config/areas/users/views.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'props' => [
'buttons' => fn () =>
ViewButtons::view('users')
->defaults('add')
->defaults('create')
->bind(['role' => $role])
->render(),
'role' => function () use ($roles, $role) {
Expand Down
7 changes: 2 additions & 5 deletions panel/src/components/View/Buttons/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Languages from "./LanguagesButton.vue";
import LanguagesDropdown from "./LanguagesDropdown.vue";
import Settings from "./SettingsButton.vue";
import Status from "./StatusButton.vue";
import Theme from "./ThemeButton.vue";
Expand All @@ -8,15 +8,12 @@ import Buttons from "./Buttons.vue";

export default {
install(app) {
app.component("k-languages-view-button", Languages);
app.component("k-languages-dropdown", LanguagesDropdown);
app.component("k-settings-view-button", Settings);
app.component("k-status-view-button", Status);
app.component("k-theme-view-button", Theme);

app.component("k-view-button", Button);
app.component("k-view-buttons", Buttons);

// @deprecated
app.component("k-languages-dropdown", Languages);
}
};
33 changes: 33 additions & 0 deletions src/Panel/Ui/Buttons/LanguageCreateButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Toolkit\I18n;

/**
* View button to create a new language
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class LanguageCreateButton extends ViewButton
{
public function __construct()
{
$user = App::instance()->user();
$permission = $user?->role()->permissions()->for('languages', 'create');

parent::__construct(
dialog: 'languages/create',
disabled: $permission !== true,
icon: 'add',
text: I18n::translate('language.create'),
);
}
}
34 changes: 34 additions & 0 deletions src/Panel/Ui/Buttons/LanguageDeleteButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Toolkit\I18n;

/**
* View button to delete a language
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class LanguageDeleteButton extends ViewButton
{
public function __construct(Language $language)
{
$user = App::instance()->user();
$permission = $user?->role()->permissions()->for('languages', 'delete');

parent::__construct(
dialog: 'languages/' . $language->id() . '/delete',
disabled: $permission !== true,
icon: 'trash',
title: I18n::translate('delete'),
);
}
}
34 changes: 34 additions & 0 deletions src/Panel/Ui/Buttons/LanguageSettingsButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\Cms\Language;
use Kirby\Toolkit\I18n;

/**
* View button to update settings of a language
*
* @package Kirby Panel
* @author Nico Hoffmann <[email protected]>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
* @since 5.0.0
* @internal
*/
class LanguageSettingsButton extends ViewButton
{
public function __construct(Language $language)
{
$user = App::instance()->user();
$permission = $user?->role()->permissions()->for('languages', 'update');

parent::__construct(
dialog: 'languages/' . $language->id() . '/update',
disabled: $permission !== true,
icon: 'cog',
title: I18n::translate('settings'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @since 5.0.0
* @internal
*/
class LanguagesButton extends ViewButton
class LanguagesDropdown extends ViewButton
{
protected App $kirby;

Expand All @@ -26,8 +26,8 @@ public function __construct(
$this->kirby = App::instance();

parent::__construct(
component: 'k-languages-view-button',
class: 'k-languages-view-button',
component: 'k-languages-dropdown',
class: 'k-languages-dropdown',
icon: 'translate',
options: $this->options(),
responsive: 'text',
Expand Down
70 changes: 70 additions & 0 deletions tests/Panel/Ui/Buttons/LanguageCreateButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Kirby\Panel\Ui\Buttons;

use Kirby\Cms\App;
use Kirby\TestCase;

/**
* @coversDefaultClass \Kirby\Panel\Ui\Buttons\LanguageCreateButton
*/
class LanguageCreateButtonTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testButton()
{
$button = new LanguageCreateButton();

$this->assertSame('languages/create', $button->dialog);
$this->assertSame('Add a new language', $button->text);
}

/**
* @covers ::__construct
*/
public function testDisabled()
{
$app = new App([
'blueprints' => [
'users/editor' => [
'name' => 'editor',
'permissions' => [
'languages' => [
'create' => true
]
]
],
'users/user' => [
'name' => 'user',
'permissions' => [
'languages' => [
'create' => false
]
]
],
],
'users' => [
['email' => '[email protected]', 'role' => 'editor'],
['email' => '[email protected]', 'role' => 'user']
]
]);

// not authenticated
$button = new LanguageCreateButton();
$this->assertTrue($button->disabled);

// with permission
$app->impersonate('[email protected]', function () {
$button = new LanguageCreateButton();
$this->assertFalse($button->disabled);
});

// without permission
$app->impersonate('[email protected]', function () {
$button = new LanguageCreateButton();
$this->assertTrue($button->disabled);
});
}
}
Loading

0 comments on commit 8407d88

Please sign in to comment.