10use MediaWiki\Auth\AuthenticationRequest;
11use MediaWiki\Auth\AuthenticationResponse;
12use MediaWiki\Auth\AuthManager;
16use MediaWiki\MediaWikiServices;
17use Wikimedia\ScopedCallback;
33 public static function addUser( $name, $email, $password ) {
34 $user = User::newFromName( $name,
'creatable' );
37 throw new MWException(
'Invalid user name' );
41 'username' => $user->getName(),
42 'password' => $password,
43 'retype' => $password,
48 $services = MediaWikiServices::getInstance();
50 $permissionManager = $services->getPermissionManager();
51 $creator = TranslateUserManager::getUser();
52 $guard = $permissionManager->addTemporaryUserRights( $creator,
'createaccount' );
54 $authManager = $services->getAuthManager();
55 $reqs = $authManager->getAuthenticationRequests( AuthManager::ACTION_CREATE );
56 $reqs = AuthenticationRequest::loadRequestsFromSubmission( $reqs, $data );
57 $res = $authManager->beginAccountCreation( $creator, $reqs,
'null:' );
59 ScopedCallback::consume( $guard );
61 switch ( $res->status ) {
62 case AuthenticationResponse::PASS:
64 case AuthenticationResponse::FAIL:
68 $reason = $res->message->inLanguage(
'en' )->useDatabase(
false )->text();
69 throw new MWException(
"Account creation failed: $reason" );
73 if ( $user->getId() ) {
74 self::deleteUser( $user,
'force' );
77 throw new MWException(
78 'AuthManager does not support such simplified account creation'
83 $services->getUserGroupManager()->addUserToGroup( $user,
'translate-sandboxed' );
95 public static function deleteUser( User $user, $force =
'' ) {
96 $uid = $user->getId();
97 $actorId = $user->getActorId();
99 if ( $force !==
'force' && !self::isSandboxed( $user ) ) {
100 throw new MWException(
'Not a sandboxed user' );
104 $dbw = wfGetDB( DB_PRIMARY );
105 $dbw->delete(
'user', [
'user_id' => $uid ], __METHOD__ );
106 $dbw->delete(
'user_groups', [
'ug_user' => $uid ], __METHOD__ );
107 $dbw->delete(
'user_properties', [
'up_user' => $uid ], __METHOD__ );
109 MediaWikiServices::getInstance()->getActorStore()->deleteActor( $user, $dbw );
112 $dbw->delete(
'logging', [
'log_actor' => $actorId ], __METHOD__ );
113 $dbw->delete(
'recentchanges', [
'rc_actor' => $actorId ], __METHOD__ );
116 $statsUpdate = SiteStatsUpdate::factory( [
'users' => -1 ] );
117 $statsUpdate->doUpdate();
121 $user->clearInstanceCache(
'defaults' );
125 $user->invalidateCache();
133 $dbw = Utilities::getSafeReadDB();
134 $userQuery = User::getQueryInfo();
135 $tables = array_merge( $userQuery[
'tables'], [
'user_groups' ] );
136 $fields = $userQuery[
'fields'];
138 'ug_group' =>
'translate-sandboxed',
141 'user_groups' => [
'JOIN',
'ug_user = user_id' ],
142 ] + $userQuery[
'joins'];
144 $res = $dbw->select( $tables, $fields, $conds, __METHOD__, [], $joins );
146 return UserArray::newFromResult( $res );
155 global $wgTranslateSandboxPromotedGroup;
157 if ( !self::isSandboxed( $user ) ) {
158 throw new MWException(
'Not a sandboxed user' );
161 $services = MediaWikiServices::getInstance();
163 $userGroupManager = $services->getUserGroupManager();
164 $userGroupManager->removeUserFromGroup( $user,
'translate-sandboxed' );
166 if ( $wgTranslateSandboxPromotedGroup ) {
167 $userGroupManager->addUserToGroup( $user, $wgTranslateSandboxPromotedGroup );
170 $userOptionsManager = $services->getUserOptionsManager();
171 $userOptionsManager->setOption( $user,
'translate-sandbox-reminders',
'' );
172 $userOptionsManager->saveOptions( $user );
183 public static function sendEmail( User $sender, User $target, $type ) {
184 global $wgEmergencyContact;
186 $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
187 $targetLang = $userOptionsLookup->getOption( $target,
'language' );
191 if ( !self::isSandboxed( $target ) ) {
192 throw new MWException(
'Not a sandboxed user' );
195 $subjectMsg =
'tsb-reminder-title-generic';
196 $bodyMsg =
'tsb-reminder-content-generic';
197 $targetSpecialPage =
'TranslationStash';
201 $subjectMsg =
'tsb-email-promoted-subject';
202 $bodyMsg =
'tsb-email-promoted-body';
203 $targetSpecialPage =
'Translate';
207 $subjectMsg =
'tsb-email-rejected-subject';
208 $bodyMsg =
'tsb-email-rejected-body';
209 $targetSpecialPage =
'TwnMainPage';
213 throw new MWException(
"'$type' is an invalid type of translate sandbox email" );
216 $subject = wfMessage( $subjectMsg )->inLanguage( $targetLang )->text();
220 SpecialPage::getTitleFor( $targetSpecialPage )->getCanonicalURL(),
222 )->inLanguage( $targetLang )->text();
225 'user' => $target->getId(),
226 'to' => MailAddress::newFromUser( $target ),
227 'from' =>
new MailAddress( $wgEmergencyContact ),
228 'replyto' =>
new MailAddress( $wgEmergencyContact ),
231 'emailType' => $type,
234 $services = MediaWikiServices::getInstance();
235 $userOptionsManager = $services->getUserOptionsManager();
237 $reminders = $userOptionsManager->getOption( $target,
'translate-sandbox-reminders' );
238 $reminders = $reminders ? explode(
'|', $reminders ) : [];
239 $reminders[] = wfTimestamp();
241 $userOptionsManager->setOption( $target,
'translate-sandbox-reminders', implode(
'|', $reminders ) );
242 $userOptionsManager->saveOptions( $target );
254 $userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager();
255 return in_array(
'translate-sandboxed', $userGroupManager->getUserGroups( $user ),
true );
265 global $wgTranslateUseSandbox;
267 if ( !$wgTranslateUseSandbox ) {
271 if ( !self::isSandboxed( $user ) ) {
281 'translate-sandboxaction',
292 $preferences[
'translate-sandbox'] = $preferences[
'translate-sandbox-reminders'] =
309 TranslationStashActionApi::class,
314 if ( self::isSandboxed( $user ) ) {
315 $class = get_class( $module );
316 if ( $module->isWriteMode() && !in_array( $class, $inclusionList,
true ) ) {
317 $message = ApiMessage::create(
'apierror-writeapidenied' );
static newJob(array $params)
Utility class for the sandbox feature of Translate.
static sendEmail(User $sender, User $target, $type)
Sends a reminder to the user.
static addUser( $name, $email, $password)
Adds a new user without doing much validation.
static enforcePermissions(User $user, array &$rights)
Hook: UserGetRights.
static promoteUser(User $user)
Removes the user from the sandbox.
static deleteUser(User $user, $force='')
Deletes a sandboxed user without doing much validation.
static isSandboxed(User $user)
Shortcut for checking if given user is in the sandbox.
static onApiCheckCanExecute(ApiBase $module, User $user, &$message)
Inclusion listing for certain API modules.
static onGetPreferences( $user, &$preferences)
Hook: onGetPreferences.
static getUsers()
Get all sandboxed users.