1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Creates an account and grants it rights. |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License along |
16
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
17
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html |
19
|
|
|
* |
20
|
|
|
* @file |
21
|
|
|
* @ingroup Maintenance |
22
|
|
|
* @author Rob Church <[email protected]> |
23
|
|
|
* @author Pablo Castellano <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
require_once __DIR__ . '/Maintenance.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Maintenance script to create an account and grant it rights. |
30
|
|
|
* |
31
|
|
|
* @ingroup Maintenance |
32
|
|
|
*/ |
33
|
|
|
class CreateAndPromote extends Maintenance { |
34
|
|
|
private static $permitRoles = [ 'sysop', 'bureaucrat', 'bot' ]; |
35
|
|
|
|
36
|
|
|
public function __construct() { |
37
|
|
|
parent::__construct(); |
38
|
|
|
$this->addDescription( 'Create a new user account and/or grant it additional rights' ); |
39
|
|
|
$this->addOption( |
40
|
|
|
'force', |
41
|
|
|
'If acccount exists already, just grant it rights or change password.' |
42
|
|
|
); |
43
|
|
|
foreach ( self::$permitRoles as $role ) { |
44
|
|
|
$this->addOption( $role, "Add the account to the {$role} group" ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->addOption( |
48
|
|
|
'custom-groups', |
49
|
|
|
'Comma-separated list of groups to add the user to', |
50
|
|
|
false, |
51
|
|
|
true |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->addArg( "username", "Username of new user" ); |
55
|
|
|
$this->addArg( "password", "Password to set (not required if --force is used)", false ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function execute() { |
59
|
|
|
$username = $this->getArg( 0 ); |
60
|
|
|
$password = $this->getArg( 1 ); |
61
|
|
|
$force = $this->hasOption( 'force' ); |
62
|
|
|
$inGroups = []; |
63
|
|
|
|
64
|
|
|
$user = User::newFromName( $username ); |
65
|
|
|
if ( !is_object( $user ) ) { |
66
|
|
|
$this->error( "invalid username.", true ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$exists = ( 0 !== $user->idForName() ); |
70
|
|
|
|
71
|
|
|
if ( $exists && !$force ) { |
72
|
|
|
$this->error( "Account exists. Perhaps you want the --force option?", true ); |
73
|
|
|
} elseif ( !$exists && !$password ) { |
74
|
|
|
$this->error( "Argument <password> required!", false ); |
75
|
|
|
$this->maybeHelp( true ); |
76
|
|
|
} elseif ( $exists ) { |
77
|
|
|
$inGroups = $user->getGroups(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$groups = array_filter( self::$permitRoles, [ $this, 'hasOption' ] ); |
81
|
|
|
if ( $this->hasOption( 'custom-groups' ) ) { |
82
|
|
|
$allGroups = array_flip( User::getAllGroups() ); |
83
|
|
|
$customGroupsText = $this->getOption( 'custom-groups' ); |
84
|
|
|
if ( $customGroupsText !== '' ) { |
85
|
|
|
$customGroups = explode( ',', $customGroupsText ); |
86
|
|
|
foreach ( $customGroups as $customGroup ) { |
87
|
|
|
if ( isset( $allGroups[$customGroup] ) ) { |
88
|
|
|
$groups[] = trim( $customGroup ); |
89
|
|
|
} else { |
90
|
|
|
$this->output( "$customGroup is not a valid group, ignoring!\n" ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$promotions = array_diff( |
97
|
|
|
$groups, |
98
|
|
|
$inGroups |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if ( $exists && !$password && count( $promotions ) === 0 ) { |
102
|
|
|
$this->output( "Account exists and nothing to do.\n" ); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} elseif ( count( $promotions ) !== 0 ) { |
106
|
|
|
$promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n"; |
107
|
|
|
if ( $exists ) { |
108
|
|
|
$this->output( wfWikiID() . ": Promoting $promoText" ); |
109
|
|
|
} else { |
110
|
|
|
$this->output( wfWikiID() . ": Creating and promoting $promoText" ); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( !$exists ) { |
115
|
|
|
# Insert the account into the database |
116
|
|
|
$user->addToDatabase(); |
117
|
|
|
$user->saveSettings(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ( $password ) { |
121
|
|
|
# Try to set the password |
122
|
|
|
try { |
123
|
|
|
$status = $user->changeAuthenticationData( [ |
124
|
|
|
'username' => $user->getName(), |
125
|
|
|
'password' => $password, |
126
|
|
|
'retype' => $password, |
127
|
|
|
] ); |
128
|
|
|
if ( !$status->isGood() ) { |
129
|
|
|
throw new PasswordError( $status->getWikiText( null, null, 'en' ) ); |
130
|
|
|
} |
131
|
|
|
if ( $exists ) { |
132
|
|
|
$this->output( "Password set.\n" ); |
133
|
|
|
$user->saveSettings(); |
134
|
|
|
} |
135
|
|
|
} catch ( PasswordError $pwe ) { |
136
|
|
|
$this->error( $pwe->getText(), true ); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
# Promote user |
141
|
|
|
array_map( [ $user, 'addGroup' ], $promotions ); |
142
|
|
|
|
143
|
|
|
if ( !$exists ) { |
144
|
|
|
# Increment site_stats.ss_users |
145
|
|
|
$ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); |
146
|
|
|
$ssu->doUpdate(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$this->output( "done.\n" ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$maintClass = "CreateAndPromote"; |
154
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN; |
155
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.