2014-05-19 00:05:59 +00:00
|
|
|
<?php
|
2018-04-11 01:29:26 +00:00
|
|
|
/**
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
2014-05-19 00:05:59 +00:00
|
|
|
|
2018-05-20 17:44:50 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2018-04-05 09:29:45 +00:00
|
|
|
use Wikimedia\Rdbms\DBConnRef;
|
|
|
|
|
2014-05-19 00:05:59 +00:00
|
|
|
class OATHUserRepository {
|
2018-05-20 17:44:50 +00:00
|
|
|
/** @var ILoadBalancer */
|
2016-05-31 20:24:18 +00:00
|
|
|
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.
|
2018-05-20 17:44:50 +00:00
|
|
|
* @param ILoadBalancer $lb
|
2016-06-15 10:07:01 +00:00
|
|
|
* @param BagOStuff $cache
|
2016-09-30 21:13:57 +00:00
|
|
|
*/
|
2018-05-20 17:44:50 +00:00
|
|
|
public function __construct( ILoadBalancer $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
|
|
|
}
|