Skip to content

Commit b1431d4

Browse files
Excited-cccclynischayn22
authored andcommitted
Support nested json path
1 parent c02026a commit b1431d4

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

JsonHelper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
class JsonHelper
4+
{
5+
/**
6+
* @param $array "A array where value extracted from"
7+
* @param $path "Path indicates the path to extract value"
8+
* @return mixed "the extracted value, maybe null"
9+
*/
10+
public static function extractValue($array, $path)
11+
{
12+
foreach (explode('.', $path) as $key) {
13+
if (is_null($array)) break;
14+
try {
15+
$array = $array[$key];
16+
} catch (Exception $e) {
17+
$array = null;
18+
}
19+
}
20+
return $array;
21+
}
22+
}

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ $wgOAuth2Client['configuration']['username'] = 'username'; // JSON path to usern
3636
$wgOAuth2Client['configuration']['email'] = 'email'; // JSON path to email
3737
```
3838

39+
The JSON path should be set to point to the appropriate attributes in the JSON.
40+
41+
If the properties you want from your JSON object are nested, you can use periods.
42+
43+
For example, if user JSON is
44+
45+
```
46+
{
47+
"user": {
48+
"username": "my username",
49+
"email": "my email"
50+
}
51+
}
52+
```
53+
54+
Then your JSON path configuration should be these
55+
56+
```
57+
$wgOAuth2Client['configuration']['username'] = 'user.username'; // JSON path to username
58+
$wgOAuth2Client['configuration']['email'] = 'user.email'; // JSON path to email
59+
```
60+
61+
You can see [Json Helper Test case](./tests/phpunit/JsonHelperTest.php) for more.
62+
3963
The **Redirect URI** for your wiki should be:
4064

4165
```

SpecialOAuth2Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
if ( !defined( 'MEDIAWIKI' ) ) {
1818
die( 'This is a MediaWiki extension, and must be run from within MediaWiki.' );
1919
}
20+
require __DIR__.'/JsonHelper.php';
2021

2122
class SpecialOAuth2Client extends SpecialPage {
2223

@@ -142,8 +143,8 @@ private function _default(){
142143
protected function _userHandling( $response ) {
143144
global $wgOAuth2Client, $wgAuth, $wgRequest;
144145

145-
$username = $response['user'][$wgOAuth2Client['configuration']['username']];
146-
$email = $response['user'][$wgOAuth2Client['configuration']['email']];
146+
$username = JsonHelper::extractValue($response, $wgOAuth2Client['configuration']['username']);
147+
$email = JsonHelper::extractValue($response, $wgOAuth2Client['configuration']['email']);
147148

148149
$user = User::newFromName($username, 'creatable');
149150
if (!$user) {

tests/phpunit/JsonHelperTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
require __DIR__ . '/../../JsonHelper.php';
4+
5+
class JsonHelperTest extends MediaWikiTestCase
6+
{
7+
protected function setUp()
8+
{
9+
parent::setUp();
10+
}
11+
12+
public function testExtractValue()
13+
{
14+
$json_array = array(
15+
"user" => array(
16+
"username" => "username",
17+
"email" => "[email protected]"
18+
)
19+
);
20+
self::assertEquals("[email protected]", JsonHelper::extractValue($json_array, "user.email"));
21+
self::assertEquals("username", JsonHelper::extractValue($json_array, "user.username"));
22+
self::assertEquals("username", JsonHelper::extractValue($json_array, "user")["username"]);
23+
self::assertNull(JsonHelper::extractValue($json_array, "user.emails"));
24+
self::assertNull(JsonHelper::extractValue($json_array, "$.user.email"));
25+
}
26+
27+
protected function tearDown()
28+
{
29+
parent::tearDown();
30+
}
31+
}

0 commit comments

Comments
 (0)