|
| 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 | +namespace tool_objectfs\check; |
| 18 | + |
| 19 | +use core\check\check; |
| 20 | +use core\check\result; |
| 21 | + |
| 22 | +/** |
| 23 | + * Token expiry check. |
| 24 | + * |
| 25 | + * @package tool_objectfs |
| 26 | + * @author Matthew Hilton <matthewhilton@catalyst-au.net> |
| 27 | + * @copyright Catalyst IT |
| 28 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 29 | + */ |
| 30 | +class token_expiry extends check { |
| 31 | + /** |
| 32 | + * Checks the token expiry time against thresholds |
| 33 | + * @return result |
| 34 | + */ |
| 35 | + public function get_result(): result { |
| 36 | + $config = \tool_objectfs\local\manager::get_objectfs_config(); |
| 37 | + $client = \tool_objectfs\local\manager::get_client($config); |
| 38 | + |
| 39 | + // No client set - n/a. |
| 40 | + if (empty($client)) { |
| 41 | + return new result(result::NA, get_string('check:tokenexpiry:na', 'tool_objectfs')); |
| 42 | + } |
| 43 | + |
| 44 | + $expirytime = $client->get_token_expiry_time(); |
| 45 | + $secondsleft = $expirytime - time(); |
| 46 | + |
| 47 | + $strparams = [ |
| 48 | + 'dayssince' => abs(round($secondsleft / DAYSECS)), |
| 49 | + 'time' => userdate($expirytime), |
| 50 | + ]; |
| 51 | + |
| 52 | + // Not implemented or token not set - n/a. |
| 53 | + if ($expirytime == -1) { |
| 54 | + return new result(result::NA, get_string('check:tokenexpiry:na', 'tool_objectfs')); |
| 55 | + } |
| 56 | + |
| 57 | + // Is in past - token has expired. |
| 58 | + if ($secondsleft < 0) { |
| 59 | + return new result(result::CRITICAL, get_string('check:tokenexpiry:expired', 'tool_objectfs', $strparams)); |
| 60 | + } |
| 61 | + |
| 62 | + // Is in warning period - warn. |
| 63 | + $warnthreshold = (int) $config->tokenexpirywarnperiod; |
| 64 | + if ($secondsleft < $warnthreshold) { |
| 65 | + return new result(result::WARNING, get_string('check:tokenexpiry:expiresin', 'tool_objectfs', $strparams)); |
| 66 | + } |
| 67 | + |
| 68 | + // Else ok. |
| 69 | + return new result(result::OK, get_string('check:tokenexpiry:expiresin', 'tool_objectfs', $strparams)); |
| 70 | + } |
| 71 | +} |
0 commit comments