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