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