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

Commit 8844e53

Browse files
committed
Add /group/users endpoint
1 parent 8688c35 commit 8844e53

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Api/Controller/GroupUsers.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace YogstationPermissions\Api\Controller;
4+
5+
use XF\Api\Controller\AbstractController;
6+
use XF\Mvc\ParameterBag;
7+
8+
/**
9+
* @api-group GroupUsers
10+
*/
11+
class GroupUsers extends AbstractController
12+
{
13+
/**
14+
* @api-desc Generates a new linking key for a external account.
15+
*
16+
* @api-in <req> str $groups Comma delimited group id's
17+
* @api-in str $account_type Type of linked account to find, use account type "forums" for forums usernames. Defaults to "byond".
18+
* @api-in bool $skip_missing If enabled, skips accounts without a linked account otherwise use forums username instead. Defaults to true.
19+
* @api-in bool $verbose Return full user object, if enabled ignores account_type parameter. Defaults to false.
20+
*
21+
* @api-out str $key the linking key generated.
22+
* @api-out str $url the url the end user needs to access to link.
23+
*/
24+
25+
public function actionGet(ParameterBag $params)
26+
{
27+
$this->assertSuperUserKey();
28+
$this->assertApiScope('linking');
29+
30+
$account_type = $this->filter('account_type', 'str');
31+
$account_type = empty($account_type) ? 'byond' : $account_type;
32+
33+
$skip_missing = $this->filter('skip_missing', 'bool');
34+
$verbose = $this->filter('verbose', 'bool');
35+
36+
$finder = \XF::finder('XF:User');
37+
$secondary_group_column = $finder->columnSqlName('secondary_group_ids');
38+
39+
$group_ids = $this->filter('groups', 'str');
40+
41+
if (empty($group_ids)) {
42+
return $this->error(\XF::phrase('yg_atleast_one_group'));
43+
}
44+
45+
$group_ids = explode(',', $this->filter('groups', 'str'));
46+
47+
$whereOr = [
48+
['user_group_id', '=', $group_ids]
49+
];
50+
51+
foreach ($group_ids as $group) {
52+
$whereOr[] = $finder->expression('FIND_IN_SET(' . $finder->quote($group) . ', ' . $secondary_group_column . ')');
53+
}
54+
55+
$users = $finder->whereOr($whereOr)->fetch();
56+
57+
if(!$verbose) {
58+
$new_users = [];
59+
60+
foreach ($users as $user) {
61+
$found = false;
62+
63+
foreach ($user->LinkedAccounts as $linked_account) {
64+
if($linked_account->account_type == $account_type) {
65+
$new_users[] = $linked_account->account_id;
66+
$found = true;
67+
}
68+
}
69+
70+
if (!$found && !$skip_missing) {
71+
$new_users[] = $user->username;
72+
}
73+
}
74+
75+
$users = $new_users;
76+
}
77+
78+
return $this->apiSuccess(["users" => $users, "query" => $finder->getQuery()]);
79+
}
80+
81+
82+
}

_output/routes/_metadata.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"api_group_.json": {
3+
"hash": "3040a51e8df98a67285c9062453f9dd1"
4+
},
25
"api_groups_.json": {
36
"hash": "cd3636cd893aa6db95981fb5f4cc3548"
47
},

_output/routes/api_group_.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"route_type": "api",
3+
"route_prefix": "group",
4+
"sub_name": "",
5+
"format": "users",
6+
"build_class": "",
7+
"build_method": "",
8+
"controller": "YogstationPermissions:GroupUsers",
9+
"context": "",
10+
"action_prefix": ""
11+
}

0 commit comments

Comments
 (0)