| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Extension\OATHAuth; |
| 20 |
|
| 21 |
use MediaWiki\User\UserIdentity; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class OATHUser { |
| 29 |
private UserIdentity $user; |
| 30 |
private int $centralId; |
| 31 |
|
| 32 |
|
| 33 |
private array $keys = []; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __construct( UserIdentity $user, int $centralId ) { |
| 41 |
$this->user = $user; |
| 42 |
$this->centralId = $centralId; |
| 43 |
} |
| 44 |
|
| 45 |
public function getUser(): UserIdentity { |
| 46 |
return $this->user; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function getCentralId(): int { |
| 53 |
return $this->centralId; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function getIssuer() { |
| 60 |
global $wgSitename, $wgOATHAuthAccountPrefix; |
| 61 |
|
| 62 |
if ( $wgOATHAuthAccountPrefix !== false ) { |
| 63 |
return $wgOATHAuthAccountPrefix; |
| 64 |
} |
| 65 |
return $wgSitename; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function getAccount() { |
| 72 |
return $this->user->getName(); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function getKeys(): array { |
| 81 |
return $this->keys; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getKeysForModule( string $moduleName ): array { |
| 89 |
return array_values( |
| 90 |
array_filter( |
| 91 |
$this->keys, |
| 92 |
static fn ( IAuthKey $key ) => $key->getModule() === $moduleName |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
public function removeKey( IAuthKey $key ) { |
| 98 |
$keyId = $key->getId(); |
| 99 |
$this->keys = array_values( |
| 100 |
array_filter( |
| 101 |
$this->keys, |
| 102 |
static fn ( IAuthKey $key ) => $key->getId() !== $keyId |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function removeKeysForModule( string $moduleName ): void { |
| 111 |
$this->keys = array_values( |
| 112 |
array_filter( |
| 113 |
$this->keys, |
| 114 |
static fn ( IAuthKey $key ) => $key->getModule() !== $moduleName |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function addKey( IAuthKey $key ) { |
| 125 |
$this->keys[] = $key; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function getModule() { |
| 135 |
|
| 136 |
if ( !$this->keys ) { |
| 137 |
return null; |
| 138 |
} |
| 139 |
$key = $this->keys[0]; |
| 140 |
return OATHAuthServices::getInstance()->getModuleRegistry()->getModuleByKey( $key->getModule() ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function isTwoFactorAuthEnabled(): bool { |
| 147 |
return count( $this->getKeys() ) >= 1; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function disable() { |
| 154 |
$this->keys = []; |
| 155 |
} |
| 156 |
} |