Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateSandboxHookHandler.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorSandbox;
5
6use MediaWiki\Api\ApiLogout;
7use MediaWiki\Api\ApiMessage;
8use MediaWiki\Api\ApiOptions;
9use MediaWiki\Api\Hook\ApiCheckCanExecuteHook;
10use MediaWiki\Config\Config;
11use MediaWiki\Permissions\Hook\TitleQuickPermissionsHook;
12use MediaWiki\Permissions\Hook\UserGetRightsHook;
13use MediaWiki\Preferences\Hook\GetPreferencesHook;
14
22 GetPreferencesHook,
23 ApiCheckCanExecuteHook,
24 UserGetRightsHook,
25 TitleQuickPermissionsHook
26{
27 private bool $isTranslateSandboxEnabled;
28 // right-translate-sandboxaction action-translate-sandboxaction
29 private const ALLOWED_RIGHTS = [
30 'editmyoptions',
31 'editmyprivateinfo',
32 'read',
33 'readapi',
34 'translate-sandboxaction',
35 'viewmyprivateinfo',
36 ];
37
38 public function __construct( Config $config ) {
39 $this->isTranslateSandboxEnabled = $config->get( 'TranslateUseSandbox' );
40 }
41
43 public function onUserGetRights( $user, &$rights ): bool {
44 if ( !$this->isTranslateSandboxEnabled ) {
45 return true;
46 }
47
48 if ( !TranslateSandbox::isSandboxed( $user ) ) {
49 return true;
50 }
51
52 $rights = self::ALLOWED_RIGHTS;
53
54 // Do not let other hooks add more actions
55 return false;
56 }
57
59 public function onTitleQuickPermissions( $title, $user, $action, &$errors, $doExpensiveQueries, $short ) {
60 if ( !$this->isTranslateSandboxEnabled ) {
61 return true;
62 }
63
64 if ( !TranslateSandbox::isSandboxed( $user ) ) {
65 return true;
66 }
67
68 if ( !in_array( $action, self::ALLOWED_RIGHTS ) ) {
69 // This is technically redundant (the userGetRights hook above will handle it)
70 // but this displays a clearer error message.
71 $errors = [ 'tsb-other-actions' ];
72 return false;
73 }
74
75 return true;
76 }
77
79 public function onGetPreferences( $user, &$preferences ): void {
80 if ( !$this->isTranslateSandboxEnabled ) {
81 return;
82 }
83
84 $preferences['translate-sandbox'] = $preferences['translate-sandbox-reminders'] =
85 [ 'type' => 'api' ];
86 }
87
92 public function onApiCheckCanExecute( $module, $user, &$message ): bool {
93 if ( !$this->isTranslateSandboxEnabled ) {
94 return true;
95 }
96
97 $inclusionList = [
98 // Obviously this is needed to get out of the sandbox
99 TranslationStashActionApi::class,
100 // Used by UniversalLanguageSelector for example
101 ApiOptions::class,
102 // Allow logging out
103 ApiLogout::class,
104 // Allow marking the welcome notification as read
105 \MediaWiki\Extension\Notifications\Api\ApiEchoMarkRead::class,
106 ];
107
108 if ( TranslateSandbox::isSandboxed( $user ) ) {
109 $class = get_class( $module );
110 if ( $module->isWriteMode() && !in_array( $class, $inclusionList, true ) ) {
111 $message = ApiMessage::create( 'apierror-writeapidenied' );
112 return false;
113 }
114 }
115
116 return true;
117 }
118}
onTitleQuickPermissions( $title, $user, $action, &$errors, $doExpensiveQueries, $short)
@inheritDoc
onApiCheckCanExecute( $module, $user, &$message)
Inclusion listing for certain API modules.
static isSandboxed(User $user)
Shortcut for checking if given user is in the sandbox.