Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transfer] Added import to unsubscribe groups/users from one or more workspaces. #2572

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 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 All @@ -60,6 +65,11 @@ services:
- '@Claroline\AppBundle\API\SerializerProvider'
- '@Claroline\AppBundle\Persistence\ObjectManager'

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

Claroline\CoreBundle\Transfer\Importer\Workspace\EmptyRole:
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
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
2 changes: 2 additions & 0 deletions src/main/core/Resources/translations/transfer.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"archive": "Archive",
"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
2 changes: 2 additions & 0 deletions src/main/core/Resources/translations/transfer.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"archive": "Archiver",
"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/RemoveAllGroups.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\Group;
use Claroline\CoreBundle\Entity\Role;
use Claroline\CoreBundle\Entity\Workspace\Workspace;
use Claroline\TransferBundle\Transfer\Importer\AbstractImporter;

class RemoveAllGroups 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.");
}

$groups = $this->om->getRepository(Group::class)->findByWorkspace($workspace);

foreach ($groups as $group) {
$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', '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_groups'];
}

public function supports(string $format, ?array $options = [], ?array $extra = []): bool
{
return in_array($format, ['json', 'csv']);
}
}
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\Role;
use Claroline\CoreBundle\Entity\User;
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']);
}
}