MediaWiki REL1_31
OATHAuthHooks.php
Go to the documentation of this file.
1<?php
22
34 public static function getOATHUserRepository() {
35 global $wgOATHAuthDatabase;
36
37 static $service = null;
38
39 if ( $service == null ) {
40 $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
41 $service = new OATHUserRepository(
42 $factory->getMainLB( $wgOATHAuthDatabase ),
43 new HashBagOStuff(
44 [
45 'maxKeys' => 5,
46 ]
47 )
48 );
49 }
50
51 return $service;
52 }
53
63 public static function onAuthChangeFormFields(
64 array $requests, array $fieldInfo, array &$formDescriptor, $action
65 ) {
66 if ( isset( $fieldInfo['OATHToken'] ) ) {
67 $formDescriptor['OATHToken'] += [
68 'cssClass' => 'loginText',
69 'id' => 'wpOATHToken',
70 'size' => 20,
71 'autofocus' => true,
72 'persistent' => false,
73 'autocomplete' => false,
74 'spellcheck' => false,
75 ];
76 }
77 return true;
78 }
79
90 public static function onTwoFactorIsEnabled( &$isEnabled ) {
91 global $wgUser;
92
93 $user = self::getOATHUserRepository()->findByUser( $wgUser );
94 if ( $user && $user->getKey() !== null ) {
95 $isEnabled = true;
96 # This two-factor extension is enabled by the user,
97 # we don't need to check others.
98 return false;
99 } else {
100 $isEnabled = false;
101 # This two-factor extension isn't enabled by the user,
102 # but others may be.
103 return true;
104 }
105 }
106
114 public static function onGetPreferences( User $user, array &$preferences ) {
115 $oathUser = self::getOATHUserRepository()->findByUser( $user );
116
117 // If there is no existing key, and the user is not allowed to enable it,
118 // we have nothing to show. (
119 if ( $oathUser->getKey() === null && !$user->isAllowed( 'oathauth-enable' ) ) {
120 return true;
121 }
122
123 $title = SpecialPage::getTitleFor( 'OATH' );
124 $msg = $oathUser->getKey() !== null ? 'oathauth-disable' : 'oathauth-enable';
125
126 $preferences[$msg] = [
127 'type' => 'info',
128 'raw' => 'true',
129 'default' => Linker::link(
130 $title,
131 wfMessage( $msg )->escaped(),
132 [],
133 [ 'returnto' => SpecialPage::getTitleFor( 'Preferences' )->getPrefixedText() ]
134 ),
135 'label-message' => 'oathauth-prefs-label',
136 'section' => 'personal/info', ];
137
138 return true;
139 }
140
145 public static function onLoadExtensionSchemaUpdates( $updater ) {
146 $base = dirname( __DIR__ );
147 switch ( $updater->getDB()->getType() ) {
148 case 'mysql':
149 case 'sqlite':
150 $updater->addExtensionTable( 'oathauth_users', "$base/sql/mysql/tables.sql" );
151 $updater->addExtensionUpdate( [ [ __CLASS__, 'schemaUpdateOldUsersFromInstaller' ] ] );
152 $updater->dropExtensionField(
153 'oathauth_users',
154 'secret_reset',
155 "$base/sql/mysql/patch-remove_reset.sql"
156 );
157 break;
158
159 case 'oracle':
160 $updater->addExtensionTable( 'oathauth_users', "$base/sql/oracle/tables.sql" );
161 break;
162
163 case 'postgres':
164 $updater->addExtensionTable( 'oathauth_users', "$base/sql/postgres/tables.sql" );
165 break;
166 }
167
168 return true;
169 }
170
179 public static function schemaUpdateOldUsersFromInstaller( DatabaseUpdater $updater ) {
180 return self::schemaUpdateOldUsers( $updater->getDB() );
181 }
182
190 public static function schemaUpdateOldUsers( IDatabase $db ) {
191 if ( !$db->fieldExists( 'oathauth_users', 'secret_reset' ) ) {
192 return true;
193 }
194
195 $res = $db->select(
196 'oathauth_users',
197 [ 'id', 'scratch_tokens' ],
198 [ 'is_validated != 0' ],
199 __METHOD__
200 );
201
202 foreach ( $res as $row ) {
203 Wikimedia\suppressWarnings();
204 $scratchTokens = unserialize( base64_decode( $row->scratch_tokens ) );
205 Wikimedia\restoreWarnings();
206 if ( $scratchTokens ) {
207 $db->update(
208 'oathauth_users',
209 [ 'scratch_tokens' => implode( ',', $scratchTokens ) ],
210 [ 'id' => $row->id ],
211 __METHOD__
212 );
213 }
214 }
215
216 // Remove rows from the table where user never completed the setup process
217 $db->delete( 'oathauth_users', [ 'is_validated' => 0 ], __METHOD__ );
218
219 return true;
220 }
221}
unserialize( $serialized)
$wgUser
Definition Setup.php:902
Class for handling database updates.
Simple store for keeping values in an associative array for the current process.
static link( $target, $html=null, $customAttribs=[], $query=[], $options=[])
This function returns an HTML link to the given target.
Definition Linker.php:107
This is a value object for authentication requests.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Hooks for Extension:OATHAuth.
static onTwoFactorIsEnabled(&$isEnabled)
Determine if two-factor authentication is enabled for $wgUser.
static onLoadExtensionSchemaUpdates( $updater)
static getOATHUserRepository()
Get the singleton OATH user repository.
static schemaUpdateOldUsers(IDatabase $db)
Helper function for converting old users to the new schema.
static onAuthChangeFormFields(array $requests, array $fieldInfo, array &$formDescriptor, $action)
static onGetPreferences(User $user, array &$preferences)
Add the necessary user preferences for OATHAuth.
static schemaUpdateOldUsersFromInstaller(DatabaseUpdater $updater)
Helper function for converting old users to the new schema.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
$res
Definition database.txt:21
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead. & $feedLinks hooks can tweak the array to change how login etc forms should look $requests
Definition hooks.txt:304
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
fieldExists( $table, $field, $fname=__METHOD__)
Determines whether a field exists in a table.