1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* A scheduled task for LDAP user sync.
|
|
|
19 |
*
|
|
|
20 |
* @package auth_ldap
|
|
|
21 |
* @copyright 2015 Vadim Dvorovenko <Vadimon@mail.ru>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace auth_ldap\task;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* A scheduled task class for LDAP user sync.
|
|
|
29 |
*
|
|
|
30 |
* @copyright 2015 Vadim Dvorovenko <Vadimon@mail.ru>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class sync_task extends \core\task\scheduled_task {
|
|
|
34 |
|
|
|
35 |
/** @var string Message prefix for mtrace */
|
|
|
36 |
protected const MTRACE_MSG = 'Synced ldap users';
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Get a descriptive name for this task (shown to admins).
|
|
|
40 |
*
|
|
|
41 |
* @return string
|
|
|
42 |
*/
|
|
|
43 |
public function get_name() {
|
|
|
44 |
return get_string('synctask', 'auth_ldap');
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Run users sync.
|
|
|
49 |
*/
|
|
|
50 |
public function execute() {
|
|
|
51 |
if (is_enabled_auth('ldap')) {
|
|
|
52 |
/** @var auth_plugin_ldap $auth */
|
|
|
53 |
$auth = get_auth_plugin('ldap');
|
|
|
54 |
$count = 0;
|
|
|
55 |
$auth->sync_users_update_callback(function ($users, $updatekeys) use (&$count) {
|
|
|
56 |
$asynctask = new asynchronous_sync_task();
|
|
|
57 |
$asynctask->set_custom_data([
|
|
|
58 |
'users' => $users,
|
|
|
59 |
'updatekeys' => $updatekeys,
|
|
|
60 |
]);
|
|
|
61 |
\core\task\manager::queue_adhoc_task($asynctask);
|
|
|
62 |
|
|
|
63 |
$count++;
|
|
|
64 |
mtrace(sprintf(" %s (%d)", self::MTRACE_MSG, $count));
|
|
|
65 |
sleep(1);
|
|
|
66 |
});
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
}
|