Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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.
19
 *
20
 * @package    repository_onedrive
21
 * @copyright  2017 Damyon Wiese
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace repository_onedrive;
26
 
27
use \core\task\scheduled_task;
28
use DateTime;
29
use DateInterval;
30
use repository_exception;
31
use \core\oauth2\rest_exception;
32
 
33
defined('MOODLE_INTERNAL') || die();
34
 
35
require_once($CFG->dirroot . '/repository/lib.php');
36
 
37
/**
38
 * Simple task to delete temporary permission records.
39
 * @package    repository_onedrive
40
 * @copyright  2017 Damyon Wiese
41
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42
 */
43
class remove_temp_access_task extends scheduled_task {
44
 
45
    /**
46
     * Get a descriptive name for this task (shown to admins).
47
     *
48
     * @return string
49
     */
50
    public function get_name() {
51
        return get_string('removetempaccesstask', 'repository_onedrive');
52
    }
53
 
54
    /**
55
     * Do the job.
56
     * Throw exceptions on errors (the job will be retried).
57
     */
58
    public function execute() {
59
        $accessrecords = access::get_records();
60
        $expires = new DateTime();
61
        $expires->sub(new DateInterval("P7D"));
62
        $timestamp = $expires->getTimestamp();
63
 
64
        $issuerid = get_config('onedrive', 'issuerid');
65
        $issuer = \core\oauth2\api::get_issuer($issuerid);
66
 
67
        // Add the current user as an OAuth writer.
68
        $systemauth = \core\oauth2\api::get_system_oauth_client($issuer);
69
 
70
        if ($systemauth === false) {
71
            $details = 'Cannot connect as system user';
72
            throw new repository_exception('errorwhilecommunicatingwith', 'repository', '', $details);
73
        }
74
        $systemservice = new \repository_onedrive\rest($systemauth);
75
 
76
        foreach ($accessrecords as $access) {
77
            if ($access->get('timemodified') < $timestamp) {
78
                $params = ['permissionid' => $access->get('permissionid'), 'itemid' => $access->get('itemid')];
79
                try {
80
                    $systemservice->call('delete_permission', $params);
81
                } catch (rest_exception $re) {
82
                    // We log and give up here or we will always fail for eternity.
83
                    mtrace('Failed to remove access from file: ' . $access->get('itemid'));
84
                }
85
                $access->delete();
86
            }
87
        }
88
    }
89
 
90
}