-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6646 from getkirby/v5/fix/language-buttons
Language view buttons (incl. permission check)
- Loading branch information
Showing
15 changed files
with
351 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
Oops, something went wrong.