-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transfer] adds action register/unregister groups from ws
- Loading branch information
Showing
11 changed files
with
268 additions
and
253 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/main/core/API/Transfer/Action/Workspace/AddGroup.php
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,104 @@ | ||
<?php | ||
|
||
namespace Claroline\CoreBundle\API\Transfer\Action\Workspace; | ||
|
||
use Claroline\AppBundle\API\Crud; | ||
use Claroline\AppBundle\API\Options; | ||
use Claroline\AppBundle\API\SerializerProvider; | ||
use Claroline\AppBundle\API\Transfer\Action\AbstractAction; | ||
use Claroline\AppBundle\Persistence\ObjectManager; | ||
use Claroline\CoreBundle\Entity\Group; | ||
use Claroline\CoreBundle\Entity\Role; | ||
use Claroline\CoreBundle\Entity\Workspace\Workspace; | ||
|
||
class AddGroup extends AbstractAction | ||
{ | ||
/** @var ObjectManager */ | ||
private $om; | ||
/** @var SerializerProvider */ | ||
private $serializer; | ||
/** @var Crud */ | ||
private $crud; | ||
|
||
public function __construct(Crud $crud, SerializerProvider $serializer, ObjectManager $om) | ||
{ | ||
$this->crud = $crud; | ||
$this->serializer = $serializer; | ||
$this->om = $om; | ||
} | ||
|
||
public function execute(array $data, &$successData = []) | ||
{ | ||
$group = $this->om->getObject($data['group'], Group::class, array_keys($data['group'])); | ||
|
||
if (!$group) { | ||
throw new \Exception('Group '.$this->printError($data['group'])." doesn't exists."); | ||
} | ||
|
||
//todo find a generic way to find the identifiers | ||
$workspace = $this->om->getObject($data['workspace'], Workspace::class, ['code']); | ||
|
||
if (!$workspace) { | ||
throw new \Exception('Workspace '.$this->printError($data['workspace'])." doesn't exists."); | ||
} | ||
|
||
$role = $this->om->getRepository(Role::class) | ||
->findOneBy(['workspace' => $workspace, 'translationKey' => $data['role']['translationKey']]); | ||
|
||
if (!$role) { | ||
throw new \Exception('Role '.$this->printError($data['role'])." doesn't exists."); | ||
} | ||
|
||
$this->crud->patch($group, 'role', 'add', [$role]); | ||
} | ||
|
||
public function printError(array $el) | ||
{ | ||
$string = ''; | ||
|
||
foreach ($el as $value) { | ||
$string .= ' '.$value; | ||
} | ||
|
||
return $string; | ||
} | ||
|
||
public function getSchema(array $options = [], array $extra = []) | ||
{ | ||
$roleSchema = [ | ||
'$schema' => 'http:\/\/json-schema.org\/draft-04\/schema#', | ||
'type' => 'object', | ||
'properties' => [ | ||
'translationKey' => [ | ||
'type' => 'string', | ||
'description' => 'The role name', | ||
], | ||
], | ||
'claroline' => [ | ||
'requiredAtCreation' => ['translationKey'], | ||
'ids' => ['translationKey'], | ||
'class' => Role::class, | ||
], | ||
]; | ||
|
||
$schema = json_decode(json_encode($roleSchema)); | ||
|
||
$schema = ['group' => Group::class, 'role' => $schema]; | ||
|
||
if (!in_array(Options::WORKSPACE_IMPORT, $options)) { | ||
$schema['workspace'] = Workspace::class; | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
public function getAction() | ||
{ | ||
return ['workspace', 'add_group']; | ||
} | ||
|
||
public function supports($format, array $options = [], array $extra = []) | ||
{ | ||
return in_array($format, ['json', 'csv']); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/core/API/Transfer/Action/Workspace/RemoveGroup.php
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,112 @@ | ||
<?php | ||
|
||
namespace Claroline\CoreBundle\API\Transfer\Action\Workspace; | ||
|
||
use Claroline\AppBundle\API\Crud; | ||
use Claroline\AppBundle\API\Options; | ||
use Claroline\AppBundle\API\SerializerProvider; | ||
use Claroline\AppBundle\API\Transfer\Action\AbstractAction; | ||
use Claroline\AppBundle\Persistence\ObjectManager; | ||
use Claroline\CoreBundle\Entity\Group; | ||
use Claroline\CoreBundle\Entity\Role; | ||
use Claroline\CoreBundle\Entity\Workspace\Workspace; | ||
|
||
class RemoveGroup extends AbstractAction | ||
{ | ||
/** @var Crud */ | ||
private $crud; | ||
/** @var SerializerProvider */ | ||
private $serializer; | ||
/** @var ObjectManager */ | ||
private $om; | ||
|
||
public function __construct(Crud $crud, SerializerProvider $serializer, ObjectManager $om) | ||
{ | ||
$this->crud = $crud; | ||
$this->serializer = $serializer; | ||
$this->om = $om; | ||
} | ||
|
||
public function execute(array $data, &$successData = []) | ||
{ | ||
// this should be handled by csv validation | ||
if (!isset($data['role'])) { | ||
throw new \Exception('No role set for delete for group '.$this->printError($data['group']).'.'); | ||
} | ||
|
||
$group = $this->om->getObject($data['group'], Group::class, array_keys($data['group'])); | ||
|
||
if (!$group) { | ||
throw new \Exception('Group '.$this->printError($data['group'])." doesn't exists."); | ||
} | ||
|
||
//todo find a generic way to find the identifiers | ||
$workspace = $this->om->getObject($data['workspace'], Workspace::class, ['code']); | ||
|
||
if (!$workspace) { | ||
throw new \Exception('Workspace '.$this->printError($data['workspace'])." doesn't exists."); | ||
} | ||
|
||
$role = $this->om->getRepository('ClarolineCoreBundle:Role') | ||
->findOneBy(['workspace' => $workspace, 'translationKey' => $data['role']['translationKey']]); | ||
|
||
if (!$role) { | ||
throw new \Exception('Role '.$this->printError($data['role'])." doesn't exists."); | ||
} | ||
|
||
$this->crud->patch($group, 'role', 'remove', [$role]); | ||
} | ||
|
||
public function printError(array $el) | ||
{ | ||
$string = ''; | ||
|
||
foreach ($el as $value) { | ||
$string .= ' '.$value; | ||
} | ||
|
||
return $string; | ||
} | ||
|
||
public function getSchema(array $options = [], array $extra = []) | ||
{ | ||
$roleSchema = [ | ||
'$schema' => 'http:\/\/json-schema.org\/draft-04\/schema#', | ||
'type' => 'object', | ||
'properties' => [ | ||
'translationKey' => [ | ||
'type' => 'string', | ||
'description' => 'The role name', | ||
], | ||
], | ||
'claroline' => [ | ||
'requiredAtCreation' => ['translationKey'], | ||
'ids' => ['translationKey'], | ||
'class' => Role::class, | ||
], | ||
]; | ||
|
||
$schema = json_decode(json_encode($roleSchema)); | ||
|
||
$schema = [ | ||
'group' => Group::class, | ||
'role' => $schema, | ||
]; | ||
|
||
if (!in_array(Options::WORKSPACE_IMPORT, $options)) { | ||
$schema['workspace'] = Workspace::class; | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
public function getAction() | ||
{ | ||
return ['workspace', 'remove_group']; | ||
} | ||
|
||
public function supports($format, array $options = [], array $extra = []) | ||
{ | ||
return in_array($format, ['json', 'csv']); | ||
} | ||
} |
Oops, something went wrong.