Skip to content

Commit

Permalink
[Home] fixes tabs edition (#1526)
Browse files Browse the repository at this point in the history
[Home] fixes tabs edition
  • Loading branch information
Elorfin authored Jan 10, 2021
1 parent 9e5b95d commit 72dc6ce
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/main/core/Controller/APINew/ObjectLockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
*/
class ObjectLockController
{
private $om;
private $serializer;
private $manager;

public function __construct(
ObjectManager $om,
SerializerProvider $serializer,
Expand All @@ -35,7 +39,7 @@ public function __construct(
/**
* @Route("/lock/class/{class}/id/{id}", name="apiv2_object_lock", methods={"PUT"})
*/
public function lockAction($class, $id)
public function lockAction($class, $id): JsonResponse
{
$this->manager->lock($class, $id);

Expand All @@ -45,7 +49,7 @@ public function lockAction($class, $id)
/**
* @Route("/unlock/class/{class}/id/{id}", name="apiv2_object_unlock", methods={"PUT"})
*/
public function unlockAction($class, $id)
public function unlockAction($class, $id): JsonResponse
{
$this->manager->unlock($class, $id);

Expand All @@ -55,7 +59,7 @@ public function unlockAction($class, $id)
/**
* @Route("/class/{class}/id/{id}", name="apiv2_object_lock_get", methods={"GET"})
*/
public function getAction($class, $id)
public function getAction($class, $id): JsonResponse
{
return new JsonResponse($this->serializer->serialize($this->manager->getLock($class, $id)));
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/core/Manager/LockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
use Claroline\CoreBundle\Entity\ObjectLock;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class LockManager
{
private $om;
private $tokenStorage;
private $authChecker;

public function __construct(ObjectManager $om, AuthorizationCheckerInterface $authChecker, TokenStorageInterface $tokenStorage)
{
$this->om = $om;
Expand All @@ -28,9 +33,11 @@ public function __construct(ObjectManager $om, AuthorizationCheckerInterface $au
public function lock($class, $uuid)
{
$this->check($class, $uuid);

$lock = $this->getLock($class, $uuid);
$lock->setLocked(true);
$lock->setUser($this->tokenStorage->getToken()->getUser());

$this->om->persist($lock);
$this->om->flush();
}
Expand All @@ -39,6 +46,7 @@ public function unlock($class, $uuid)
{
$lock = $this->getLock($class, $uuid);
$lock->setLocked(false);

$this->om->persist($lock);
$this->om->flush();
}
Expand Down Expand Up @@ -67,10 +75,12 @@ public function isLocked($class, $uuid)
public function create($class, $uuid)
{
$this->check($class, $uuid);

$lock = new ObjectLock();
$lock->setObjectUuid($uuid);
$lock->setObjectClass($class);
$lock->setUser($this->tokenStorage->getToken()->getUser());

$this->om->persist($lock);
$this->om->flush();

Expand All @@ -82,7 +92,7 @@ public function check($class, $uuid)
$object = $this->om->find($class, $uuid);

if (!$this->authChecker->isGranted('EDIT', $object)) {
throw new \Exception('You cannot (un)lock this resource');
throw new AccessDeniedException('You cannot (un)lock this resource');
}
}
}
33 changes: 31 additions & 2 deletions src/plugin/home/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Claroline\CoreBundle\Manager\LockManager;
use Claroline\HomeBundle\Entity\HomeTab;
use Claroline\HomeBundle\Serializer\HomeTabSerializer;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -109,7 +110,21 @@ public function updateAction(Request $request, string $context, string $contextI
foreach ($tabs as $tab) {
// do not update tabs set by the administration tool
if (HomeTab::TYPE_ADMIN_DESKTOP !== $tab['context']) {
$entity = $this->crud->update(HomeTab::class, $tab, [$context, Crud::THROW_EXCEPTION]);
$new = true;
if (isset($tab['id'])) {
foreach ($installedTabs as $installedTab) {
if ($installedTab->getUuid() === $tab['id']) {
$new = false;
break;
}
}
}

if ($new) {
$entity = $this->crud->create(HomeTab::class, $tab, [$context, Crud::THROW_EXCEPTION]);
} else {
$entity = $this->crud->update(HomeTab::class, $tab, [$context, Crud::THROW_EXCEPTION]);
}
} else {
$entity = $this->om->getObject($tab, HomeTab::class);
}
Expand Down Expand Up @@ -151,7 +166,21 @@ public function adminUpdateAction(Request $request, string $context): JsonRespon
$ids = [];
$updated = [];
foreach ($tabs as $tab) {
$entity = $this->crud->update(HomeTab::class, $tab, [$context]);
$new = true;
if (isset($tab['id'])) {
foreach ($installedTabs as $installedTab) {
if ($installedTab->getUuid() === $tab['id']) {
$new = false;
break;
}
}
}
if ($new) {
$entity = $this->crud->create(HomeTab::class, $tab, [$context, Crud::THROW_EXCEPTION]);
} else {
$entity = $this->crud->update(HomeTab::class, $tab, [$context, Crud::THROW_EXCEPTION]);
}

$updated[] = $entity;
$ids = array_merge($ids, [$entity->getUuid()], array_map(function (HomeTab $child) {
return $child->getUuid();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/home/Finder/HomeTabFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function configureQueryBuilder(
// only grab tabs accessible by user
$roleNames = $this->tokenStorage->getToken()->getRoleNames();

$isAdmin = in_array('ROLE_ADMIN', $roleNames) || (isset($searches['workspace']) && in_array('ROLE_MANAGER_'.$searches['workspace'], $roleNames));
$isAdmin = in_array('ROLE_ADMIN', $roleNames) || (isset($searches['workspace']) && in_array('ROLE_WS_MANAGER_'.$searches['workspace'], $roleNames));
if (!$isAdmin) {
// only get visible tabs for non admin
$qb->andWhere('config.visible = true');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class EditorTab extends Component {
name={selectors.FORM_NAME}
dataPart={getFormDataPart(this.props.currentTab.id, this.props.tabs)}
buttons={true}
lock={this.props.currentTab ? {
lock={this.props.currentTab && !get(this.props.currentTab, '_new', false) ? {
id: this.props.currentTab.id,
className: 'Claroline\\HomeBundle\\Entity\\HomeTab'
} : undefined}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ actions.startCreation = (context, type, administration, currentUser, position) =
context.type,
administration: administration,
user: context.type === 'desktop' && !administration ? currentUser : null,
workspace: context.type === 'workspace' ? {id: context.data.id} : null
workspace: context.type === 'workspace' ? {id: context.data.id} : null,
_new: true // this is used to avoid requesting an ObjectLock to the server as the tab not already exists
}), true))

// set the tab title
Expand Down

0 comments on commit 72dc6ce

Please # to comment.