@@ -214,13 +214,26 @@ public static function create_contacts_parameters() {
214
214
* @since Moodle 2.5
215
215
*/
216
216
public static function create_contacts ($ userids , $ userid = 0 ) {
217
- global $ CFG ;
217
+ global $ CFG , $ USER ;
218
218
219
219
// Check if messaging is enabled.
220
220
if (empty ($ CFG ->messaging )) {
221
221
throw new moodle_exception ('disabled ' , 'message ' );
222
222
}
223
223
224
+ if (empty ($ userid )) {
225
+ $ userid = $ USER ->id ;
226
+ }
227
+
228
+ // Validate context.
229
+ $ context = context_system::instance ();
230
+ self ::validate_context ($ context );
231
+
232
+ $ capability = 'moodle/site:manageallmessaging ' ;
233
+ if (($ USER ->id != $ userid ) && !has_capability ($ capability , $ context )) {
234
+ throw new required_capability_exception ($ context , $ capability , 'nopermissions ' , '' );
235
+ }
236
+
224
237
$ params = array ('userids ' => $ userids , 'userid ' => $ userid );
225
238
$ params = self ::validate_parameters (self ::create_contacts_parameters (), $ params );
226
239
@@ -276,13 +289,26 @@ public static function delete_contacts_parameters() {
276
289
* @since Moodle 2.5
277
290
*/
278
291
public static function delete_contacts ($ userids , $ userid = 0 ) {
279
- global $ CFG ;
292
+ global $ CFG , $ USER ;
280
293
281
294
// Check if messaging is enabled.
282
295
if (empty ($ CFG ->messaging )) {
283
296
throw new moodle_exception ('disabled ' , 'message ' );
284
297
}
285
298
299
+ if (empty ($ userid )) {
300
+ $ userid = $ USER ->id ;
301
+ }
302
+
303
+ // Validate context.
304
+ $ context = context_system::instance ();
305
+ self ::validate_context ($ context );
306
+
307
+ $ capability = 'moodle/site:manageallmessaging ' ;
308
+ if (($ USER ->id != $ userid ) && !has_capability ($ capability , $ context )) {
309
+ throw new required_capability_exception ($ context , $ capability , 'nopermissions ' , '' );
310
+ }
311
+
286
312
$ params = array ('userids ' => $ userids , 'userid ' => $ userid );
287
313
$ params = self ::validate_parameters (self ::delete_contacts_parameters (), $ params );
288
314
@@ -331,13 +357,26 @@ public static function block_contacts_parameters() {
331
357
* @since Moodle 2.5
332
358
*/
333
359
public static function block_contacts ($ userids , $ userid = 0 ) {
334
- global $ CFG ;
360
+ global $ CFG , $ USER ;
335
361
336
362
// Check if messaging is enabled.
337
363
if (empty ($ CFG ->messaging )) {
338
364
throw new moodle_exception ('disabled ' , 'message ' );
339
365
}
340
366
367
+ if (empty ($ userid )) {
368
+ $ userid = $ USER ->id ;
369
+ }
370
+
371
+ // Validate context.
372
+ $ context = context_system::instance ();
373
+ self ::validate_context ($ context );
374
+
375
+ $ capability = 'moodle/site:manageallmessaging ' ;
376
+ if (($ USER ->id != $ userid ) && !has_capability ($ capability , $ context )) {
377
+ throw new required_capability_exception ($ context , $ capability , 'nopermissions ' , '' );
378
+ }
379
+
341
380
$ params = array ('userids ' => $ userids , 'userid ' => $ userid );
342
381
$ params = self ::validate_parameters (self ::block_contacts_parameters (), $ params );
343
382
@@ -393,13 +432,26 @@ public static function unblock_contacts_parameters() {
393
432
* @since Moodle 2.5
394
433
*/
395
434
public static function unblock_contacts ($ userids , $ userid = 0 ) {
396
- global $ CFG ;
435
+ global $ CFG , $ USER ;
397
436
398
437
// Check if messaging is enabled.
399
438
if (empty ($ CFG ->messaging )) {
400
439
throw new moodle_exception ('disabled ' , 'message ' );
401
440
}
402
441
442
+ if (empty ($ userid )) {
443
+ $ userid = $ USER ->id ;
444
+ }
445
+
446
+ // Validate context.
447
+ $ context = context_system::instance ();
448
+ self ::validate_context ($ context );
449
+
450
+ $ capability = 'moodle/site:manageallmessaging ' ;
451
+ if (($ USER ->id != $ userid ) && !has_capability ($ capability , $ context )) {
452
+ throw new required_capability_exception ($ context , $ capability , 'nopermissions ' , '' );
453
+ }
454
+
403
455
$ params = array ('userids ' => $ userids , 'userid ' => $ userid );
404
456
$ params = self ::validate_parameters (self ::unblock_contacts_parameters (), $ params );
405
457
@@ -1679,8 +1731,9 @@ public static function get_blocked_users($userid) {
1679
1731
core_user::require_active_user ($ user );
1680
1732
1681
1733
// Check if we have permissions for retrieve the information.
1682
- if ($ userid != $ USER ->id and !has_capability ('moodle/site:readallmessages ' , $ context )) {
1683
- throw new moodle_exception ('accessdenied ' , 'admin ' );
1734
+ $ capability = 'moodle/site:manageallmessaging ' ;
1735
+ if (($ USER ->id != $ userid ) && !has_capability ($ capability , $ context )) {
1736
+ throw new required_capability_exception ($ context , $ capability , 'nopermissions ' , '' );
1684
1737
}
1685
1738
1686
1739
// Now, we can get safely all the blocked users.
0 commit comments