Make UserRightsProxy implement UserIdentity

This is done to allow a more easy transition away from UserRightsProxy.

Bug: T255309
Change-Id: I730d28c12ad389e001f18c12662d220cb74f998e
This commit is contained in:
Alexander Vorwerk 2021-08-24 16:55:38 +02:00
parent 3f12217fe6
commit cdaff4efbc
2 changed files with 69 additions and 19 deletions

View file

@ -20,16 +20,19 @@
* @file
*/
use MediaWiki\DAO\WikiAwareEntityTrait;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\User\UserIdentity;
use Wikimedia\Rdbms\IDatabase;
/**
* Cut-down copy of User interface for local-interwiki-database
* user rights manipulation.
*/
class UserRightsProxy {
class UserRightsProxy implements UserIdentity {
use WikiAwareEntityTrait;
/** @var IDatabase */
private $db;
/** @var string */
@ -169,17 +172,18 @@ class UserRightsProxy {
}
/**
* @param string|false $wikiId
* @return int
*/
public function getId() {
public function getId( $wikiId = self::LOCAL ): int {
return $this->id;
}
/**
* @return bool
*/
public function isAnon() {
return $this->getId() == 0;
public function isAnon(): bool {
return !$this->isRegistered();
}
/**
@ -187,7 +191,7 @@ class UserRightsProxy {
*
* @return string
*/
public function getName() {
public function getName(): string {
return $this->name . '@' . $this->dbDomain;
}
@ -215,12 +219,7 @@ class UserRightsProxy {
* @since 1.29
*/
public function getGroupMemberships() {
// TODO: We are creating an artificial UserIdentity to pass on to the user group manager.
// After all the relevant UserGroupMemberships methods are ported into UserGroupManager,
// the usages of this class will be changed into usages of the UserGroupManager,
// thus the need of this class and the need of this artificial UserIdentityValue will parish.
$user = new UserIdentityValue( $this->getId(), $this->getName() );
return $this->userGroupManager->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST );
return $this->userGroupManager->getUserGroupMemberships( $this, IDBAccessObject::READ_LATEST );
}
/**
@ -232,9 +231,7 @@ class UserRightsProxy {
*/
public function addGroup( $group, $expiry = null ) {
return $this->userGroupManager->addUserToGroup(
// TODO: Artificial UserIdentity just for passing the id and name.
// see comment in getGroupMemberships.
new UserIdentityValue( $this->getId(), $this->getName() ),
$this,
$group,
$expiry,
true
@ -249,9 +246,7 @@ class UserRightsProxy {
*/
public function removeGroup( $group ) {
return $this->userGroupManager->removeUserFromGroup(
// TODO: Artificial UserIdentity just for passing the id and name.
// see comment in getGroupMemberships.
new UserIdentityValue( $this->getId(), $this->getName() ),
$this,
$group
);
}
@ -303,4 +298,31 @@ class UserRightsProxy {
__METHOD__
);
}
/**
* @inheritDoc
*/
public function equals( ?UserIdentity $user ): bool {
if ( !$user ) {
return false;
}
return $this->getName() === $user->getName();
}
/**
* @inheritDoc
*/
public function isRegistered(): bool {
return $this->getId( $this->getWikiId() ) != 0;
}
/**
* Returns the db Domain of the wiki the UserRightsProxy is associated with.
*
* @since 1.37
* @return string
*/
public function getWikiId() {
return $this->dbDomain;
}
}

View file

@ -58,7 +58,7 @@ class UserRightsProxyTest extends MediaWikiIntegrationTestCase {
$id = 12345;
$userRightsProxy = UserRightsProxy::newFromId( 'foowiki', $id );
$this->assertInstanceOf( UserRightsProxy::class, $userRightsProxy );
$this->assertSame( $id, $userRightsProxy->getId() );
$this->assertSame( $id, $userRightsProxy->getId( $userRightsProxy->getWikiId() ) );
}
/**
@ -93,6 +93,34 @@ class UserRightsProxyTest extends MediaWikiIntegrationTestCase {
);
}
/**
* @covers ::equals
*/
public function testEquals() {
$userRightsProxy = UserRightsProxy::newFromName( 'foowiki', 'UserRightsProxyTest' );
$userRightsProxy2 = $this->createMock( UserRightsProxy::class );
$userRightsProxy2->method( 'getName' )->willReturn( 'UserRightsProxyTest@foowiki' );
$this->assertTrue( $userRightsProxy->equals( $userRightsProxy2 ) );
}
/**
* @covers ::getWikiId
*/
public function testGetWikiId() {
$userRightsProxy = UserRightsProxy::newFromName( 'foowiki', 'UserRightsProxyTest' );
$this->assertSame( $userRightsProxy->getWikiId(), 'foowiki' );
}
/**
* @covers ::isAnon
* @covers ::isRegistered
*/
public function testIsRegistered() {
$userRightsProxy = UserRightsProxy::newFromName( 'foowiki', 'UserRightsProxyTest' );
$this->assertTrue( $userRightsProxy->isRegistered() );
$this->assertFalse( $userRightsProxy->isAnon() );
}
/**
* @covers ::addGroup
* @covers ::getGroupMemberships