mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/OATHAuth
synced 2024-12-01 03:16:24 +00:00
9cceee17cc
* array() -> [] * spacing fixes * dirname( __FILE__ ) -> __DIR__ * Add phpcs style checks using latest mediawiki-codesniffer to keep things clean. Co-Authored-By: Bryan Davis <bd808@wikimedia.org> Change-Id: I95735f928d3e5d6ac9d2a10d92b40ed01cf2737c
60 lines
1.4 KiB
PHP
60 lines
1.4 KiB
PHP
<?php
|
|
|
|
class OATHUserRepository {
|
|
/** @var LoadBalancer */
|
|
protected $lb;
|
|
|
|
public function __construct( LoadBalancer $lb ) {
|
|
$this->lb = $lb;
|
|
}
|
|
|
|
/**
|
|
* @param User $user
|
|
* @return OATHUser
|
|
*/
|
|
public function findByUser( User $user ) {
|
|
$oathUser = new OATHUser( $user, null );
|
|
|
|
$uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
|
|
$res = $this->getDB( DB_SLAVE )
|
|
->selectRow( 'oathauth_users', '*', [ 'id' => $uid ], __METHOD__ );
|
|
if ( $res ) {
|
|
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
|
|
$oathUser->setKey( $key );
|
|
}
|
|
|
|
return $oathUser;
|
|
}
|
|
|
|
public function persist( OATHUser $user ) {
|
|
$this->getDB( DB_MASTER )->replace(
|
|
'oathauth_users',
|
|
[ 'id' ],
|
|
[
|
|
'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ),
|
|
'secret' => $user->getKey()->getSecret(),
|
|
'scratch_tokens' => implode( ',', $user->getKey()->getScratchTokens() ),
|
|
],
|
|
__METHOD__
|
|
);
|
|
}
|
|
|
|
public function remove( OATHUser $user ) {
|
|
$this->getDB( DB_MASTER )->delete(
|
|
'oathauth_users',
|
|
[ 'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ) ],
|
|
__METHOD__
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param integer $index DB_MASTER/DB_SLAVE
|
|
* @return DBConnRef
|
|
*/
|
|
private function getDB( $index ) {
|
|
global $wgOATHAuthDatabase;
|
|
|
|
return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
|
|
}
|
|
}
|