-
-
Notifications
You must be signed in to change notification settings - Fork 171
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 #6876 from getkirby/v5/feature/access-permissions-2
Access permissions 2: New `LanguagePermissions` class
- Loading branch information
Showing
7 changed files
with
197 additions
and
22 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
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,22 @@ | ||
<?php | ||
|
||
namespace Kirby\Cms; | ||
|
||
/** | ||
* LanguagePermissions | ||
* | ||
* @package Kirby Cms | ||
* @author Ahmet Bora <[email protected]> | ||
* @link https://getkirby.com | ||
* @copyright Bastian Allgeier | ||
* @license https://getkirby.com/license | ||
*/ | ||
class LanguagePermissions extends ModelPermissions | ||
{ | ||
protected string $category = 'languages'; | ||
|
||
protected function canDelete(): bool | ||
{ | ||
return $this->model->isDeletable() === true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
<?php | ||
|
||
namespace Kirby\Cms; | ||
|
||
use Kirby\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Kirby\Cms\LanguagePermissions | ||
*/ | ||
class LanguagePermissionsTest extends TestCase | ||
{ | ||
public static function actionProvider(): array | ||
{ | ||
return [ | ||
['create'], | ||
['delete'], | ||
['update'], | ||
]; | ||
} | ||
|
||
/** | ||
* @covers \Kirby\Cms\ModelPermissions::can | ||
* @dataProvider actionProvider | ||
*/ | ||
public function testWithAdmin($action) | ||
{ | ||
$kirby = new App([ | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'roles' => [ | ||
['name' => 'admin'], | ||
['name' => 'editor'] | ||
] | ||
]); | ||
|
||
$kirby->impersonate('kirby'); | ||
|
||
$language = new Language(['code' => 'en']); | ||
$perms = $language->permissions(); | ||
|
||
$this->assertTrue($perms->can($action)); | ||
} | ||
|
||
/** | ||
* @covers \Kirby\Cms\ModelPermissions::can | ||
* @dataProvider actionProvider | ||
*/ | ||
public function testWithNobody($action) | ||
{ | ||
new App([ | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'roles' => [ | ||
['name' => 'admin'], | ||
['name' => 'editor'] | ||
] | ||
]); | ||
|
||
$language = new Language(['code' => 'en']); | ||
$perms = $language->permissions(); | ||
|
||
$this->assertFalse($perms->can($action)); | ||
} | ||
|
||
/** | ||
* @covers \Kirby\Cms\ModelPermissions::can | ||
* @dataProvider actionProvider | ||
*/ | ||
public function testWithNoAdmin($action) | ||
{ | ||
$app = new App([ | ||
'languages' => [ | ||
[ | ||
'code' => 'en' | ||
] | ||
], | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'roles' => [ | ||
['name' => 'admin'], | ||
[ | ||
'name' => 'editor', | ||
'permissions' => [ | ||
'languages' => [ | ||
'create' => false, | ||
'delete' => false, | ||
'update' => false | ||
], | ||
] | ||
] | ||
], | ||
'user' => '[email protected]', | ||
'users' => [ | ||
[ | ||
'email' => '[email protected]', | ||
'role' => 'admin' | ||
], | ||
[ | ||
'email' => '[email protected]', | ||
'role' => 'editor' | ||
] | ||
], | ||
]); | ||
|
||
$language = $app->language('en'); | ||
$perms = $language->permissions(); | ||
|
||
$this->assertSame('editor', $app->role()->name()); | ||
$this->assertFalse($perms->can($action)); | ||
} | ||
|
||
/** | ||
* @covers ::canDelete | ||
*/ | ||
public function testCanDeleteWhenNotDeletable() | ||
{ | ||
$app = new App([ | ||
'languages' => [ | ||
[ | ||
'code' => 'en', | ||
'default' => true | ||
], | ||
[ | ||
'code' => 'de' | ||
] | ||
], | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'roles' => [ | ||
['name' => 'admin'] | ||
] | ||
]); | ||
|
||
$app->impersonate('kirby'); | ||
|
||
$language = $app->language('en'); | ||
$perms = $language->permissions(); | ||
|
||
$this->assertFalse($perms->can('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
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 |
---|---|---|
|
@@ -122,16 +122,24 @@ public function testWithNoAdmin($action) | |
|
||
public function testChangeSingleRole() | ||
{ | ||
new App([ | ||
$app = new App([ | ||
'roots' => [ | ||
'index' => '/dev/null' | ||
], | ||
'roles' => [ | ||
['name' => 'admin'] | ||
], | ||
'users' => [ | ||
[ | ||
'email' => '[email protected]', | ||
'role' => 'admin' | ||
] | ||
] | ||
]); | ||
|
||
$user = new User(['email' => '[email protected]']); | ||
$app->impersonate('kirby'); | ||
|
||
$user = $app->user('[email protected]'); | ||
$perms = $user->permissions(); | ||
|
||
$this->assertFalse($perms->can('changeRole')); | ||
|