Skip to content

Commit c58bd95

Browse files
committed
Conduit user.query method
Summary: also includes a PhabricatorPeopleQuery object Test Plan: queried for various combinations username, realname, id, phid, and email. verified correct results. Reviewers: epriestley Reviewed By: epriestley CC: zeeg, aran, Koolvin Maniphest Tasks: T1075 Differential Revision: https://secure.phabricator.com/D2416
1 parent 45e9f8e commit c58bd95

File tree

5 files changed

+241
-0
lines changed

5 files changed

+241
-0
lines changed

src/__phutil_library_map__.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
'ConduitAPI_user_addstatus_Method' => 'applications/conduit/method/user/addstatus',
186186
'ConduitAPI_user_find_Method' => 'applications/conduit/method/user/find',
187187
'ConduitAPI_user_info_Method' => 'applications/conduit/method/user/info',
188+
'ConduitAPI_user_query_Method' => 'applications/conduit/method/user/query',
188189
'ConduitAPI_user_removestatus_Method' => 'applications/conduit/method/user/removestatus',
189190
'ConduitAPI_user_whoami_Method' => 'applications/conduit/method/user/whoami',
190191
'ConduitException' => 'applications/conduit/protocol/exception',
@@ -784,6 +785,7 @@
784785
'PhabricatorPeopleListController' => 'applications/people/controller/list',
785786
'PhabricatorPeopleLogsController' => 'applications/people/controller/logs',
786787
'PhabricatorPeopleProfileController' => 'applications/people/controller/profile',
788+
'PhabricatorPeopleQuery' => 'applications/people/query',
787789
'PhabricatorPolicies' => 'applications/policy/constants/policy',
788790
'PhabricatorPolicyCapability' => 'applications/policy/constants/capability',
789791
'PhabricatorPolicyConstants' => 'applications/policy/constants/base',
@@ -1213,6 +1215,7 @@
12131215
'ConduitAPI_user_addstatus_Method' => 'ConduitAPI_user_Method',
12141216
'ConduitAPI_user_find_Method' => 'ConduitAPI_user_Method',
12151217
'ConduitAPI_user_info_Method' => 'ConduitAPI_user_Method',
1218+
'ConduitAPI_user_query_Method' => 'ConduitAPI_user_Method',
12161219
'ConduitAPI_user_removestatus_Method' => 'ConduitAPI_user_Method',
12171220
'ConduitAPI_user_whoami_Method' => 'ConduitAPI_user_Method',
12181221
'DarkConsoleConfigPlugin' => 'DarkConsolePlugin',
@@ -1689,6 +1692,7 @@
16891692
'PhabricatorPeopleListController' => 'PhabricatorPeopleController',
16901693
'PhabricatorPeopleLogsController' => 'PhabricatorPeopleController',
16911694
'PhabricatorPeopleProfileController' => 'PhabricatorPeopleController',
1695+
'PhabricatorPeopleQuery' => 'PhabricatorOffsetPagedQuery',
16921696
'PhabricatorPolicies' => 'PhabricatorPolicyConstants',
16931697
'PhabricatorPolicyCapability' => 'PhabricatorPolicyConstants',
16941698
'PhabricatorPolicyQuery' => 'PhabricatorQuery',
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2012 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @group conduit
21+
*/
22+
final class ConduitAPI_user_query_Method
23+
extends ConduitAPI_user_Method {
24+
25+
public function getMethodDescription() {
26+
return "Query users.";
27+
}
28+
29+
public function defineParamTypes() {
30+
31+
return array(
32+
'usernames' => 'optional list<string>',
33+
'emails' => 'optional list<string>',
34+
'realnames' => 'optional list<string>',
35+
'phids' => 'optional list<phid>',
36+
'ids' => 'optional list<uint>',
37+
'offset' => 'optional int',
38+
'limit' => 'optional int (default = 100)',
39+
);
40+
41+
}
42+
43+
public function defineReturnType() {
44+
return 'list<dict>';
45+
}
46+
47+
public function defineErrorTypes() {
48+
return array(
49+
'ERR-INVALID-PARAMETER' => 'Missing or malformed parameter.',
50+
);
51+
}
52+
53+
protected function execute(ConduitAPIRequest $request) {
54+
$usernames = $request->getValue('usernames', array());
55+
$emails = $request->getValue('emails', array());
56+
$realnames = $request->getValue('realnames', array());
57+
$phids = $request->getValue('phids', array());
58+
$ids = $request->getValue('ids', array());
59+
$offset = $request->getValue('offset', 0);
60+
$limit = $request->getValue('limit', 100);
61+
62+
$query = new PhabricatorPeopleQuery();
63+
if ($usernames) {
64+
$query->withUsernames($usernames);
65+
}
66+
if ($emails) {
67+
// TODO -- validate emails and maybs
68+
// throw new ConduitException('ERR-INVALID-PARAMETER');
69+
$query->withEmails($emails);
70+
}
71+
if ($realnames) {
72+
$query->withRealnames($realnames);
73+
}
74+
if ($phids) {
75+
$query->withPHIDs($phids);
76+
}
77+
if ($ids) {
78+
$query->withIDs($ids);
79+
}
80+
if ($limit) {
81+
$query->setLimit($limit);
82+
}
83+
if ($offset) {
84+
$query->setOffset($offset);
85+
}
86+
$users = $query->execute();
87+
88+
$results = array();
89+
foreach ($users as $user) {
90+
$results[] = $this->buildUserInformationDictionary($user);
91+
}
92+
return $results;
93+
}
94+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'applications/conduit/method/user/base');
10+
phutil_require_module('phabricator', 'applications/people/query');
11+
12+
13+
phutil_require_source('ConduitAPI_user_query_Method.php');
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2012 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
final class PhabricatorPeopleQuery extends PhabricatorOffsetPagedQuery {
20+
private $usernames;
21+
private $realnames;
22+
private $emails;
23+
private $phids;
24+
private $ids;
25+
26+
public function withIds(array $ids) {
27+
$this->ids = $ids;
28+
return $this;
29+
}
30+
public function withPhids(array $phids) {
31+
$this->phids = $phids;
32+
return $this;
33+
}
34+
public function withEmails(array $emails) {
35+
$this->emails = $emails;
36+
return $this;
37+
}
38+
public function withRealnames(array $realnames) {
39+
$this->realnames = $realnames;
40+
return $this;
41+
}
42+
public function withUsernames(array $usernames) {
43+
$this->usernames = $usernames;
44+
return $this;
45+
}
46+
47+
public function execute() {
48+
$table = new PhabricatorUser();
49+
$conn_r = $table->establishConnection('r');
50+
51+
$joins_clause = $this->buildJoinsClause($conn_r);
52+
$where_clause = $this->buildWhereClause($conn_r);
53+
$limit_clause = $this->buildLimitClause($conn_r);
54+
55+
$data = queryfx_all(
56+
$conn_r,
57+
'SELECT * FROM %T user %Q %Q %Q',
58+
$table->getTableName(),
59+
$joins_clause,
60+
$where_clause,
61+
$limit_clause);
62+
63+
$users = $table->loadAllFromArray($data);
64+
65+
return $users;
66+
}
67+
68+
private function buildJoinsClause($conn_r) {
69+
$joins = array();
70+
71+
if ($this->emails) {
72+
$email_table = new PhabricatorUserEmail();
73+
$joins[] = qsprintf(
74+
$conn_r,
75+
'JOIN %T email ON email.userPHID = user.PHID',
76+
$email_table->getTableName());
77+
}
78+
79+
$joins = implode(' ', $joins);
80+
return $joins;
81+
}
82+
83+
private function buildWhereClause($conn_r) {
84+
$where = array();
85+
86+
if ($this->usernames) {
87+
$where[] = qsprintf($conn_r,
88+
'userName IN (%Ls)',
89+
$this->usernames);
90+
}
91+
if ($this->emails) {
92+
$where[] = qsprintf($conn_r,
93+
'email.address IN (%Ls)',
94+
$this->emails);
95+
}
96+
if ($this->realnames) {
97+
$where[] = qsprintf($conn_r,
98+
'realName IN (%Ls)',
99+
$this->realnames);
100+
}
101+
if ($this->phids) {
102+
$where[] = qsprintf($conn_r,
103+
'phid IN (%Ls)',
104+
$this->phids);
105+
}
106+
if ($this->ids) {
107+
$where[] = qsprintf($conn_r,
108+
'id IN (%Ld)',
109+
$this->ids);
110+
}
111+
112+
return $this->formatWhereClause($where);
113+
}
114+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'applications/people/storage/email');
10+
phutil_require_module('phabricator', 'applications/people/storage/user');
11+
phutil_require_module('phabricator', 'infrastructure/query/offsetpaged');
12+
phutil_require_module('phabricator', 'storage/qsprintf');
13+
phutil_require_module('phabricator', 'storage/queryfx');
14+
15+
16+
phutil_require_source('PhabricatorPeopleQuery.php');

0 commit comments

Comments
 (0)