-
Notifications
You must be signed in to change notification settings - Fork 611
/
Users.php
132 lines (117 loc) · 3.15 KB
/
Users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\fields;
use Craft;
use craft\elements\conditions\ElementCondition;
use craft\elements\db\UserQuery;
use craft\elements\ElementCollection;
use craft\elements\User;
use craft\gql\arguments\elements\User as UserArguments;
use craft\gql\interfaces\elements\User as UserInterface;
use craft\gql\resolvers\elements\User as UserResolver;
use craft\helpers\Gql;
use craft\helpers\Gql as GqlHelper;
use craft\models\GqlSchema;
use craft\services\Gql as GqlService;
use GraphQL\Type\Definition\Type;
/**
* Users represents a Users field.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Users extends BaseRelationField
{
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('app', 'Users');
}
/**
* @inheritdoc
*/
public static function icon(): string
{
return 'user-group';
}
/**
* @inheritdoc
*/
public static function elementType(): string
{
return User::class;
}
/**
* @inheritdoc
*/
public static function defaultSelectionLabel(): string
{
return Craft::t('app', 'Add a user');
}
/**
* @inheritdoc
*/
public static function phpType(): string
{
return sprintf('\\%s|\\%s<\\%s>', UserQuery::class, ElementCollection::class, User::class);
}
/**
* @inheritdoc
*/
public function includeInGqlSchema(GqlSchema $schema): bool
{
return Gql::canQueryUsers($schema);
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getContentGqlType(): Type|array
{
return [
'name' => $this->handle,
'type' => Type::nonNull(Type::listOf(UserInterface::getType())),
'args' => UserArguments::getArguments(),
'resolve' => UserResolver::class . '::resolve',
'complexity' => GqlHelper::relatedArgumentComplexity(GqlService::GRAPHQL_COMPLEXITY_EAGER_LOAD),
];
}
/**
* @inheritdoc
* @since 3.3.0
*/
public function getEagerLoadingGqlConditions(): ?array
{
$allowedEntities = Gql::extractAllowedEntitiesFromSchema();
$userGroupUids = $allowedEntities['usergroups'] ?? [];
if (in_array('everyone', $userGroupUids, false)) {
return [];
}
if (empty($userGroupUids)) {
return null;
}
$userGroupsService = Craft::$app->getUserGroups();
$userGroupIds = array_filter(array_map(function(string $uid) use ($userGroupsService) {
$userGroupsService = $userGroupsService->getGroupByUid($uid);
return $userGroupsService->id ?? null;
}, $userGroupUids));
return [
'groupId' => $userGroupIds,
];
}
/**
* @inheritdoc
*/
protected function createSelectionCondition(): ?ElementCondition
{
$condition = User::createCondition();
$condition->queryParams = ['group', 'groupId'];
return $condition;
}
}