3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @subpackage backup-factories
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 * Non instantiable helper class providing different backup checks
28 * This class contains various static methods available in order to easily
29 * perform a bunch of backup architecture tests
31 * TODO: Finish phpdocs
33 abstract class backup_check {
35 public static function check_format_and_type($format, $type) {
38 $file = $CFG->dirroot . '/backup/' . $format . '/backup_plan_builder.class.php';
39 if (! file_exists($file)) {
40 throw new backup_controller_exception('backup_check_unsupported_format', $format);
43 if (!in_array($type, backup_plan_builder::supported_backup_types())) {
44 throw new backup_controller_exception('backup_check_unsupported_type', $type);
47 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
50 public static function check_id($type, $id) {
53 case backup::TYPE_1ACTIVITY:
54 // id must exist in course_modules table
55 if (! $DB->record_exists('course_modules', array('id' => $id))) {
56 throw new backup_controller_exception('backup_check_module_not_exists', $id);
59 case backup::TYPE_1SECTION:
60 // id must exist in course_sections table
61 if (! $DB->record_exists('course_sections', array('id' => $id))) {
62 throw new backup_controller_exception('backup_check_section_not_exists', $id);
65 case backup::TYPE_1COURSE:
66 // id must exist in course table
67 if (! $DB->record_exists('course', array('id' => $id))) {
68 throw new backup_controller_exception('backup_check_course_not_exists', $id);
72 throw new backup_controller_exception('backup_check_incorrect_type', $type);
77 public static function check_user($userid) {
79 // userid must exist in user table
80 if (! $DB->record_exists('user', array('id' => $userid))) {
81 throw new backup_controller_exception('backup_check_user_not_exists', $userid);
86 public static function check_security($backup_controller, $apply) {
89 if (! $backup_controller instanceof backup_controller) {
90 throw new backup_controller_exception('backup_check_security_requires_backup_controller');
92 $backup_controller->log('checking plan security', backup::LOG_INFO);
95 $type = $backup_controller->get_type();
96 $mode = $backup_controller->get_mode();
97 $courseid = $backup_controller->get_courseid();
98 $coursectx= get_context_instance(CONTEXT_COURSE, $courseid);
99 $userid = $backup_controller->get_userid();
100 $id = $backup_controller->get_id(); // courseid / sectionid / cmid
102 // Note: all the checks along the function MUST be performed for $userid, that
103 // is the user who "requested" the course backup, not current $USER at all!!
105 // First of all, check the main backup[course|section|activity] principal caps
106 // Lacking the corresponding one makes this to break with exception always
108 case backup::TYPE_1COURSE :
109 $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); // course exists
110 require_capability('moodle/backup:backupcourse', $coursectx, $userid);
112 case backup::TYPE_1SECTION :
113 $DB->get_record('course_sections', array('course' => $courseid, 'id' => $id), '*', MUST_EXIST); // sec exists
114 require_capability('moodle/backup:backupsection', $coursectx, $userid);
116 case backup::TYPE_1ACTIVITY :
117 get_coursemodule_from_id(null, $id, $courseid, false, MUST_EXIST); // cm exists
118 $modulectx = get_context_instance(CONTEXT_MODULE, $id);
119 require_capability('moodle/backup:backupactivity', $modulectx, $userid);
122 print_error('unknownbackuptype');
125 // Now, if backup mode is hub or import, check userid has permissions for those modes
127 case backup::MODE_HUB:
128 require_capability('moodle/backup:backuptargethub', $coursectx, $userid);
130 case backup::MODE_IMPORT:
131 require_capability('moodle/backup:backuptargetimport', $coursectx, $userid);
135 // Now, enforce 'moodle/backup:userinfo' to 'users' setting, applying changes if allowed,
136 // else throwing exception
137 $userssetting = $backup_controller->get_plan()->get_setting('users');
138 $prevvalue = $userssetting->get_value();
139 $prevstatus = $userssetting->get_status();
140 $hasusercap = has_capability('moodle/backup:userinfo', $coursectx, $userid);
142 // If setting is enabled but user lacks permission
143 if (!$hasusercap && $prevvalue) { // If user has not the capability and setting is enabled
144 // Now analyse if we are allowed to apply changes or must stop with exception
145 if (!$apply) { // Cannot apply changes, throw exception
147 $a->setting = 'users';
148 $a->value = $prevvalue;
149 $a->capability = 'moodle/backup:userinfo';
150 throw new backup_controller_exception('backup_setting_value_wrong_for_capability', $a);
152 } else { // Can apply changes
153 $userssetting->set_value(false); // Set the value to false
154 $userssetting->set_status(base_setting::LOCKED_BY_PERMISSION);// Set the status to locked by perm
158 // Now, enforce 'moodle/backup:anonymise' to 'anonymise' setting, applying changes if allowed,
159 // else throwing exception
160 $anonsetting = $backup_controller->get_plan()->get_setting('anonymize');
161 $prevvalue = $userssetting->get_value();
162 $prevstatus = $userssetting->get_status();
163 $hasanoncap = has_capability('moodle/backup:anonymise', $coursectx, $userid);
165 // If setting is enabled but user lacks permission
166 if (!$hasanoncap && $prevvalue) { // If user has not the capability and setting is enabled
167 // Now analyse if we are allowed to apply changes or must stop with exception
168 if (!$apply) { // Cannot apply changes, throw exception
170 $a->setting = 'anonymize';
171 $a->value = $prevvalue;
172 $a->capability = 'moodle/backup:anonymise';
173 throw new backup_controller_exception('backup_setting_value_wrong_for_capability', $a);
175 } else { // Can apply changes
176 $anonsetting->set_value(false); // Set the value to false
177 $anonsetting->set_status(base_setting::LOCKED_BY_PERMISSION);// Set the status to locked by perm