Passed
Pull Request — master (#6127)
by Angel Fernando Quiroz
40:13 queued 28:46
created

MigrationMoodleEventSubscriber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 76
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onCheckLoginCredentials() 0 28 5
A getSubscribedEvents() 0 4 1
A getExtraFieldValue() 0 5 1
A getExtraField() 0 8 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ExtraField. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
9
use Chamilo\CoreBundle\Event\CheckLoginCredentialsEvent;
10
use Chamilo\CoreBundle\Event\Events;
11
use Doctrine\ORM\Exception\NotSupported;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
class MigrationMoodleEventSubscriber implements EventSubscriberInterface
16
{
17
    private MigrationMoodlePlugin $plugin;
18
19
    public function __construct()
20
    {
21
        $this->plugin = MigrationMoodlePlugin::create();
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30
            Events::CHECK_LOGIN_CREDENTIALS => 'onCheckLoginCredentials',
31
        ];
32
    }
33
34
    /**
35
     * @throws NotSupported
36
     */
37
    public function onCheckLoginCredentials(CheckLoginCredentialsEvent $event): void
38
    {
39
        if (!$this->plugin->isEnabled(true)) {
40
            return;
41
        }
42
43
        $userData = $event->getUser();
44
        $credentials = $event->getCredentials();
45
46
        $extraField = $this->getExtraField();
47
48
        if (empty($extraField)) {
49
            return;
50
        }
51
52
        $fieldValue = $this->getExtraFieldValue($extraField, $userData);
53
54
        if (empty($fieldValue)) {
55
            return;
56
        }
57
58
        $isPasswordVerified = password_verify(
59
            $credentials['password'],
60
            $fieldValue->getFieldValue()
61
        );
62
63
        if (!$isPasswordVerified) {
64
            throw new AccessDeniedException();
65
        }
66
    }
67
68
    /**
69
     * @throws NotSupported
70
     */
71
    private function getExtraField(): ?ExtraField
72
    {
73
        return Database::getManager()
74
            ->getRepository(ExtraField::class)
75
            ->findOneBy(
76
                [
77
                    'variable' => 'moodle_password',
78
                    'extraFieldType' => ExtraField::USER_FIELD_TYPE,
79
                ]
80
            );
81
    }
82
83
    /**
84
     * @throws NotSupported
85
     */
86
    private function getExtraFieldValue(ExtraField $extraField, array $userData): ?ExtraFieldValues
87
    {
88
        return Database::getManager()
89
            ->getRepository(ExtraFieldValues::class)
90
            ->findOneBy(['field' => $extraField, 'itemId' => $userData['id']]);
91
    }
92
}
93