mediawiki-extensions-OATHAuth/OATHUserRepository.php
Tyler Anthony Romeo 89455cdfb2 Refactor extension key storage
This takes out the actual key information from
OATHUser and puts it into an OATHKey class, which OATHUser
depends on. This allows easily swapping keys in/out from
a user.

Change-Id: Ife5f1bae4ad65b66c5e20017cc43c0576b4aba19
2016-03-22 18:08:45 -07:00

46 lines
1 KiB
PHP

<?php
class OATHUserRepository {
private $dbr;
private $dbw;
public function __construct( LoadBalancer $lb ) {
$this->dbr = $lb->getConnection( DB_SLAVE );
$this->dbw = $lb->getConnection( DB_MASTER );
}
public function findByUser( User $user ) {
$oathUser = new OATHUser( $user, null );
$res = $this->dbr->selectRow( 'oathauth_users', '*', array( 'id' => $user->getId() ), __METHOD__ );
if ($res) {
$key = new OATHAuthKey( $res->secret, explode( ',', $res->scratch_tokens ) );
$oathUser->setKey( $key );
}
return $oathUser;
}
public function persist( OATHUser $user ) {
$this->dbw->replace(
'oathauth_users',
array( 'id' ),
array(
'id' => $user->getUser()->getId(),
'secret' => $user->getKey()->getSecret(),
'scratch_tokens' => implode( ',', $user->getKey()->getScratchTokens() ),
),
__METHOD__
);
}
public function remove( OATHUser $user ) {
$this->dbw->delete(
'oathauth_users',
array( 'id' => $user->getUser()->getId() ),
__METHOD__
);
}
}