Passed
Pull Request — 1.11.x (#5763)
by Angel Fernando Quiroz
08:35
created

AzureSyncUsersCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 42
c 3
b 0
f 0
dl 0
loc 81
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 74 8
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use Chamilo\UserBundle\Entity\User;
6
7
class AzureSyncUsersCommand extends AzureCommand
8
{
9
    /**
10
     * @throws Exception
11
     *
12
     * @return Generator<int, string>
13
     */
14
    public function __invoke(): Generator
15
    {
16
        yield 'Synchronizing users from Azure.';
17
18
        /** @var array<string, int> $existingUsers */
19
        $existingUsers = [];
20
21
        foreach ($this->getAzureUsers() as $azureUserInfo) {
22
            try {
23
                $userId = $this->plugin->registerUser($azureUserInfo, 'id');
24
            } catch (Exception $e) {
25
                yield $e->getMessage();
26
27
                continue;
28
            }
29
30
            $existingUsers[$azureUserInfo['id']] = $userId;
31
32
            yield sprintf('User (ID %d) with received info: %s ', $userId, serialize($azureUserInfo));
33
        }
34
35
        yield '----------------';
36
        yield 'Updating users status';
37
38
        $roleGroups = $this->plugin->getGroupUidByRole();
39
        $roleActions = $this->plugin->getUpdateActionByRole();
40
41
        $userManager = UserManager::getManager();
42
        $em = Database::getManager();
43
44
        foreach ($roleGroups as $userRole => $groupUid) {
45
            $azureGroupMembersInfo = iterator_to_array($this->getAzureGroupMembers($groupUid));
46
            $azureGroupMembersUids = array_column($azureGroupMembersInfo, 'id');
47
48
            foreach ($azureGroupMembersUids as $azureGroupMembersUid) {
49
                $userId = $existingUsers[$azureGroupMembersUid] ?? null;
50
51
                if (!$userId) {
52
                    continue;
53
                }
54
55
                if (isset($roleActions[$userRole])) {
56
                    /** @var User $user */
57
                    $user = $userManager->find($userId);
58
59
                    $roleActions[$userRole]($user);
60
61
                    yield sprintf('User (ID %d) status %s', $userId, $userRole);
62
                }
63
            }
64
65
            $em->flush();
66
        }
67
68
        if ('true' === $this->plugin->get(AzureActiveDirectory::SETTING_DEACTIVATE_NONEXISTING_USERS)) {
69
            yield '----------------';
70
71
            yield 'Trying deactivate non-existing users in Azure';
72
73
            $users = UserManager::getRepository()->findByAuthSource('azure');
74
            $userIdList = array_map(
75
                function ($user) {
76
                    return $user->getId();
77
                },
78
                $users
79
            );
80
81
            $nonExistingUsers = array_diff($userIdList, $existingUsers);
82
83
            UserManager::deactivate_users($nonExistingUsers);
84
85
            yield sprintf(
86
                'Deactivated users IDs: %s',
87
                implode(', ', $nonExistingUsers)
88
            );
89
        }
90
    }
91
}
92