Debian Codegraph @

extensions/OATHAuth/src/OATHUser.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 namespace MediaWiki\Extension\OATHAuth;
20
21 use MediaWiki\User\UserIdentity;
22
23 /**
24 * Class representing a user from OATH's perspective
25 *
26 * @ingroup Extensions
27 */
28 class OATHUser {
29 private UserIdentity $user;
30 private int $centralId;
31
32 /** @var IAuthKey[] */
33 private array $keys = [];
34
35 /**
36 * Constructor. Can't be called directly. Use OATHUserRepository::findByUser instead.
37 * @param UserIdentity $user
38 * @param int $centralId
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 * @return int The central ID of this user
51 */
52 public function getCentralId(): int {
53 return $this->centralId;
54 }
55
56 /**
57 * @return string
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 * @return string
70 */
71 public function getAccount() {
72 return $this->user->getName();
73 }
74
75 /**
76 * Get the key associated with this user.
77 *
78 * @return IAuthKey[]
79 */
80 public function getKeys(): array {
81 return $this->keys;
82 }
83
84 /**
85 * @param string $moduleName As in IModule::getName().
86 * @return IAuthKey[]
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 * @param string $moduleName As in IModule::getName()
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 * Adds single key to the key array
121 *
122 * @param IAuthKey $key
123 */
124 public function addKey( IAuthKey $key ) {
125 $this->keys[] = $key;
126 }
127
128 /**
129 * Gets the module instance associated with this user
130 *
131 * @return IModule|null
132 * @deprecated Use {@link IAuthKey::getModule()} instead
133 */
134 public function getModule() {
135 // wfDeprecated( 'OATHUser::getModule()', '1.44', 'OATHAuth' );
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 * @return bool Whether this user has two-factor authentication enabled or not
145 */
146 public function isTwoFactorAuthEnabled(): bool {
147 return count( $this->getKeys() ) >= 1;
148 }
149
150 /**
151 * Disables current (if any) auth method
152 */
153 public function disable() {
154 $this->keys = [];
155 }
156 }