summaryrefslogtreecommitdiff
blob: 1e6d533946ef655f698c14fdc18198c15c9bf1fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
 * @todo This should use FormSpecialPage
 */
class SpecialDisableAccount extends SpecialPage {
	function __construct() {
		parent::__construct( 'DisableAccount', 'disableaccount' );
	}

	public function doesWrites() {
		return true;
	}

	public function execute( $par ) {
		$this->setHeaders();
		$this->checkPermissions();

		$formFields = [
			'account' => [
				'type' => 'text',
				'required' => true,
				'label-message' => 'disableaccount-user',
			],
			'confirm' => [
				'type' => 'toggle',
				'validation-callback' => [ __CLASS__, 'checkConfirmation' ],
				'label-message' => 'disableaccount-confirm',
			],
		];

		$htmlForm = HTMLForm::factory( 'ooui', $formFields, $this->getContext(), 'disableaccount' );

		$htmlForm->setSubmitCallback( [ __CLASS__, 'submit' ] );

		$htmlForm->show();
	}

	static function checkConfirmation( $field, $allFields ) {
		if ( $field ) {
			return true;
		} else {
			return wfMessage( 'disableaccount-mustconfirm' )->parse();
		}
	}

	static function submit( $fields, $form ) {
		global $wgOut;

		// While we're not actually turning the user into a "system" user, it
		// has the same end result: all passwords and other authentication
		// credentials removed or set to something invalid, email blanked,
		// token invalidated, and existing sessions dropped. So let's just use
		// that if possible instead of duplicating all the code.
		if ( is_callable( 'User::newSystemUser' ) ) {
			$user = User::newSystemUser( $fields['account'], [ 'create' => false, 'steal' => true ] );
			if ( !$user ) {
				return wfMessage( 'disableaccount-nosuchuser', $fields['account'] )->text();
			}
		} else {
			$user = User::newFromName( $fields['account'] );

			if ( !$user || $user->getId() === 0 ) {
				return wfMessage( 'disableaccount-nosuchuser', $fields['account'] )->text();
			}

			$user->setPassword( null );
			$user->setEmail( null );
			$user->setToken();
		}

		$user->addGroup( 'inactive' );

		$user->saveSettings();
		$user->invalidateCache();

		$logEntry = new ManualLogEntry( 'block', 'disableaccount' );
		$logEntry->setPerformer( $form->getUser() );
		$logEntry->setTarget( $user->getUserPage() );
		$logEntry->setParameters( [ '4::targetUsername' => $user->getName() ] );
		$logId = $logEntry->insert();
		$logEntry->publish( $logId );

		$wgOut->addWikiMsg( 'disableaccount-success', $user->getName() );

		return true;
	}
}