1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Command; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\Usergroup; |
10
|
|
|
use Exception; |
11
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
16
|
|
|
|
17
|
|
|
#[AsCommand( |
18
|
|
|
name: 'app:azure-sync-usergroups', |
19
|
|
|
description: 'Synchronize groups registered in Azure with Chamilo user groups', |
20
|
|
|
)] |
21
|
|
|
class AzureSyncUsergroupsCommand extends AzureSyncAbstractCommand |
22
|
|
|
{ |
23
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
24
|
|
|
{ |
25
|
|
|
$io = new SymfonyStyle($input, $output); |
26
|
|
|
$io->title('Synchronizing groups from Azure.'); |
27
|
|
|
|
28
|
|
|
$accessUrl = $this->accessUrlHelper->getCurrent(); |
29
|
|
|
|
30
|
|
|
/** @var array<string, Usergroup> $groupIdByUid */ |
31
|
|
|
$groupIdByUid = []; |
32
|
|
|
|
33
|
|
|
$admin = $this->userRepository->getRootUser(); |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
foreach ($this->getAzureGroups() as $azureGroupInfo) { |
37
|
|
|
$userGroup = $this->usergroupRepository->getOneByTitleInUrl($azureGroupInfo['displayName'], $accessUrl); |
38
|
|
|
|
39
|
|
|
if ($userGroup) { |
40
|
|
|
$userGroup->getUsers()->clear(); |
41
|
|
|
|
42
|
|
|
$io->text( |
43
|
|
|
sprintf( |
44
|
|
|
'Class exists, all users unsubscribed: %s (ID %d)', |
45
|
|
|
$userGroup->getTitle(), |
46
|
|
|
$userGroup->getId() |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
} else { |
50
|
|
|
$userGroup = (new Usergroup()) |
51
|
|
|
->setTitle($azureGroupInfo['displayName']) |
52
|
|
|
->setDescription($azureGroupInfo['description']) |
53
|
|
|
->setCreator($admin) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
if ('true' === $this->settingsManager->getSetting('profile.allow_teachers_to_classes')) { |
57
|
|
|
$userGroup->setAuthorId( |
58
|
|
|
$this->userHelper->getCurrent()->getId() |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$userGroup->addAccessUrl($accessUrl); |
63
|
|
|
|
64
|
|
|
$this->usergroupRepository->create($userGroup); |
65
|
|
|
|
66
|
|
|
$io->text(sprintf('Class created: %s (ID %d)', $userGroup->getTitle(), $userGroup->getId())); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$groupIdByUid[$azureGroupInfo['id']] = $userGroup; |
70
|
|
|
} |
71
|
|
|
} catch (Exception $e) { |
72
|
|
|
$io->error($e->getMessage()); |
73
|
|
|
|
74
|
|
|
return Command::INVALID; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$io->section('Subscribing users to groups'); |
78
|
|
|
|
79
|
|
|
foreach ($groupIdByUid as $azureGroupUid => $group) { |
80
|
|
|
$newGroupMembers = []; |
81
|
|
|
|
82
|
|
|
$io->text(sprintf('Obtaining members for group (ID %d)', $group->getId())); |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
foreach ($this->getAzureGroupMembers($azureGroupUid) as $azureGroupMember) { |
86
|
|
|
if ($userId = $this->azureHelper->getUserIdByVerificationOrder($azureGroupMember)) { |
87
|
|
|
$newGroupMembers[] = $userId; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} catch (Exception $e) { |
91
|
|
|
$io->warning($e->getMessage()); |
92
|
|
|
|
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
foreach ($newGroupMembers as $newGroupMemberId) { |
97
|
|
|
$user = $this->userRepository->find($newGroupMemberId); |
98
|
|
|
|
99
|
|
|
$group->addUser($user); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$io->text( |
103
|
|
|
sprintf( |
104
|
|
|
'User IDs subscribed in class (ID %d): %s', |
105
|
|
|
$group->getId(), |
106
|
|
|
implode(', ', $newGroupMembers) |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->entityManager->flush(); |
112
|
|
|
|
113
|
|
|
$io->success('Done.'); |
114
|
|
|
|
115
|
|
|
return Command::SUCCESS; |
116
|
|
|
} |
117
|
|
|
} |