-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialCloseAccount.php
168 lines (145 loc) · 4.88 KB
/
SpecialCloseAccount.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* A special page to allow mortals to close their accounts.
* Originally used to be a part of the main EditAccount special page, but a
* rather essential bug prevented this feature from ever working as intended.
* It's easier to have that feature implemented as a special page than fixing
* the broken-by-design logic.
*
* @file
* @date 27 February 2015
* @see https://bugzilla.shoutwiki.com/show_bug.cgi?id=294
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserNameUtils;
// @note Extends EditAccount so that we don't have to duplicate closeAccount() etc.
class CloseAccount extends EditAccount {
/**
* @var null|User User object for the account that is to be disabled
*/
public ?User $mUser;
/**
* @var UserGroupManager
*/
private UserGroupManager $userGroupManager;
/** @var UserNameUtils */
private UserNameUtils $userNameUtils;
/**
* Constructor -- set up the new special page
*
* @param UserGroupManager $userGroupManager
* @param UserNameUtils $userNameUtils
*/
public function __construct(
UserGroupManager $userGroupManager,
UserNameUtils $userNameUtils
) {
SpecialPage::__construct( 'CloseAccount' );
$this->userGroupManager = $userGroupManager;
$this->userNameUtils = $userNameUtils;
}
/**
* Group this special page under the correct header in Special:SpecialPages.
*
* @return string
*/
public function getGroupName(): string {
return 'users';
}
/**
* Special page description shown on Special:SpecialPages (for mortals)
*
* @return string Special page description
*/
public function getDescription(): string {
return $this->msg( 'editaccount-general-description' )->plain();
}
/**
* Show this special page on Special:SpecialPages only for registered users
* who are not staff members
*
* @return bool
*/
public function isListed(): bool {
$user = $this->getUser();
$effectiveGroups = $this->userGroupManager->getUserEffectiveGroups( $user );
$isStaff = in_array( 'staff', $effectiveGroups );
return $user->isRegistered() && !$isStaff;
}
/**
* Show the special page
*
* @param string|null $subPage Parameter (user name) passed to the page or null
*/
public function execute( $subPage ) {
$out = $this->getOutput();
$request = $this->getRequest();
$user = $this->getUser();
// Anons should not be allowed to access this special page
if ( !$user->isRegistered() ) {
throw new PermissionsError( 'editaccount' );
}
// Show a message if the database is in read-only mode
$this->checkReadOnly();
// If user is blocked, s/he doesn't need to access this page
if ( $user->getBlock() ) {
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable
throw new UserBlockedError( $user->getBlock() );
}
// Redirect staff members to Special:EditAccount instead
$effectiveGroups = $this->userGroupManager->getUserEffectiveGroups( $user );
if ( in_array( 'staff', $effectiveGroups ) ) {
$out->redirect( SpecialPage::getTitleFor( 'EditAccount' )->getFullURL() );
}
// Set page title and other stuff
$this->setHeaders();
// Special:EditAccount is a fairly stupid page title
$out->setPageTitle( $this->getDescription() );
// Mortals can only close their own account
$userName = $user->getName();
// Clean up the user name
$userName = str_replace( '_', ' ', trim( $userName ) );
// User names begin with a capital letter
$userName = $this->getLanguage()->ucfirst( $userName );
// Check if user name is an existing user
if ( $this->userNameUtils->isValid( $userName ) ) {
$this->mUser = MediaWikiServices::getInstance()->getUserFactory()->newFromName( $userName );
}
$changeReason = $request->getVal( 'wpReason' );
if ( $request->wasPosted() ) {
$this->mStatus = $this->closeAccount( $changeReason );
if ( $this->mStatus ) {
$color = 'darkgreen';
} else {
$color = '#fe0000';
}
$out->addHTML(
"<fieldset>\n<legend>" . $this->msg( 'editaccount-status' )->escaped() .
'</legend>' .
Xml::element( 'span', [ 'style' => "color: $color; font-weight: bold;" ], $this->mStatusMsg ) .
'</fieldset>'
);
} else {
// Load the correct template file and initiate a new template object
include __DIR__ . '/../../templates/closeaccount.tmpl.php';
$tmpl = new EditAccountCloseAccountTemplate;
$templateVariables = [
// the value of this is irrelevant, it just needs to be defined
// for the template because we're reusing EditAccount's UI template
// and otherwise we'll get "undefined index" notices
'status' => '',
'statusMsg' => '',
// likewise
'user' => $userName,
'user_hsc' => htmlspecialchars( $userName )
];
foreach ( $templateVariables as $templateVariable => $variableValue ) {
$tmpl->set( $templateVariable, $variableValue );
}
// Output everything!
// @phan-suppress-next-line PhanTypeMismatchArgument
$out->addTemplate( $tmpl );
}
}
}