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

Commit c70a651

Browse files
authored
Update GroupUsers.php
1 parent c09b1ee commit c70a651

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

Api/Controller/GroupUsers.php

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,71 @@
1111
class GroupUsers extends AbstractController
1212
{
1313
/**
14-
* @api-desc Generates a new linking key for a external account.
14+
* @api-desc Gets users for a group
1515
*
1616
* @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.
17+
* @api-in str $account_type optional account type, defaults to byond
2018
*
21-
* @api-out str $key the linking key generated.
22-
* @api-out str $url the url the end user needs to access to link.
2319
*/
2420

2521
public function actionGet(ParameterBag $params)
2622
{
2723
$this->assertSuperUserKey();
2824
$this->assertApiScope('linking');
2925

26+
$group_ids = $this->filter('groups', 'str');
3027
$account_type = $this->filter('account_type', 'str');
3128
$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');
4029

4130
if (empty($group_ids)) {
4231
return $this->error(\XF::phrase('yg_atleast_one_group'));
4332
}
4433

4534
$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) {
35+
$group_priorities = [];
36+
$response = [];
37+
38+
foreach($group_ids as $group_id) {
39+
$groupfinder = \XF::finder('XF:UserGroup');
40+
$userfinder = \XF::finder('XF:User')->isValidUser();
41+
42+
$group = $groupfinder->where("user_group_id", $group_id)->fetchOne();
43+
if(!$group) {
44+
return $this->error(\XF::phrase('yg_invalid_group'));
45+
}
46+
$group_obj = [
47+
"user_group_id" => $group_id,
48+
"name" => $group->title,
49+
"priority" => $group->display_style_priority
50+
];
51+
52+
$query = $userfinder->where($userfinder->expression('FIND_IN_SET(' . $userfinder->quote($group_id) . ", " . $userfinder->columnSqlName("secondary_group_ids") . ")"));
53+
$users = $query->fetch();
5854
$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;
55+
foreach($users as $user) {
56+
foreach($user->LinkedAccounts as $link) {
57+
if($link->account_type !== $account_type) continue;
58+
$new_users[] = $link->account_id;
7259
}
7360
}
74-
75-
$users = $new_users;
61+
$group_obj["users"] = $new_users;
62+
if (\XF::$debugMode) {
63+
$group_obj["query"] = $query->getQuery();
64+
}
65+
$response[] = $group_obj;
7666
}
67+
68+
usort($response, function ($item1, $item2) {
69+
return $item2["priority"] <=> $item1["priority"];
70+
});
71+
72+
return $this->apiSuccess([
73+
"groups" => $response
74+
]);
75+
76+
7777

78-
return $this->apiSuccess(["users" => $users, "query" => $finder->getQuery()]);
7978
}
8079

8180

82-
}
81+
}

0 commit comments

Comments
 (0)