1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
use Chamilo\CoreBundle\Event\UserCreatedEvent; |
8
|
|
|
use Chamilo\CoreBundle\Event\Events; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
|
11
|
|
|
class CreateDrupalUserEventSubscriber implements EventSubscriberInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @inheritDoc |
15
|
|
|
*/ |
16
|
|
|
public static function getSubscribedEvents(): array |
17
|
|
|
{ |
18
|
|
|
return [ |
19
|
|
|
Events::USER_CREATED => 'onCreateUser', |
20
|
|
|
]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function onCreateUser(UserCreatedEvent $event): void |
24
|
|
|
{ |
25
|
|
|
$drupalDomain = CreateDrupalUser::create()->get('drupal_domain'); |
26
|
|
|
$drupalDomain = rtrim($drupalDomain, '/').'/'; |
27
|
|
|
|
28
|
|
|
if (HOOK_EVENT_TYPE_POST === $event->getType()) { |
29
|
|
|
$return = $event->getUser(); |
30
|
|
|
$originalPassword = $event->getOriginalPassword(); |
31
|
|
|
|
32
|
|
|
$userInfo = api_get_user_info($return->getId()); |
33
|
|
|
$fields = [ |
34
|
|
|
'name' => $userInfo['username'], |
35
|
|
|
'pass' => $originalPassword, |
36
|
|
|
'mail' => $userInfo['email'], |
37
|
|
|
'status' => 1, |
38
|
|
|
'init' => $userInfo['email'], |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
$extraFields = [ |
42
|
|
|
'first_name' => $userInfo['firstname'], |
43
|
|
|
'last_name' => $userInfo['lastname'], |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
$options = [ |
47
|
|
|
'location' => $drupalDomain.'sites/all/modules/chamilo/soap.php?wsdl', |
48
|
|
|
'uri' => $drupalDomain, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$client = new SoapClient(null, $options); |
52
|
|
|
$drupalUserId = false; |
53
|
|
|
|
54
|
|
|
if (isset($_SESSION['ws_drupal_user_id'])) { |
55
|
|
|
//$drupalUserId = $_SESSION['ws_drupal_user_id']; |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (false === $drupalUserId) { |
61
|
|
|
$drupalUserId = $client->addUser($fields, $extraFields); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (false !== $drupalUserId) { |
65
|
|
|
UserManager::update_extra_field_value($return->getId(), 'drupal_user_id', $drupalUserId); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|