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