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