2014-05-19 00:05:59 +00:00
|
|
|
<?php
|
|
|
|
|
2018-04-05 09:29:45 +00:00
|
|
|
use Wikimedia\Rdbms\LoadBalancer;
|
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
|
|
|
|
2014-05-19 00:05:59 +00:00
|
|
|
class OATHUserRepository {
|
2016-05-31 20:24:18 +00:00
|
|
|
/** @var LoadBalancer */
|
|
|
|
protected $lb;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
/** @var BagOStuff */
|
|
|
|
protected $cache;
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* OATHUserRepository constructor.
|
|
|
|
* @param LoadBalancer $lb
|
2016-06-15 10:07:01 +00:00
|
|
|
* @param BagOStuff $cache
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2016-06-15 10:07:01 +00:00
|
|
|
public function __construct( LoadBalancer $lb, BagOStuff $cache ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->lb = $lb;
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache = $cache;
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 23:18:35 +00:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @return OATHUser
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
public function findByUser( User $user ) {
|
2016-06-15 10:07:01 +00:00
|
|
|
$oathUser = $this->cache->get( $user->getName() );
|
|
|
|
if ( !$oathUser ) {
|
|
|
|
$oathUser = new OATHUser( $user, null );
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
$uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
|
2017-09-24 05:30:19 +00:00
|
|
|
$res = $this->getDB( DB_REPLICA )->selectRow(
|
2016-06-15 10:07:01 +00:00
|
|
|
'oathauth_users',
|
|
|
|
'*',
|
|
|
|
[ 'id' => $uid ],
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
if ( $res ) {
|
|
|
|
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
|
|
|
|
$oathUser->setKey( $key );
|
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache->set( $user->getName(), $oathUser );
|
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
return $oathUser;
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* @param OATHUser $user
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
public function persist( OATHUser $user ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->getDB( DB_MASTER )->replace(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2016-09-16 23:18:35 +00:00
|
|
|
[ 'id' ],
|
|
|
|
[
|
2016-04-01 20:45:40 +00:00
|
|
|
'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ),
|
2014-05-19 00:05:59 +00:00
|
|
|
'secret' => $user->getKey()->getSecret(),
|
|
|
|
'scratch_tokens' => implode( ',', $user->getKey()->getScratchTokens() ),
|
2016-09-16 23:18:35 +00:00
|
|
|
],
|
2014-05-19 00:05:59 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache->set( $user->getUser()->getName(), $user );
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* @param OATHUser $user
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
public function remove( OATHUser $user ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->getDB( DB_MASTER )->delete(
|
2014-05-19 00:05:59 +00:00
|
|
|
'oathauth_users',
|
2016-09-16 23:18:35 +00:00
|
|
|
[ 'id' => CentralIdLookup::factory()->centralIdFromLocalUser( $user->getUser() ) ],
|
2014-05-19 00:05:59 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2016-06-15 10:07:01 +00:00
|
|
|
$this->cache->delete( $user->getUser()->getName() );
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|
2016-05-31 20:24:18 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-15 09:13:34 +00:00
|
|
|
* @param integer $index DB_MASTER/DB_REPLICA
|
2016-05-31 20:24:18 +00:00
|
|
|
* @return DBConnRef
|
|
|
|
*/
|
|
|
|
private function getDB( $index ) {
|
|
|
|
global $wgOATHAuthDatabase;
|
|
|
|
|
2016-09-16 23:18:35 +00:00
|
|
|
return $this->lb->getConnectionRef( $index, [], $wgOATHAuthDatabase );
|
2016-05-31 20:24:18 +00:00
|
|
|
}
|
2014-05-19 00:05:59 +00:00
|
|
|
}
|