Skip to content

Commit aeb1d9d

Browse files
authored
[Perspectives] Add permissions on install (#934)
* add studio perspective permissions * Apply php-cs-fixer changes
1 parent be414a6 commit aeb1d9d

9 files changed

+61
-7
lines changed

src/Installer.php

+50
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Pimcore\Bundle\StudioBackendBundle\Entity\Grid\GridConfigurationShare;
2828
use Pimcore\Bundle\StudioBackendBundle\Entity\Perspective\UserPerspectiveData;
2929
use Pimcore\Bundle\StudioBackendBundle\Translation\Service\TranslatorService;
30+
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
3031
use Pimcore\Extension\Bundle\Installer\Exception\InstallationException;
3132
use Pimcore\Extension\Bundle\Installer\SettingsStoreAwareInstaller;
3233
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
@@ -36,6 +37,11 @@
3637
*/
3738
final class Installer extends SettingsStoreAwareInstaller
3839
{
40+
private const array PERSPECTIVE_PERMISSIONS = [
41+
UserPermissions::PERSPECTIVE_EDITOR->value,
42+
UserPermissions::WIDGET_EDITOR->value,
43+
];
44+
3945
public function __construct(
4046
private readonly Connection $db,
4147
BundleInterface $bundle,
@@ -55,6 +61,7 @@ public function install(): void
5561
$this->createGridConfigurationSharesTable($schema);
5662
$this->createGridConfigurationFavoritesTable($schema);
5763
$this->createUserPerspectivesTable($schema);
64+
$this->addUserPermission($schema);
5865
$this->executeDiffSql($schema);
5966

6067
parent::install();
@@ -83,6 +90,7 @@ public function uninstall(): void
8390
if ($schema->hasTable(UserPerspectiveData::TABLE_NAME)) {
8491
$schema->dropTable(UserPerspectiveData::TABLE_NAME);
8592
}
93+
$this->removeUserPermission($schema);
8694

8795
$this->executeDiffSql($schema);
8896

@@ -334,6 +342,48 @@ public function createUserPerspectivesTable(Schema $schema): void
334342
$table->setPrimaryKey(['user'], 'pk_' . UserPerspectiveData::TABLE_NAME);
335343
}
336344

345+
/**
346+
* @throws Exception
347+
*/
348+
private function addUserPermission(Schema $schema): void
349+
{
350+
if ($schema->hasTable(UserPermissions::DEFINITIONS_TABLE->value)) {
351+
foreach (self::PERSPECTIVE_PERMISSIONS as $permission) {
352+
$queryBuilder = $this->db->createQueryBuilder();
353+
$queryBuilder
354+
->insert(UserPermissions::DEFINITIONS_TABLE->value)
355+
->values([
356+
$this->db->quoteIdentifier('key') => ':key',
357+
$this->db->quoteIdentifier('category') => ':category',
358+
])
359+
->setParameters([
360+
'key' => $permission,
361+
'category' => UserPermissions::PERMISSIONS_CATEGORY->value,
362+
]);
363+
364+
$queryBuilder->executeStatement();
365+
}
366+
}
367+
}
368+
369+
/**
370+
* @throws Exception
371+
*/
372+
private function removeUserPermission(Schema $schema): void
373+
{
374+
if ($schema->hasTable(UserPermissions::DEFINITIONS_TABLE->value)) {
375+
foreach (self::PERSPECTIVE_PERMISSIONS as $permission) {
376+
$queryBuilder = $this->db->createQueryBuilder();
377+
$queryBuilder
378+
->delete(UserPermissions::DEFINITIONS_TABLE->value)
379+
->where($this->db->quoteIdentifier('key') . ' = :key')
380+
->setParameter('key', $permission);
381+
382+
$queryBuilder->executeStatement();
383+
}
384+
}
385+
}
386+
337387
/**
338388
* @throws Exception
339389
*/

src/Perspective/Controller/Widget/AddController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function __construct(
5959
name: 'pimcore_studio_api_create_perspectives_widgets_config',
6060
methods: ['POST']
6161
)]
62-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
62+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
6363
#[Post(
6464
path: self::PREFIX . self::ROUTE,
6565
operationId: 'perspective_widget_create',

src/Perspective/Controller/Widget/CollectionConfigurationController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
* @throws InvalidArgumentException|NotFoundException
5757
*/
5858
#[Route(self::ROUTE, name: 'pimcore_studio_api_get_perspectives_widgets_configurations_list', methods: ['GET'])]
59-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
59+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
6060
#[Get(
6161
path: self::PREFIX . self::ROUTE,
6262
operationId: 'perspective_widget_get_config_collection',

src/Perspective/Controller/Widget/DeleteController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
* @throws InvalidArgumentException|NotFoundException|NotWriteableException
5252
*/
5353
#[Route(self::ROUTE, name: 'pimcore_studio_api_delete_perspectives_widgets_config', methods: ['DELETE'])]
54-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
54+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
5555
#[Delete(
5656
path: self::PREFIX . self::ROUTE,
5757
operationId: 'perspective_widget_delete',

src/Perspective/Controller/Widget/GetConfigurationController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function __construct(
5858
requirements: ['id' => '\d+'],
5959
methods: ['GET']
6060
)]
61-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
61+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
6262
#[Get(
6363
path: self::PREFIX . self::ROUTE,
6464
operationId: 'perspective_widget_get_config_by_id',

src/Perspective/Controller/Widget/TypeController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545
}
4646

4747
#[Route(self::ROUTE, name: 'pimcore_studio_api_get_perspectives_widgets_types', methods: ['GET'])]
48-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
48+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
4949
#[Get(
5050
path: self::PREFIX . self::ROUTE,
5151
operationId: 'perspective_widget_get_type_collection',

src/Perspective/Controller/Widget/UpdateController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __construct(
6464
name: 'pimcore_studio_api_update_perspectives_widgets_config',
6565
methods: ['PUT']
6666
)]
67-
#[IsGranted(UserPermissions::WIDGET_EDIT->value)]
67+
#[IsGranted(UserPermissions::WIDGET_EDITOR->value)]
6868
#[Put(
6969
path: self::PREFIX . self::ROUTE,
7070
operationId: 'perspective_widget_update_config_by_id',

src/Util/Constant/UserPermissions.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
enum UserPermissions: string
2323
{
24+
case PERMISSIONS_CATEGORY = 'Pimcore Studio Backend Bundle';
25+
case DEFINITIONS_TABLE = 'users_permission_definitions';
2426
case ASSETS = 'assets';
2527
case DATA_OBJECTS = 'objects';
2628
case DOCUMENTS = 'documents';
@@ -40,5 +42,5 @@ enum UserPermissions: string
4042
case TAGS_SEARCH = 'tags_search';
4143
case THUMBNAILS = 'thumbnails';
4244
case USER_MANAGEMENT = 'users';
43-
case WIDGET_EDIT = 'perspective_editor_view_edit';
45+
case WIDGET_EDITOR = 'perspective_widget_editor';
4446
}

translations/admin.en.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
perspective_editor: Perspective Editor
2+
perspective_widget_editor: Widget Editor

0 commit comments

Comments
 (0)