1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\LDAP\Jobs; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use SilverStripe\LDAP\Tasks\LDAPGroupSyncTask; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
10
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
11
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class LDAPAllSyncJob |
15
|
|
|
* |
16
|
|
|
* A {@link QueuedJob} job to sync all groups and members to the site using LDAP. |
17
|
|
|
* This doesn't do the actual sync work, but rather just triggers {@link LDAPGroupSyncTask} and |
18
|
|
|
* {@link LDAPMemberSyncTask} |
19
|
|
|
*/ |
20
|
|
|
class LDAPAllSyncJob extends AbstractQueuedJob |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* If you specify this value in seconds, it tells the completed job to queue another of itself |
24
|
|
|
* x seconds ahead of time. |
25
|
|
|
* |
26
|
|
|
* @var mixed |
27
|
|
|
* @config |
28
|
|
|
*/ |
29
|
|
|
private static $regenerate_time = null; |
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
// noop, but needed for QueuedJobsAdmin::createjob() to work |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getJobType() |
40
|
|
|
{ |
41
|
|
|
return QueuedJob::QUEUED; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function getTitle() |
48
|
|
|
{ |
49
|
|
|
return _t(__CLASS__ . '.SYNCTITLE', 'Sync all groups and users from Active Directory, and set mappings up.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getSignature() |
56
|
|
|
{ |
57
|
|
|
return md5(get_class($this)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @throws Exception |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function validateRegenerateTime() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time'); |
66
|
|
|
|
67
|
|
|
// don't allow this job to run less than every 15 minutes, as it could take a while. |
68
|
|
|
if ($regenerateTime !== null && $regenerateTime < 900) { |
69
|
|
|
throw new Exception('LDAPAllSyncJob::regenerate_time must be 15 minutes or greater'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
|
|
public function process() |
77
|
|
|
{ |
78
|
|
|
$regenerateTime = Config::inst()->get(LDAPAllSyncJob::class, 'regenerate_time'); |
79
|
|
View Code Duplication |
if ($regenerateTime) { |
|
|
|
|
80
|
|
|
$this->validateRegenerateTime(); |
81
|
|
|
|
82
|
|
|
$nextJob = Injector::inst()->create(LDAPAllSyncJob::class); |
83
|
|
|
singleton(QueuedJobService::class)->queueJob($nextJob, date('Y-m-d H:i:s', time() + $regenerateTime)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$task = Injector::inst()->create(LDAPGroupSyncTask::class); |
87
|
|
|
$task->run(null); |
88
|
|
|
|
89
|
|
|
$task = Injector::inst()->create(LDAPGroupSyncTask::class); |
90
|
|
|
$task->run(null); |
91
|
|
|
|
92
|
|
|
$this->isComplete = true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.