Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]
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 declare(strict_types=1); 18 19 namespace core_reportbuilder; 20 21 use context_system; 22 use core_reportbuilder\local\helpers\audience; 23 use core_reportbuilder\local\models\report; 24 use core_reportbuilder\local\report\base; 25 26 /** 27 * Report permission class 28 * 29 * @package core_reportbuilder 30 * @copyright 2021 Paul Holden <paulh@moodle.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class permission { 34 35 /** 36 * Require given user can view reports list 37 * 38 * @param int|null $userid User ID to check, or the current user if omitted 39 * @throws report_access_exception 40 */ 41 public static function require_can_view_reports_list(?int $userid = null): void { 42 if (!static::can_view_reports_list($userid)) { 43 throw new report_access_exception(); 44 } 45 } 46 47 /** 48 * Whether given user can view reports list 49 * 50 * @param int|null $userid User ID to check, or the current user if omitted 51 * @return bool 52 */ 53 public static function can_view_reports_list(?int $userid = null): bool { 54 global $CFG; 55 56 return !empty($CFG->enablecustomreports) && has_any_capability([ 57 'moodle/reportbuilder:editall', 58 'moodle/reportbuilder:edit', 59 'moodle/reportbuilder:view', 60 ], context_system::instance(), $userid); 61 } 62 63 /** 64 * Require given user can view report 65 * 66 * @param report $report 67 * @param int|null $userid User ID to check, or the current user if omitted 68 * @throws report_access_exception 69 */ 70 public static function require_can_view_report(report $report, ?int $userid = null): void { 71 if (!static::can_view_report($report, $userid)) { 72 throw new report_access_exception('errorreportview'); 73 } 74 } 75 76 /** 77 * Whether given user can view report 78 * 79 * @param report $report 80 * @param int|null $userid User ID to check, or the current user if omitted 81 * @return bool 82 */ 83 public static function can_view_report(report $report, ?int $userid = null): bool { 84 if (!static::can_view_reports_list($userid)) { 85 return false; 86 } 87 88 if (self::can_edit_report($report, $userid)) { 89 return true; 90 } 91 92 $reports = audience::user_reports_list($userid); 93 return in_array($report->get('id'), $reports); 94 } 95 96 /** 97 * Require given user can edit report 98 * 99 * @param report $report 100 * @param int|null $userid User ID to check, or the current user if omitted 101 * @throws report_access_exception 102 */ 103 public static function require_can_edit_report(report $report, ?int $userid = null): void { 104 if (!static::can_edit_report($report, $userid)) { 105 throw new report_access_exception('errorreportedit'); 106 } 107 } 108 109 /** 110 * Whether given user can edit report 111 * 112 * @param report $report 113 * @param int|null $userid User ID to check, or the current user if omitted 114 * @return bool 115 */ 116 public static function can_edit_report(report $report, ?int $userid = null): bool { 117 global $CFG, $USER; 118 119 if (empty($CFG->enablecustomreports)) { 120 return false; 121 } 122 123 // We can only edit custom reports. 124 if ($report->get('type') !== base::TYPE_CUSTOM_REPORT) { 125 return false; 126 } 127 128 // To edit their own reports, users must have either of the 'edit' or 'editall' capabilities. For reports belonging 129 // to other users, they must have the specific 'editall' capability. 130 $userid = $userid ?: (int) $USER->id; 131 if ($report->get('usercreated') === $userid) { 132 return has_any_capability([ 133 'moodle/reportbuilder:edit', 134 'moodle/reportbuilder:editall', 135 ], context_system::instance(), $userid); 136 } else { 137 return has_capability('moodle/reportbuilder:editall', context_system::instance(), $userid); 138 } 139 } 140 141 /** 142 * Whether given user can create a new report 143 * 144 * @param int|null $userid User ID to check, or the current user if omitted 145 * @return bool 146 */ 147 public static function can_create_report(?int $userid = null): bool { 148 global $CFG; 149 150 return !empty($CFG->enablecustomreports) && has_any_capability([ 151 'moodle/reportbuilder:edit', 152 'moodle/reportbuilder:editall', 153 ], context_system::instance(), $userid); 154 } 155 156 /** 157 * Require given user can create a new report 158 * 159 * @param int|null $userid User ID to check, or the current user if omitted 160 * @throws report_access_exception 161 */ 162 public static function require_can_create_report(?int $userid = null): void { 163 if (!static::can_create_report($userid)) { 164 throw new report_access_exception('errorreportcreate'); 165 } 166 } 167 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body