1
|
|
|
<?php |
2
|
|
|
/* For licensing terms, see /license.txt */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class HookCreateDrupalUser |
6
|
|
|
* Hook to create an user in Drupal website. |
7
|
|
|
* |
8
|
|
|
* @author Angel Fernando Quiroz Campos <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @package chamilo.plugin.createDrupalUser |
11
|
|
|
*/ |
12
|
|
|
class HookCreateDrupalUser extends HookObserver implements HookCreateUserObserverInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct( |
20
|
|
|
'plugin/createdrupaluser/src/CreateDrupalUser.php', |
21
|
|
|
'drupaluser' |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a Drupal user when the Chamilo user is registered. |
27
|
|
|
* |
28
|
|
|
* @param HookCreateUserEventInterface $hook The hook |
29
|
|
|
*/ |
30
|
|
|
public function hookCreateUser(HookCreateUserEventInterface $hook) |
31
|
|
|
{ |
32
|
|
|
$data = $hook->getEventData(); |
33
|
|
|
|
34
|
|
|
$drupalDomain = CreateDrupalUser::create()->get('drupal_domain'); |
35
|
|
|
$drupalDomain = rtrim($drupalDomain, '/').'/'; |
36
|
|
|
|
37
|
|
|
if ($data['type'] === HOOK_EVENT_TYPE_POST) { |
38
|
|
|
$return = $data['return']; |
39
|
|
|
$originalPassword = $data['originalPassword']; |
40
|
|
|
|
41
|
|
|
$userInfo = api_get_user_info($return); |
42
|
|
|
$fields = [ |
43
|
|
|
'name' => $userInfo['username'], |
44
|
|
|
'pass' => $originalPassword, |
45
|
|
|
'mail' => $userInfo['email'], |
46
|
|
|
'status' => 1, |
47
|
|
|
'init' => $userInfo['email'], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$extraFields = [ |
51
|
|
|
'first_name' => $userInfo['firstname'], |
52
|
|
|
'last_name' => $userInfo['lastname'], |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$options = [ |
56
|
|
|
'location' => $drupalDomain.'sites/all/modules/chamilo/soap.php?wsdl', |
57
|
|
|
'uri' => $drupalDomain, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$client = new SoapClient(null, $options); |
61
|
|
|
$drupalUserId = false; |
62
|
|
|
|
63
|
|
|
if (isset($_SESSION['ws_drupal_user_id'])) { |
64
|
|
|
$drupalUserId = $_SESSION['ws_drupal_user_id']; |
65
|
|
|
|
66
|
|
|
return true; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($drupalUserId === false) { |
70
|
|
|
$drupalUserId = $client->addUser($fields, $extraFields); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($drupalUserId !== false) { |
74
|
|
|
UserManager::update_extra_field_value($return, 'drupal_user_id', $drupalUserId); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: