Path: blob/master/src/applications/people/query/PhabricatorPeopleSearchEngine.php
12256 views
<?php12final class PhabricatorPeopleSearchEngine3extends PhabricatorApplicationSearchEngine {45public function getResultTypeDescription() {6return pht('Users');7}89public function getApplicationClassName() {10return 'PhabricatorPeopleApplication';11}1213public function newQuery() {14return id(new PhabricatorPeopleQuery())15->needPrimaryEmail(true)16->needProfileImage(true);17}1819protected function buildCustomSearchFields() {20$fields = array(21id(new PhabricatorSearchStringListField())22->setLabel(pht('Usernames'))23->setKey('usernames')24->setAliases(array('username'))25->setDescription(pht('Find users by exact username.')),26id(new PhabricatorSearchTextField())27->setLabel(pht('Name Contains'))28->setKey('nameLike')29->setDescription(30pht('Find users whose usernames contain a substring.')),31id(new PhabricatorSearchThreeStateField())32->setLabel(pht('Administrators'))33->setKey('isAdmin')34->setOptions(35pht('(Show All)'),36pht('Show Only Administrators'),37pht('Hide Administrators'))38->setDescription(39pht(40'Pass true to find only administrators, or false to omit '.41'administrators.')),42id(new PhabricatorSearchThreeStateField())43->setLabel(pht('Disabled'))44->setKey('isDisabled')45->setOptions(46pht('(Show All)'),47pht('Show Only Disabled Users'),48pht('Hide Disabled Users'))49->setDescription(50pht(51'Pass true to find only disabled users, or false to omit '.52'disabled users.')),53id(new PhabricatorSearchThreeStateField())54->setLabel(pht('Bots'))55->setKey('isBot')56->setAliases(array('isSystemAgent'))57->setOptions(58pht('(Show All)'),59pht('Show Only Bots'),60pht('Hide Bots'))61->setDescription(62pht(63'Pass true to find only bots, or false to omit bots.')),64id(new PhabricatorSearchThreeStateField())65->setLabel(pht('Mailing Lists'))66->setKey('isMailingList')67->setOptions(68pht('(Show All)'),69pht('Show Only Mailing Lists'),70pht('Hide Mailing Lists'))71->setDescription(72pht(73'Pass true to find only mailing lists, or false to omit '.74'mailing lists.')),75id(new PhabricatorSearchThreeStateField())76->setLabel(pht('Needs Approval'))77->setKey('needsApproval')78->setOptions(79pht('(Show All)'),80pht('Show Only Unapproved Users'),81pht('Hide Unapproved Users'))82->setDescription(83pht(84'Pass true to find only users awaiting administrative approval, '.85'or false to omit these users.')),86);8788$viewer = $this->requireViewer();89if ($viewer->getIsAdmin()) {90$fields[] = id(new PhabricatorSearchThreeStateField())91->setLabel(pht('Has MFA'))92->setKey('mfa')93->setOptions(94pht('(Show All)'),95pht('Show Only Users With MFA'),96pht('Hide Users With MFA'))97->setDescription(98pht(99'Pass true to find only users who are enrolled in MFA, or false '.100'to omit these users.'));101}102103$fields[] = id(new PhabricatorSearchDateField())104->setKey('createdStart')105->setLabel(pht('Joined After'))106->setDescription(107pht('Find user accounts created after a given time.'));108109$fields[] = id(new PhabricatorSearchDateField())110->setKey('createdEnd')111->setLabel(pht('Joined Before'))112->setDescription(113pht('Find user accounts created before a given time.'));114115return $fields;116}117118protected function getDefaultFieldOrder() {119return array(120'...',121'createdStart',122'createdEnd',123);124}125126protected function buildQueryFromParameters(array $map) {127$query = $this->newQuery();128129$viewer = $this->requireViewer();130131// If the viewer can't browse the user directory, restrict the query to132// just the user's own profile. This is a little bit silly, but serves to133// restrict users from creating a dashboard panel which essentially just134// contains a user directory anyway.135$can_browse = PhabricatorPolicyFilter::hasCapability(136$viewer,137$this->getApplication(),138PeopleBrowseUserDirectoryCapability::CAPABILITY);139if (!$can_browse) {140$query->withPHIDs(array($viewer->getPHID()));141}142143if ($map['usernames']) {144$query->withUsernames($map['usernames']);145}146147if ($map['nameLike']) {148$query->withNameLike($map['nameLike']);149}150151if ($map['isAdmin'] !== null) {152$query->withIsAdmin($map['isAdmin']);153}154155if ($map['isDisabled'] !== null) {156$query->withIsDisabled($map['isDisabled']);157}158159if ($map['isMailingList'] !== null) {160$query->withIsMailingList($map['isMailingList']);161}162163if ($map['isBot'] !== null) {164$query->withIsSystemAgent($map['isBot']);165}166167if ($map['needsApproval'] !== null) {168$query->withIsApproved(!$map['needsApproval']);169}170171if (idx($map, 'mfa') !== null) {172$viewer = $this->requireViewer();173if (!$viewer->getIsAdmin()) {174throw new PhabricatorSearchConstraintException(175pht(176'The "Has MFA" query constraint may only be used by '.177'administrators, to prevent attackers from using it to target '.178'weak accounts.'));179}180181$query->withIsEnrolledInMultiFactor($map['mfa']);182}183184if ($map['createdStart']) {185$query->withDateCreatedAfter($map['createdStart']);186}187188if ($map['createdEnd']) {189$query->withDateCreatedBefore($map['createdEnd']);190}191192return $query;193}194195protected function getURI($path) {196return '/people/'.$path;197}198199protected function getBuiltinQueryNames() {200$names = array(201'active' => pht('Active'),202'all' => pht('All'),203);204205$viewer = $this->requireViewer();206if ($viewer->getIsAdmin()) {207$names['approval'] = pht('Approval Queue');208}209210return $names;211}212213public function buildSavedQueryFromBuiltin($query_key) {214$query = $this->newSavedQuery();215$query->setQueryKey($query_key);216217switch ($query_key) {218case 'all':219return $query;220case 'active':221return $query222->setParameter('isDisabled', false);223case 'approval':224return $query225->setParameter('needsApproval', true)226->setParameter('isDisabled', false);227}228229return parent::buildSavedQueryFromBuiltin($query_key);230}231232protected function renderResultList(233array $users,234PhabricatorSavedQuery $query,235array $handles) {236237assert_instances_of($users, 'PhabricatorUser');238239$request = $this->getRequest();240$viewer = $this->requireViewer();241242$list = new PHUIObjectItemListView();243244$is_approval = ($query->getQueryKey() == 'approval');245246foreach ($users as $user) {247$primary_email = $user->loadPrimaryEmail();248if ($primary_email && $primary_email->getIsVerified()) {249$email = pht('Verified');250} else {251$email = pht('Unverified');252}253254$item = new PHUIObjectItemView();255$item->setHeader($user->getFullName())256->setHref('/p/'.$user->getUsername().'/')257->addAttribute(phabricator_datetime($user->getDateCreated(), $viewer))258->addAttribute($email)259->setImageURI($user->getProfileImageURI());260261if ($is_approval && $primary_email) {262$item->addAttribute($primary_email->getAddress());263}264265if ($user->getIsDisabled()) {266$item->addIcon('fa-ban', pht('Disabled'));267$item->setDisabled(true);268}269270if (!$is_approval) {271if (!$user->getIsApproved()) {272$item->addIcon('fa-clock-o', pht('Needs Approval'));273}274}275276if ($user->getIsAdmin()) {277$item->addIcon('fa-star', pht('Admin'));278}279280if ($user->getIsSystemAgent()) {281$item->addIcon('fa-desktop', pht('Bot'));282}283284if ($user->getIsMailingList()) {285$item->addIcon('fa-envelope-o', pht('Mailing List'));286}287288if ($viewer->getIsAdmin()) {289if ($user->getIsEnrolledInMultiFactor()) {290$item->addIcon('fa-lock', pht('Has MFA'));291}292}293294if ($viewer->getIsAdmin()) {295$user_id = $user->getID();296if ($is_approval) {297$item->addAction(298id(new PHUIListItemView())299->setIcon('fa-ban')300->setName(pht('Disable'))301->setWorkflow(true)302->setHref($this->getApplicationURI('disapprove/'.$user_id.'/')));303$item->addAction(304id(new PHUIListItemView())305->setIcon('fa-thumbs-o-up')306->setName(pht('Approve'))307->setWorkflow(true)308->setHref($this->getApplicationURI('approve/'.$user_id.'/')));309}310}311312$list->addItem($item);313}314315$result = new PhabricatorApplicationSearchResultView();316$result->setObjectList($list);317$result->setNoDataString(pht('No accounts found.'));318319return $result;320}321322protected function newExportFields() {323return array(324id(new PhabricatorStringExportField())325->setKey('username')326->setLabel(pht('Username')),327id(new PhabricatorStringExportField())328->setKey('realName')329->setLabel(pht('Real Name')),330);331}332333protected function newExportData(array $users) {334$viewer = $this->requireViewer();335336$export = array();337foreach ($users as $user) {338$export[] = array(339'username' => $user->getUsername(),340'realName' => $user->getRealName(),341);342}343344return $export;345}346347}348349350