|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chamilo\CoreBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\User; |
|
6
|
|
|
use Chamilo\CoreBundle\Entity\UserAuthSource; |
|
7
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
14
|
|
|
|
|
15
|
|
|
#[AsCommand( |
|
16
|
|
|
name: 'app:azure-sync-users', |
|
17
|
|
|
description: 'Synchronize users accounts registered in Azure with Chamilo user accounts', |
|
18
|
|
|
)] |
|
19
|
|
|
class AzureSyncUsersCommand extends AzureSyncAbstractCommand |
|
20
|
|
|
{ |
|
21
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
22
|
|
|
{ |
|
23
|
|
|
$io = new SymfonyStyle($input, $output); |
|
24
|
|
|
|
|
25
|
|
|
$io->title('Synchronizing users from Azure.'); |
|
26
|
|
|
|
|
27
|
|
|
/** @var array<string, int> $azureCreatedUserIdList */ |
|
28
|
|
|
$azureCreatedUserIdList = []; |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
|
|
foreach ($this->getAzureUsers() as $azureUserInfo) { |
|
32
|
|
|
try { |
|
33
|
|
|
$user = $this->azureHelper->registerUser($azureUserInfo); |
|
34
|
|
|
} catch (NonUniqueResultException $e) { |
|
35
|
|
|
$io->warning($e->getMessage()); |
|
36
|
|
|
|
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$azureCreatedUserIdList[$azureUserInfo['id']] = $user->getId(); |
|
41
|
|
|
|
|
42
|
|
|
$io->text( |
|
43
|
|
|
sprintf('User (ID %d) with received info: %s ', $user->getId(), serialize($azureUserInfo)) |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
} catch (Exception $e) { |
|
47
|
|
|
$io->error($e->getMessage()); |
|
48
|
|
|
|
|
49
|
|
|
return Command::INVALID; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$io->section('Updating users status'); |
|
53
|
|
|
|
|
54
|
|
|
$roleGroups = $this->getGroupUidByRole(); |
|
55
|
|
|
$roleActions = $this->getUpdateActionByRole(); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($roleGroups as $userRole => $groupUid) { |
|
58
|
|
|
try { |
|
59
|
|
|
$azureGroupMembersInfo = iterator_to_array($this->getAzureGroupMembers($groupUid)); |
|
60
|
|
|
} catch (Exception $e) { |
|
61
|
|
|
$io->warning($e->getMessage()); |
|
62
|
|
|
|
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$azureGroupMembersUids = array_column($azureGroupMembersInfo, 'id'); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($azureGroupMembersUids as $azureGroupMembersUid) { |
|
69
|
|
|
$userId = $azureCreatedUserIdList[$azureGroupMembersUid] ?? null; |
|
70
|
|
|
|
|
71
|
|
|
if (!$userId) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (isset($roleActions[$userRole])) { |
|
76
|
|
|
$user = $this->userRepository->find($userId); |
|
77
|
|
|
|
|
78
|
|
|
$roleActions[$userRole]($user); |
|
79
|
|
|
|
|
80
|
|
|
$io->text( |
|
81
|
|
|
sprintf('User (ID %d) status %s', $userId, $userRole) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->entityManager->flush(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->providerParams['deactivate_nonexisting_users'] |
|
90
|
|
|
&& !$this->providerParams['script_users_delta'] |
|
91
|
|
|
) { |
|
92
|
|
|
$io->section('Trying deactivate non-existing users in Azure'); |
|
93
|
|
|
|
|
94
|
|
|
$users = $this->userRepository->findByAuthsource(UserAuthSource::AZURE); |
|
95
|
|
|
|
|
96
|
|
|
$chamiloUserIdList = array_map( |
|
97
|
|
|
fn(User $user) => $user->getId(), |
|
98
|
|
|
$users |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$nonExistingUsers = array_diff($chamiloUserIdList, $azureCreatedUserIdList); |
|
102
|
|
|
|
|
103
|
|
|
$this->userRepository->deactivateUsers($nonExistingUsers); |
|
104
|
|
|
|
|
105
|
|
|
$io->text( |
|
106
|
|
|
sprintf( |
|
107
|
|
|
'Deactivated users IDs: %s', |
|
108
|
|
|
implode(', ', $nonExistingUsers) |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$io->success('You have a new command! Now make it your own! Pass --help to see your options.'); |
|
114
|
|
|
|
|
115
|
|
|
return Command::SUCCESS; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|