Skip to content

Commit

Permalink
Permission check in LocationAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
manuxi committed Mar 27, 2021
1 parent 2572bb1 commit abd4fe1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 60 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# SuluEventBundle!
This bundle was made based on [Sulu workshop](https://github.com/sulu/sulu-workshop).
I made it to quickly install the possibility to manage events in my projects.
This bundle is still in development. Use at own risk 🤞🏻

<div align="center">
<div>
<a href="https://www.travis-ci.com/manuxi/SuluEventBundle" target="_blank">
<img src="https://www.travis-ci.com/manuxi/SuluEventBundle.svg?branch=main" alt="Travis status">
</a>
Expand All @@ -13,9 +9,15 @@ This bundle is still in development. Use at own risk 🤞🏻
<a href="https://github.com/manuxi/SuluEventBundle/tags" target="_blank">
<img src="https://img.shields.io/github/v/tag/manuxi/SuluEventBundle" alt="GitHub license">
</a>

</div>

This bundle was made based on [Sulu workshop](https://github.com/sulu/sulu-workshop).
I made it to quickly install the possibility to manage events in my projects.
This bundle is still in development. Use at own risk 🤞🏻



## 👩🏻‍🏭 Installation
Install the package with:
```console
Expand Down
128 changes: 74 additions & 54 deletions src/Admin/LocationAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Manuxi\SuluEventBundle\Admin;

use Manuxi\SuluEventBundle\Entity\Event;
use Manuxi\SuluEventBundle\Entity\Location;
use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
Expand All @@ -12,6 +13,8 @@
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\AdminBundle\Exception\NavigationItemNotFoundException;
use Sulu\Component\Security\Authorization\PermissionTypes;
use Sulu\Component\Security\Authorization\SecurityCheckerInterface;

class LocationAdmin extends Admin
{
Expand All @@ -25,71 +28,88 @@ class LocationAdmin extends Admin

private $viewBuilderFactory;

public function __construct(ViewBuilderFactoryInterface $viewBuilderFactory)
{
private $securityChecker;

public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
SecurityCheckerInterface $securityChecker
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->securityChecker = $securityChecker;
}

/**
* @throws NavigationItemNotFoundException
*/
public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void
{
$module = $navigationItemCollection->get('app.events');
$locations = new NavigationItem('app.locations');
$locations->setPosition(10);
$locations->setView(static::LOCATION_LIST_VIEW);

$module->addChild($locations);
if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$module = $navigationItemCollection->get('app.events');
$locations = new NavigationItem('app.locations');
$locations->setPosition(10);
$locations->setView(static::LOCATION_LIST_VIEW);

$module->addChild($locations);
}
}

public function configureViews(ViewCollection $viewCollection): void
{
$listToolbarActions = [
new ToolbarAction('sulu_admin.add'),
new ToolbarAction('sulu_admin.delete'),
];

$listView = $this->viewBuilderFactory->createListViewBuilder(self::LOCATION_LIST_VIEW, '/locations')
->setResourceKey(Location::RESOURCE_KEY)
->setListKey(self::LOCATION_LIST_KEY)
->setTitle('app.locations')
->addListAdapters(['table'])
->setAddView(static::LOCATION_ADD_FORM_VIEW)
->setEditView(static::LOCATION_EDIT_FORM_VIEW)
->addToolbarActions($listToolbarActions);
$viewCollection->add($listView);

$addFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(self::LOCATION_ADD_FORM_VIEW, '/locations/add')
->setResourceKey('locations')
->setBackView(static::LOCATION_LIST_VIEW);
$viewCollection->add($addFormView);

$addDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(self::LOCATION_ADD_FORM_VIEW . '.details', '/details')
->setResourceKey('locations')
->setFormKey('location_details')
->setTabTitle('sulu_admin.details')
->setEditView(static::LOCATION_EDIT_FORM_VIEW)
->addToolbarActions([new ToolbarAction('sulu_admin.save')])
->setParent(static::LOCATION_ADD_FORM_VIEW);
$viewCollection->add($addDetailsFormView);

$editFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(static::LOCATION_EDIT_FORM_VIEW, '/locations/:id')
->setResourceKey('locations')
->setBackView(static::LOCATION_LIST_VIEW)
->setTitleProperty('title');
$viewCollection->add($editFormView);

$formToolbarActions = [
new ToolbarAction('sulu_admin.save'),
new ToolbarAction('sulu_admin.delete'),
];
$editDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(static::LOCATION_EDIT_FORM_VIEW . '.details', '/details')
->setResourceKey('locations')
->setFormKey('location_details')
->setTabTitle('sulu_admin.details')
->addToolbarActions($formToolbarActions)
->setParent(static::LOCATION_EDIT_FORM_VIEW);
$viewCollection->add($editDetailsFormView);
$formToolbarActions = [];
$listToolbarActions = [];

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::ADD)) {
$listToolbarActions[] = new ToolbarAction('sulu_admin.add');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {
$formToolbarActions[] = new ToolbarAction('sulu_admin.save');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::DELETE)) {
$formToolbarActions[] = new ToolbarAction('sulu_admin.delete');
$listToolbarActions[] = new ToolbarAction('sulu_admin.delete');
}

if ($this->securityChecker->hasPermission(Event::SECURITY_CONTEXT, PermissionTypes::EDIT)) {

$listView = $this->viewBuilderFactory->createListViewBuilder(self::LOCATION_LIST_VIEW, '/locations')
->setResourceKey(Location::RESOURCE_KEY)
->setListKey(self::LOCATION_LIST_KEY)
->setTitle('app.locations')
->addListAdapters(['table'])
->setAddView(static::LOCATION_ADD_FORM_VIEW)
->setEditView(static::LOCATION_EDIT_FORM_VIEW)
->addToolbarActions($listToolbarActions);
$viewCollection->add($listView);

$addFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(self::LOCATION_ADD_FORM_VIEW, '/locations/add')
->setResourceKey('locations')
->setBackView(static::LOCATION_LIST_VIEW);
$viewCollection->add($addFormView);

$addDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(self::LOCATION_ADD_FORM_VIEW . '.details', '/details')
->setResourceKey('locations')
->setFormKey('location_details')
->setTabTitle('sulu_admin.details')
->setEditView(static::LOCATION_EDIT_FORM_VIEW)
->addToolbarActions([new ToolbarAction('sulu_admin.save')])
->setParent(static::LOCATION_ADD_FORM_VIEW);
$viewCollection->add($addDetailsFormView);

$editFormView = $this->viewBuilderFactory->createResourceTabViewBuilder(static::LOCATION_EDIT_FORM_VIEW, '/locations/:id')
->setResourceKey('locations')
->setBackView(static::LOCATION_LIST_VIEW)
->setTitleProperty('title');
$viewCollection->add($editFormView);

$editDetailsFormView = $this->viewBuilderFactory->createFormViewBuilder(static::LOCATION_EDIT_FORM_VIEW . '.details', '/details')
->setResourceKey('locations')
->setFormKey('location_details')
->setTabTitle('sulu_admin.details')
->addToolbarActions($formToolbarActions)
->setParent(static::LOCATION_EDIT_FORM_VIEW);
$viewCollection->add($editDetailsFormView);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<service id="sulu_event.admin.location" class="Manuxi\SuluEventBundle\Admin\LocationAdmin">
<argument type="service" id="sulu_admin.view_builder_factory"/>
<argument type="service" id="sulu_security.security_checker"/>

<tag name="sulu.admin"/>
<tag name="sulu.context" context="admin"/>
Expand Down

0 comments on commit abd4fe1

Please sign in to comment.