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 restore checks
28 * This class contains various static methods available in order to easily
29 * perform a bunch of restore architecture tests
31 * TODO: Finish phpdocs
33 abstract class restore_check
{
35 public static function check_courseid($courseid) {
37 // id must exist in course table
38 if (! $DB->record_exists('course', array('id' => $courseid))) {
39 throw new restore_controller_exception('restore_check_course_not_exists', $courseid);
44 public static function check_user($userid) {
46 // userid must exist in user table
47 if (! $DB->record_exists('user', array('id' => $userid))) {
48 throw new restore_controller_exception('restore_check_user_not_exists', $userid);
53 public static function check_security($restore_controller, $apply) {
56 if (! $restore_controller instanceof restore_controller
) {
57 throw new restore_controller_exception('restore_check_security_requires_restore_controller');
59 $restore_controller->log('checking plan security', backup
::LOG_INFO
);
62 $type = $restore_controller->get_type();
63 $mode = $restore_controller->get_mode();
64 $courseid = $restore_controller->get_courseid();
65 $coursectx= context_course
::instance($courseid);
66 $userid = $restore_controller->get_userid();
68 // Note: all the checks along the function MUST be performed for $userid, that
69 // is the user who "requested" the course restore, not current $USER at all!!
71 // First of all, decide which caps/contexts are we going to check
72 // for common backups (general, automated...) based exclusively
73 // in the type (course, section, activity). And store them into
74 // one capability => context array structure
75 $typecapstocheck = array();
77 case backup
::TYPE_1COURSE
:
78 $typecapstocheck['moodle/restore:restorecourse'] = $coursectx;
80 case backup
::TYPE_1SECTION
:
81 $typecapstocheck['moodle/restore:restoresection'] = $coursectx;
83 case backup
::TYPE_1ACTIVITY
:
84 $typecapstocheck['moodle/restore:restoreactivity'] = $coursectx;
87 throw new restore_controller_exception('restore_unknown_restore_type', $type);
90 // Now, if restore mode is hub or import, check userid has permissions for those modes
91 // other modes will perform common checks only (restorexxxx capabilities in $typecapstocheck)
93 case backup
::MODE_HUB
:
94 if (!has_capability('moodle/restore:restoretargethub', $coursectx, $userid)) {
97 $a->courseid
= $courseid;
98 $a->capability
= 'moodle/restore:restoretargethub';
99 throw new restore_controller_exception('restore_user_missing_capability', $a);
102 case backup
::MODE_IMPORT
:
103 if (!has_capability('moodle/restore:restoretargetimport', $coursectx, $userid)) {
105 $a->userid
= $userid;
106 $a->courseid
= $courseid;
107 $a->capability
= 'moodle/restore:restoretargetimport';
108 throw new restore_controller_exception('restore_user_missing_capability', $a);
111 // Common backup (general, automated...), let's check all the $typecapstocheck
112 // capability => context pairs
114 foreach ($typecapstocheck as $capability => $context) {
115 if (!has_capability($capability, $context, $userid)) {
117 $a->userid
= $userid;
118 $a->courseid
= $courseid;
119 $a->capability
= $capability;
120 throw new restore_controller_exception('restore_user_missing_capability', $a);
125 // Now, enforce 'moodle/restore:userinfo' to 'users' setting, applying changes if allowed,
126 // else throwing exception
127 $userssetting = $restore_controller->get_plan()->get_setting('users');
128 $prevvalue = $userssetting->get_value();
129 $prevstatus = $userssetting->get_status();
130 $hasusercap = has_capability('moodle/restore:userinfo', $coursectx, $userid);
132 // If setting is enabled but user lacks permission
133 if (!$hasusercap && $prevvalue) { // If user has not the capability and setting is enabled
134 // Now analyse if we are allowed to apply changes or must stop with exception
135 if (!$apply) { // Cannot apply changes, throw exception
137 $a->setting
= 'users';
138 $a->value
= $prevvalue;
139 $a->capability
= 'moodle/restore:userinfo';
140 throw new restore_controller_exception('restore_setting_value_wrong_for_capability', $a);
142 } else { // Can apply changes
143 $userssetting->set_value(false); // Set the value to false
144 $userssetting->set_status(base_setting
::LOCKED_BY_PERMISSION
);// Set the status to locked by perm
148 // Now, if mode is HUB or IMPORT, and still we are including users in restore, turn them off
149 // Defaults processing should have handled this, but we need to be 100% sure
150 if ($mode == backup
::MODE_IMPORT ||
$mode == backup
::MODE_HUB
) {
151 $userssetting = $restore_controller->get_plan()->get_setting('users');
152 if ($userssetting->get_value()) {
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 // Check the user has the ability to configure the restore. If not then we need
159 // to lock all settings by permission so that no changes can be made. This does
160 // not apply to the import facility, where all the activities (picked on backup)
161 // are restored automatically without restore UI
162 if ($mode != backup
::MODE_IMPORT
) {
163 $hasconfigcap = has_capability('moodle/restore:configure', $coursectx, $userid);
164 if (!$hasconfigcap) {
165 $settings = $restore_controller->get_plan()->get_settings();
166 foreach ($settings as $setting) {
167 $setting->set_status(base_setting
::LOCKED_BY_PERMISSION
);
172 // Ensure the user has the rolldates capability. If not we want to lock this
173 // settings so that they cannot change it.
174 $hasrolldatescap = has_capability('moodle/restore:rolldates', $coursectx, $userid);
175 if ($type == backup
::TYPE_1COURSE
&& !$hasrolldatescap) {
176 $datesetting = $restore_controller->get_plan()->get_setting('course_startdate');
178 $datesetting->set_status(base_setting
::LOCKED_BY_PERMISSION
);