2014-05-19 00:05:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class OATHUserRepository {
|
2016-05-31 20:24:18 +00:00
|
|
|
/** @var LoadBalancer */
|
|
|
|
protected $lb;
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2016-09-30 21:13:57 +00:00
|
|
|
/**
|
|
|
|
* OATHUserRepository constructor.
|
|
|
|
* @param LoadBalancer $lb
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
public function __construct( LoadBalancer $lb ) {
|
2016-05-31 20:24:18 +00:00
|
|
|
$this->lb = $lb;
|
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 ) {
|
|
|
|
$oathUser = new OATHUser( $user, null );
|
|
|
|
|
2016-04-01 20:45:40 +00:00
|
|
|
$uid = CentralIdLookup::factory()->centralIdFromLocalUser( $user );
|
2016-05-31 20:24:18 +00:00
|
|
|
$res = $this->getDB( DB_SLAVE )
|
2016-09-16 23:18:35 +00:00
|
|
|
->selectRow( 'oathauth_users', '*', [ 'id' => $uid ], __METHOD__ );
|
2016-05-31 20:24:18 +00:00
|
|
|
if ( $res ) {
|
2014-05-19 00:05:59 +00:00
|
|
|
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
|
|
|
|
$oathUser->setKey( $key );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-05-31 20:24:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param integer $index DB_MASTER/DB_SLAVE
|
|
|
|
* @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
|
|
|
}
|