createAndPromote.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <robchur@gmail.com>
  23. * @author Pablo Castellano <pablo@anche.no>
  24. */
  25. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script to create an account and grant it rights.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class CreateAndPromote extends Maintenance {
  32. private static $permitRoles = [ 'sysop', 'bureaucrat', 'bot' ];
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Create a new user account and/or grant it additional rights' );
  36. $this->addOption(
  37. 'force',
  38. 'If acccount exists already, just grant it rights or change password.'
  39. );
  40. foreach ( self::$permitRoles as $role ) {
  41. $this->addOption( $role, "Add the account to the {$role} group" );
  42. }
  43. $this->addOption(
  44. 'custom-groups',
  45. 'Comma-separated list of groups to add the user to',
  46. false,
  47. true
  48. );
  49. $this->addArg( "username", "Username of new user" );
  50. $this->addArg( "password", "Password to set (not required if --force is used)", false );
  51. }
  52. public function execute() {
  53. $username = $this->getArg( 0 );
  54. $password = $this->getArg( 1 );
  55. $force = $this->hasOption( 'force' );
  56. $inGroups = [];
  57. $user = User::newFromName( $username );
  58. if ( !is_object( $user ) ) {
  59. $this->error( "invalid username.", true );
  60. }
  61. $exists = ( 0 !== $user->idForName() );
  62. if ( $exists && !$force ) {
  63. $this->error( "Account exists. Perhaps you want the --force option?", true );
  64. } elseif ( !$exists && !$password ) {
  65. $this->error( "Argument <password> required!", false );
  66. $this->maybeHelp( true );
  67. } elseif ( $exists ) {
  68. $inGroups = $user->getGroups();
  69. }
  70. $groups = array_filter( self::$permitRoles, [ $this, 'hasOption' ] );
  71. if ( $this->hasOption( 'custom-groups' ) ) {
  72. $allGroups = array_flip( User::getAllGroups() );
  73. $customGroupsText = $this->getOption( 'custom-groups' );
  74. if ( $customGroupsText !== '' ) {
  75. $customGroups = explode( ',', $customGroupsText );
  76. foreach ( $customGroups as $customGroup ) {
  77. if ( isset( $allGroups[$customGroup] ) ) {
  78. $groups[] = trim( $customGroup );
  79. } else {
  80. $this->output( "$customGroup is not a valid group, ignoring!\n" );
  81. }
  82. }
  83. }
  84. }
  85. $promotions = array_diff(
  86. $groups,
  87. $inGroups
  88. );
  89. if ( $exists && !$password && count( $promotions ) === 0 ) {
  90. $this->output( "Account exists and nothing to do.\n" );
  91. return;
  92. } elseif ( count( $promotions ) !== 0 ) {
  93. $promoText = "User:{$username} into " . implode( ', ', $promotions ) . "...\n";
  94. if ( $exists ) {
  95. $this->output( wfWikiID() . ": Promoting $promoText" );
  96. } else {
  97. $this->output( wfWikiID() . ": Creating and promoting $promoText" );
  98. }
  99. }
  100. if ( !$exists ) {
  101. # Insert the account into the database
  102. $user->addToDatabase();
  103. $user->saveSettings();
  104. }
  105. if ( $password ) {
  106. # Try to set the password
  107. try {
  108. $status = $user->changeAuthenticationData( [
  109. 'username' => $user->getName(),
  110. 'password' => $password,
  111. 'retype' => $password,
  112. ] );
  113. if ( !$status->isGood() ) {
  114. throw new PasswordError( $status->getWikiText( null, null, 'en' ) );
  115. }
  116. if ( $exists ) {
  117. $this->output( "Password set.\n" );
  118. $user->saveSettings();
  119. }
  120. } catch ( PasswordError $pwe ) {
  121. $this->error( $pwe->getText(), true );
  122. }
  123. }
  124. # Promote user
  125. array_map( [ $user, 'addGroup' ], $promotions );
  126. if ( !$exists ) {
  127. # Increment site_stats.ss_users
  128. $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
  129. $ssu->doUpdate();
  130. }
  131. $this->output( "done.\n" );
  132. }
  133. }
  134. $maintClass = "CreateAndPromote";
  135. require_once RUN_MAINTENANCE_IF_MAIN;