Skip to content

Commit

Permalink
Added import to unsubscribe all users from one or more workspaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Aug 25, 2023
1 parent 23bdf89 commit 56a86b9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/core/Resources/config/services/importer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ services:
- '@Claroline\AppBundle\API\SerializerProvider'
- '@Claroline\AppBundle\Persistence\ObjectManager'

Claroline\CoreBundle\Transfer\Importer\Workspace\RemoveAllUsers:
arguments:
- '@Claroline\AppBundle\API\Crud'
- '@Claroline\AppBundle\Persistence\ObjectManager'

Claroline\CoreBundle\Transfer\Importer\Workspace\AddGroup:
arguments:
- '@Claroline\AppBundle\API\Crud'
Expand Down
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
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
1 change: 1 addition & 0 deletions src/main/core/Resources/translations/transfer.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"unarchive": "Unarchive",
"empty_role": "Empty role (users and groups)",
"remove_all_groups": "Delete all groups",
"remove_all_users": "Delete all users",

"directory_id": "The directory id",
"directory_name": "The directory name",
Expand Down
1 change: 1 addition & 0 deletions src/main/core/Resources/translations/transfer.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"unarchive": "Désarchiver",
"empty_role": "Vider le rôle (utilisateurs et groupes)",
"remove_all_groups": "Supprimer tous les groupes",
"remove_all_users": "Supprimer tous les utilisateurs",

"directory_id": "L'id du dossier",
"directory_name": "Le nom du dossier",
Expand Down
95 changes: 95 additions & 0 deletions src/main/core/Transfer/Importer/Workspace/RemoveAllUsers.php
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']);
}
}

0 comments on commit 56a86b9

Please sign in to comment.