-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added import to unsubscribe all users from one or more workspaces.
- Loading branch information
Showing
6 changed files
with
112 additions
and
0 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
5 changes: 5 additions & 0 deletions
5
src/main/core/Resources/samples/workspace/csv/valid/remove_all_users/by_code.csv
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,5 @@ | ||
role.translationKey;workspace.code | ||
RoleTest;MYWS2 | ||
collaborator;MYWS1 | ||
collaborator;MYWS2 | ||
RoleTest;MYWS1 |
5 changes: 5 additions & 0 deletions
5
src/main/core/Resources/samples/workspace/csv/valid/remove_all_users/by_id.csv
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,5 @@ | ||
role.translationKey;workspace.id | ||
RoleTest;9ea9deb3-8d58-42ed-8fb3-1c875e5b6654 | ||
collaborator;9ea9deb3-8d58-42ed-8fb3-1c875e5b6653 | ||
collaborator;9ea9deb3-8d58-42ed-8fb3-1c875e5b6654 | ||
RoleTest;9ea9deb3-8d58-42ed-8fb3-1c875e5b6653 |
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
95 changes: 95 additions & 0 deletions
95
src/main/core/Transfer/Importer/Workspace/RemoveAllUsers.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,95 @@ | ||
<?php | ||
|
||
namespace Claroline\CoreBundle\Transfer\Importer\Workspace; | ||
|
||
use Claroline\AppBundle\API\Crud; | ||
use Claroline\AppBundle\Persistence\ObjectManager; | ||
use Claroline\CoreBundle\Entity\User; | ||
use Claroline\CoreBundle\Entity\Role; | ||
use Claroline\CoreBundle\Entity\Workspace\Workspace; | ||
use Claroline\TransferBundle\Transfer\Importer\AbstractImporter; | ||
|
||
class RemoveAllUsers extends AbstractImporter | ||
{ | ||
/** @var Crud */ | ||
private $crud; | ||
/** @var ObjectManager */ | ||
private $om; | ||
|
||
public function __construct(Crud $crud, ObjectManager $om) | ||
{ | ||
$this->crud = $crud; | ||
$this->om = $om; | ||
} | ||
|
||
public function execute(array $data): array | ||
{ | ||
if (!isset($data['workspace'])) { | ||
throw new \InvalidArgumentException('The "workspace" key is missing or is not an array.'); | ||
} | ||
|
||
$workspace = $this->om->getRepository(Workspace::class)->findOneBy($data['workspace']); | ||
|
||
if (!$workspace) { | ||
throw new \Exception('Workspace '.$this->printError($data['workspace'])." doesn't exists."); | ||
} | ||
|
||
$users = $this->om->getRepository(User::class)->findByWorkspaces([$workspace]); | ||
|
||
foreach ($users as $user) { | ||
$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($user, 'role', 'remove', [$role]); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function printError(array $el): string | ||
{ | ||
$string = ''; | ||
|
||
foreach ($el as $value) { | ||
$string .= ' '.$value; | ||
} | ||
|
||
return $string; | ||
} | ||
|
||
public function getSchema(?array $options = [], ?array $extra = []): array | ||
{ | ||
return [ | ||
'workspace' => Workspace::class, | ||
'role' => json_decode(json_encode([ | ||
'$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, | ||
], | ||
])), | ||
]; | ||
} | ||
|
||
public static function getAction(): array | ||
{ | ||
return ['workspace', 'remove_all_users']; | ||
} | ||
|
||
public function supports(string $format, ?array $options = [], ?array $extra = []): bool | ||
{ | ||
return in_array($format, ['json', 'csv']); | ||
} | ||
} |