| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\OATHAuth; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 25 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 26 |
|
| 27 |
class OATHAuthModuleRegistry { |
| 28 |
|
| 29 |
private IConnectionProvider $dbProvider; |
| 30 |
private ObjectFactory $objectFactory; |
| 31 |
|
| 32 |
|
| 33 |
private array $modules; |
| 34 |
|
| 35 |
|
| 36 |
private ?array $moduleIds = null; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
IConnectionProvider $dbProvider, |
| 40 |
ObjectFactory $objectFactory, |
| 41 |
array $modules |
| 42 |
) { |
| 43 |
$this->dbProvider = $dbProvider; |
| 44 |
$this->objectFactory = $objectFactory; |
| 45 |
$this->modules = $modules; |
| 46 |
} |
| 47 |
|
| 48 |
public function moduleExists( string $moduleKey ): bool { |
| 49 |
return isset( $this->getModules()[$moduleKey] ); |
| 50 |
} |
| 51 |
|
| 52 |
public function getModuleByKey( string $key ): IModule { |
| 53 |
if ( !isset( $this->getModules()[$key] ) ) { |
| 54 |
throw new InvalidArgumentException( "No such two-factor module $key" ); |
| 55 |
} |
| 56 |
|
| 57 |
$data = $this->getModules()[$key]; |
| 58 |
if ( is_string( $data ) ) { |
| 59 |
$module = call_user_func_array( $this->getModules()[$key], [] ); |
| 60 |
} else { |
| 61 |
$module = $this->objectFactory->createObject( |
| 62 |
$data, |
| 63 |
[ 'assertClass' => IModule::class ] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
return $module; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getAllModules(): array { |
| 76 |
$modules = []; |
| 77 |
foreach ( $this->getModules() as $key => $callback ) { |
| 78 |
$modules[$key] = $this->getModuleByKey( $key ); |
| 79 |
} |
| 80 |
return $modules; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function getModuleId( string $key ): int { |
| 90 |
$ids = $this->getModuleIds(); |
| 91 |
if ( isset( $ids[$key] ) ) { |
| 92 |
return $ids[$key]; |
| 93 |
} |
| 94 |
|
| 95 |
throw new InvalidArgumentException( "Module $key does not seem to exist" ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public function getModuleIds(): array { |
| 102 |
if ( $this->moduleIds === null ) { |
| 103 |
$this->moduleIds = $this->getModuleIdsFromDatabase( false ); |
| 104 |
} |
| 105 |
|
| 106 |
$missing = array_diff( |
| 107 |
array_keys( $this->getModules() ), |
| 108 |
array_keys( $this->moduleIds ) |
| 109 |
); |
| 110 |
|
| 111 |
if ( $missing ) { |
| 112 |
$insert = $this->dbProvider |
| 113 |
->getPrimaryDatabase( 'virtual-oathauth' ) |
| 114 |
->newInsertQueryBuilder() |
| 115 |
->insertInto( 'oathauth_types' ) |
| 116 |
->caller( __METHOD__ ); |
| 117 |
|
| 118 |
foreach ( $missing as $name ) { |
| 119 |
$insert->row( [ 'oat_name' => $name ] ); |
| 120 |
} |
| 121 |
|
| 122 |
$insert->execute(); |
| 123 |
$this->moduleIds = $this->getModuleIdsFromDatabase( true ); |
| 124 |
} |
| 125 |
|
| 126 |
return $this->moduleIds; |
| 127 |
} |
| 128 |
|
| 129 |
private function getModuleIdsFromDatabase( bool $fromPrimary ): array { |
| 130 |
$ids = []; |
| 131 |
|
| 132 |
if ( $fromPrimary ) { |
| 133 |
$dbr = $this->dbProvider->getPrimaryDatabase( 'virtual-oathauth' ); |
| 134 |
} else { |
| 135 |
$dbr = $this->dbProvider->getReplicaDatabase( 'virtual-oathauth' ); |
| 136 |
} |
| 137 |
|
| 138 |
$rows = $dbr->newSelectQueryBuilder() |
| 139 |
->select( [ 'oat_id', 'oat_name' ] ) |
| 140 |
->from( 'oathauth_types' ) |
| 141 |
->caller( __METHOD__ ) |
| 142 |
->fetchResultSet(); |
| 143 |
|
| 144 |
foreach ( $rows as $row ) { |
| 145 |
$ids[$row->oat_name] = (int)$row->oat_id; |
| 146 |
} |
| 147 |
|
| 148 |
return $ids; |
| 149 |
} |
| 150 |
|
| 151 |
private function getModules(): array { |
| 152 |
return $this->modules; |
| 153 |
} |
| 154 |
} |