Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit a1eb6de

Browse files
committed
adds linked accounts to API and finishes linked account page.
1 parent 0fbccb5 commit a1eb6de

File tree

12 files changed

+83
-21
lines changed

12 files changed

+83
-21
lines changed

Api/Controller/Linking.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function actionPost(ParameterBag $params)
2525
$this->assertSuperUserKey();
2626
$this->assertApiScope('linking');
2727

28-
$key = $this->service('YogstationPermissions:Linking\Generate', $params->account_type, $params->account_type)->generateKey();
28+
$key = $this->service('YogstationPermissions:Linking\Generate', $params->account_type, $params->account_id)->generateKey();
2929

3030
return $this->apiSuccess([
3131
'key' => $key,
@@ -40,17 +40,17 @@ public function actionGet(ParameterBag $params)
4040

4141
$finder = \XF::finder('YogstationPermissions:LinkedAccount');
4242

43-
$user = $finder->where('account_id', $params->account_type)
43+
$linked_account = $finder->where('account_id', $params->account_type)
4444
->where('account_type', $params->account_type)
4545
->with('User')->fetchOne();
4646

47-
if(!$user)
47+
if(!$linked_account)
4848
{
4949
return $this->error(\XF::phrase('yg_no_linked_account'));
5050
}
5151

5252
return $this->apiSuccess([
53-
'user' => $user
53+
'user' => $linked_account->User
5454
]);
5555
}
5656
}

Entity/LinkedAccount.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static function getStructure(Structure $structure)
1111
{
1212
$structure->table = 'yg_linked_account';
1313
$structure->shortName = 'YG:LinkedAccount';
14-
$structure->primaryKey = 'user_id';
14+
$structure->primaryKey = ['user_id', 'account_type'];
1515
$structure->columns = [
1616
'user_id' => ['type' => self::UINT],
1717
'account_type' => ['type' => self::STR, 'maxLength' => 50],
@@ -28,4 +28,11 @@ public static function getStructure(Structure $structure)
2828

2929
return $structure;
3030
}
31+
32+
protected function setupApiResultData(
33+
\XF\Api\Result\EntityResult $result, $verbosity = self::VERBOSITY_NORMAL, array $options = [])
34+
{
35+
$result->account_type = $this->account_type;
36+
$result->account_id = $this->account_id;
37+
}
3138
}

Listener/User.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ class User
88
{
99
public static function userEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
1010
{
11-
$structure->getters['permissions'] = true;
11+
$structure->getters['permissions'] = true;
12+
13+
$structure->relations['LinkedAccounts'] = [
14+
'entity' => 'YogstationPermissions:LinkedAccount',
15+
'type' => Entity::TO_MANY,
16+
'conditions' => 'user_id',
17+
'primary' => true
18+
];
1219
}
1320
}

Pub/Controller/Linking.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ protected function preDispatchController($action, ParameterBag $params)
1313

1414
public function actionLink(ParameterBag $params) {
1515
$visitor = \XF::visitor();
16+
17+
$linking_key = $this->filter('key', 'str');
1618

1719
$linking_key = \XF::finder('YogstationPermissions:LinkingKey')
18-
->where('linking_key', $params->key)
20+
->where('linking_key', $linking_key)
1921
->fetchOne();
2022

2123
if(!$linking_key) {
@@ -24,10 +26,10 @@ public function actionLink(ParameterBag $params) {
2426

2527
$this->service('YogstationPermissions:Linking\Linking', $visitor->user_id, $linking_key)->link();
2628

27-
return $this->redirect($this->buildLink('linking/success'));
29+
return $this->redirect($this->buildLink('linking/success', null, ["account_type" => $linking_key->account_type]));
2830
}
2931

3032
public function actionSuccess() {
31-
return $this->view('YogstationPermissions:Linking\Success', 'yg_linking_success');
33+
return $this->view('YogstationPermissions:Linking\Success', 'yg_linking_success', ['account_type' => $this->filter('account_type', 'str')]);
3234
}
3335
}

Service/Linking/Linking.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ public function link()
2626

2727
$linked_account->save();
2828

29-
return $key;
29+
return $linked_account;
3030
}
3131

3232
public function removeOldAccounts() {
3333
$finder = \XF::finder('YogstationPermissions:LinkedAccount');
3434

35-
$linked_account = $finder->where('account_id', $params->account_type)
36-
->where('account_type', $params->account_type)
37-
->fetchOne();
35+
$linked_account = $finder->where('account_type', $this->linking_key->account_type)
36+
->whereOr(
37+
['account_id', $this->linking_key->account_type],
38+
['user_id', $this->user_id]
39+
)->fetchOne();
3840

39-
if($user) {
41+
if($linked_account) {
4042
$linked_account->delete();
4143
}
4244
}

XF/Entity/User.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ public function getPermissions() {
1111

1212
$permissions = [];
1313

14-
foreach ($this->getPermissionSet()->getGlobalPerms() as $group => $group_values) {
15-
foreach($group_values as $permission => $value) {
16-
if ($value == true) {
14+
foreach ($this->getPermissionSet()->getGlobalPerms() as $group => $group_values)
15+
{
16+
foreach($group_values as $permission => $value)
17+
{
18+
if ($value == true)
19+
{
1720
$permissions[] = $group . "." . $permission;
18-
} elseif ($value) {
21+
}
22+
elseif ($value)
23+
{
1924
$permissions[] = $group . "." . $permission . '.' . $value;
2025
}
2126
}
@@ -25,11 +30,11 @@ public function getPermissions() {
2530
}
2631

2732
protected function setupApiResultData(
28-
\XF\Api\Result\EntityResult $result, $verbosity = self::VERBOSITY_NORMAL, array $options = []
29-
) {
33+
\XF\Api\Result\EntityResult $result, $verbosity = self::VERBOSITY_NORMAL, array $options = [])
34+
{
3035
parent::setupApiResultData($result, $verbosity, $options);
3136

3237
$result->permissions = $this->getPermissions();
38+
$result->linked_accounts = $this->LinkedAccounts;
3339
}
34-
3540
}

_output/phrases/_metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,11 @@
184184
"version_id": 20201006,
185185
"version_string": "20.20.10 6",
186186
"hash": "df1945eeae99df4ac8e4216678326ebc"
187+
},
188+
"yg_key_not_exist.txt": {
189+
"global_cache": false,
190+
"version_id": 20201006,
191+
"version_string": "20.20.10 6",
192+
"hash": "c8e39ae7d9b2186c9986aecf294ec900"
187193
}
188194
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This linking URL has expired, pleaes generate a new link.
2+
Links expire after 15 minutes.

_output/routes/_metadata.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"api_linking_.json": {
33
"hash": "8746785f7f0cde8a03be2abafa5d6751"
4+
},
5+
"public_linking_.json": {
6+
"hash": "bbcf1941dcc62b76e44af2e896e2bf7d"
47
}
58
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"route_type": "public",
3+
"route_prefix": "linking",
4+
"sub_name": "",
5+
"format": "",
6+
"build_class": "",
7+
"build_method": "",
8+
"controller": "YogstationPermissions:Linking",
9+
"context": "",
10+
"action_prefix": ""
11+
}

0 commit comments

Comments
 (0)