1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\LDAP\Tasks; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\Control\HTTPRequest; |
7
|
|
|
use SilverStripe\LDAP\Services\LDAPService; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LDAPMemberSyncOneTask |
11
|
|
|
* @package SilverStripe\LDAP\Tasks |
12
|
|
|
* |
13
|
|
|
* Debug build task that can be used to sync a single member by providing their email address registered in LDAP. |
14
|
|
|
* |
15
|
|
|
* Usage: /dev/tasks/[email protected] |
16
|
|
|
*/ |
17
|
|
|
class LDAPMemberSyncOneTask extends LDAPMemberSyncTask |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $segment = 'LDAPMemberSyncOneTask'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $dependencies = [ |
|
|
|
|
29
|
|
|
'LDAPService' => '%$' . LDAPService::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var LDAPService |
34
|
|
|
*/ |
35
|
|
|
protected $ldapService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getTitle() |
41
|
|
|
{ |
42
|
|
|
return _t(__CLASS__ . '.SYNCONETITLE', 'Sync single user from LDAP'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Syncs a single user based on the email address passed in the URL |
47
|
|
|
* |
48
|
|
|
* @param HTTPRequest $request |
49
|
|
|
*/ |
50
|
|
|
public function run($request) |
51
|
|
|
{ |
52
|
|
|
$email = $request->getVar('email'); |
53
|
|
|
|
54
|
|
|
if (!$email) { |
55
|
|
|
echo 'You must supply an email parameter to this method.', PHP_EOL; |
56
|
|
|
exit; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$user = $this->ldapService->getUserByEmail($email); |
60
|
|
|
|
61
|
|
|
if (!$user) { |
|
|
|
|
62
|
|
|
echo sprintf('No user found in LDAP for email %s', $email), PHP_EOL; |
63
|
|
|
exit; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$member = $this->findOrCreateMember($user); |
67
|
|
|
|
68
|
|
|
// If member exists already, we're updating - otherwise we're creating |
69
|
|
|
if ($member->exists()) { |
70
|
|
|
$this->log(sprintf( |
71
|
|
|
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)', |
72
|
|
|
$user['objectguid'], |
73
|
|
|
$member->getName(), |
74
|
|
|
$member->ID, |
75
|
|
|
$user['samaccountname'] |
76
|
|
|
)); |
77
|
|
|
} else { |
78
|
|
|
$this->log(sprintf( |
79
|
|
|
'Creating new Member %s: "%s" (SAM Account Name: %s)', |
80
|
|
|
$user['objectguid'], |
81
|
|
|
$user['cn'], |
82
|
|
|
$user['samaccountname'] |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->log('User data returned from LDAP follows:'); |
87
|
|
|
$this->log(var_export($user)); |
|
|
|
|
88
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$this->ldapService->updateMemberFromLDAP($member, $user); |
91
|
|
|
$this->log('Done!'); |
92
|
|
|
} catch (Exception $e) { |
93
|
|
|
$this->log($e->getMessage()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param LDAPService $service |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setLDAPService(LDAPService $service) |
102
|
|
|
{ |
103
|
|
|
$this->ldapService = $service; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|