Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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