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+ }
0 commit comments