| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MediaWikiServices; |
| 4 |
use MediaWiki\Permissions\Authority; |
| 5 |
use MediaWiki\User\User; |
| 6 |
use MediaWiki\User\UserIdentity; |
| 7 |
use MediaWiki\User\UserIdentityValue; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class TestUser { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private $username; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private $password; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private $user; |
| 28 |
|
| 29 |
private function assertNotReal() { |
| 30 |
global $wgDBprefix; |
| 31 |
if ( |
| 32 |
$wgDBprefix !== MediaWikiIntegrationTestCase::DB_PREFIX && |
| 33 |
$wgDBprefix !== ParserTestRunner::DB_PREFIX |
| 34 |
) { |
| 35 |
throw new RuntimeException( "Can't create user on real database" ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
public function __construct( $username, $realname = 'Real Name', |
| 40 |
$email = '[email protected]', $groups = [] |
| 41 |
) { |
| 42 |
$this->assertNotReal(); |
| 43 |
|
| 44 |
$this->username = $username; |
| 45 |
$this->password = 'TestUser'; |
| 46 |
|
| 47 |
$this->user = User::newFromName( $this->username ); |
| 48 |
$this->user->load(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
if ( !$this->user->isRegistered() ) { |
| 55 |
|
| 56 |
$this->user = User::createNew( |
| 57 |
$this->username, [ |
| 58 |
"email" => $email, |
| 59 |
"real_name" => $realname |
| 60 |
] |
| 61 |
); |
| 62 |
|
| 63 |
if ( !$this->user ) { |
| 64 |
throw new RuntimeException( "Error creating TestUser " . $username ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
$this->setPassword( $this->password ); |
| 70 |
$change = $this->setEmail( $email ) || |
| 71 |
$this->setRealName( $realname ); |
| 72 |
|
| 73 |
|
| 74 |
$userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager(); |
| 75 |
$currentGroups = $userGroupManager->getUserGroups( $this->user ); |
| 76 |
$userGroupManager->addUserToMultipleGroups( $this->user, array_diff( $groups, $currentGroups ) ); |
| 77 |
foreach ( array_diff( $currentGroups, $groups ) as $group ) { |
| 78 |
$userGroupManager->removeUserFromGroup( $this->user, $group ); |
| 79 |
} |
| 80 |
if ( $change ) { |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
$this->user->mTouched = ''; |
| 87 |
$this->user->saveSettings(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private function setRealName( $realname ) { |
| 96 |
if ( $this->user->getRealName() !== $realname ) { |
| 97 |
$this->user->setRealName( $realname ); |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private function setEmail( string $email ) { |
| 109 |
if ( $this->user->getEmail() !== $email ) { |
| 110 |
$this->user->setEmail( $email ); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
private function setPassword( $password ) { |
| 121 |
self::setPasswordForUser( $this->user, $password ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public static function setPasswordForUser( User $user, $password ) { |
| 134 |
if ( !$user->getId() ) { |
| 135 |
throw new InvalidArgumentException( "Passed User has not been added to the database yet!" ); |
| 136 |
} |
| 137 |
|
| 138 |
$services = MediaWikiServices::getInstance(); |
| 139 |
|
| 140 |
$dbw = $services->getConnectionProvider()->getPrimaryDatabase(); |
| 141 |
$row = $dbw->newSelectQueryBuilder() |
| 142 |
->select( [ 'user_password' ] ) |
| 143 |
->from( 'user' ) |
| 144 |
->where( [ 'user_id' => $user->getId() ] ) |
| 145 |
->caller( __METHOD__ )->fetchRow(); |
| 146 |
if ( !$row ) { |
| 147 |
throw new RuntimeException( "Passed User has an ID but is not in the database?" ); |
| 148 |
} |
| 149 |
|
| 150 |
$passwordFactory = $services->getPasswordFactory(); |
| 151 |
if ( !$passwordFactory->newFromCiphertext( $row->user_password )->verify( $password ) ) { |
| 152 |
$passwordHash = $passwordFactory->newFromPlaintext( $password ); |
| 153 |
$dbw->newUpdateQueryBuilder() |
| 154 |
->update( 'user' ) |
| 155 |
->set( [ 'user_password' => $passwordHash->toString() ] ) |
| 156 |
->where( [ 'user_id' => $user->getId() ] ) |
| 157 |
->caller( __METHOD__ )->execute(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function getUser() { |
| 166 |
return $this->user; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getAuthority(): Authority { |
| 174 |
return $this->user; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function getUserIdentity(): UserIdentity { |
| 182 |
return new UserIdentityValue( $this->user->getId(), $this->user->getName() ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function getPassword() { |
| 190 |
return $this->password; |
| 191 |
} |
| 192 |
} |