Path: blob/master/src/applications/almanac/controller/AlmanacDeviceViewController.php
12262 views
<?php12final class AlmanacDeviceViewController3extends AlmanacDeviceController {45public function shouldAllowPublic() {6return true;7}89public function handleRequest(AphrontRequest $request) {10$viewer = $request->getViewer();1112$name = $request->getURIData('name');1314$device = id(new AlmanacDeviceQuery())15->setViewer($viewer)16->withNames(array($name))17->needProperties(true)18->executeOne();19if (!$device) {20return new Aphront404Response();21}2223$title = pht('Device %s', $device->getName());2425$curtain = $this->buildCurtain($device);2627$header = id(new PHUIHeaderView())28->setUser($viewer)29->setHeader($device->getName())30->setPolicyObject($device)31->setHeaderIcon('fa-server');3233$status = $device->getStatusObject();34if ($status->hasStatusTag()) {35$header->setStatus(36$status->getStatusTagIcon(),37$status->getStatusTagColor(),38$status->getName());39}4041$issue = null;42if ($device->isClusterDevice()) {43$issue = $this->addClusterMessage(44pht('This device is bound to a cluster service.'),45pht(46'This device is bound to a cluster service. You do not have '.47'permission to manage cluster services, so the device can not '.48'be edited.'));49}5051$interfaces = $this->buildInterfaceList($device);5253$crumbs = $this->buildApplicationCrumbs();54$crumbs->addTextCrumb($device->getName());55$crumbs->setBorder(true);5657$timeline = $this->buildTransactionTimeline(58$device,59new AlmanacDeviceTransactionQuery());60$timeline->setShouldTerminate(true);6162$view = id(new PHUITwoColumnView())63->setHeader($header)64->setCurtain($curtain)65->setMainColumn(array(66$issue,67$interfaces,68$this->buildAlmanacPropertiesTable($device),69$this->buildSSHKeysTable($device),70$this->buildServicesTable($device),71$timeline,72));7374return $this->newPage()75->setTitle($title)76->setCrumbs($crumbs)77->appendChild(78array(79$view,80));81}8283private function buildCurtain(AlmanacDevice $device) {84$viewer = $this->getViewer();8586$can_edit = PhabricatorPolicyFilter::hasCapability(87$viewer,88$device,89PhabricatorPolicyCapability::CAN_EDIT);9091$id = $device->getID();92$edit_uri = $this->getApplicationURI("device/edit/{$id}/");9394$curtain = $this->newCurtainView($device);9596$curtain->addAction(97id(new PhabricatorActionView())98->setIcon('fa-pencil')99->setName(pht('Edit Device'))100->setHref($edit_uri)101->setWorkflow(!$can_edit)102->setDisabled(!$can_edit));103104return $curtain;105}106107private function buildInterfaceList(AlmanacDevice $device) {108$viewer = $this->getViewer();109$id = $device->getID();110111$can_edit = PhabricatorPolicyFilter::hasCapability(112$viewer,113$device,114PhabricatorPolicyCapability::CAN_EDIT);115116$interfaces = id(new AlmanacInterfaceQuery())117->setViewer($viewer)118->withDevicePHIDs(array($device->getPHID()))119->execute();120121$table = id(new AlmanacInterfaceTableView())122->setUser($viewer)123->setInterfaces($interfaces)124->setCanEdit($can_edit);125126$header = id(new PHUIHeaderView())127->setHeader(pht('Device Interfaces'))128->addActionLink(129id(new PHUIButtonView())130->setTag('a')131->setHref($this->getApplicationURI("interface/edit/?deviceID={$id}"))132->setWorkflow(!$can_edit)133->setDisabled(!$can_edit)134->setText(pht('Add Interface'))135->setIcon('fa-plus'));136137return id(new PHUIObjectBoxView())138->setHeader($header)139->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)140->setTable($table);141}142143private function buildSSHKeysTable(AlmanacDevice $device) {144$viewer = $this->getViewer();145$id = $device->getID();146$device_phid = $device->getPHID();147148$can_edit = PhabricatorPolicyFilter::hasCapability(149$viewer,150$device,151PhabricatorPolicyCapability::CAN_EDIT);152153$keys = id(new PhabricatorAuthSSHKeyQuery())154->setViewer($viewer)155->withObjectPHIDs(array($device_phid))156->withIsActive(true)157->execute();158159$table = id(new PhabricatorAuthSSHKeyTableView())160->setUser($viewer)161->setKeys($keys)162->setCanEdit($can_edit)163->setShowID(true)164->setShowTrusted(true)165->setNoDataString(pht('This device has no associated SSH public keys.'));166167$menu_button = PhabricatorAuthSSHKeyTableView::newKeyActionsMenu(168$viewer,169$device);170171$header = id(new PHUIHeaderView())172->setHeader(pht('SSH Public Keys'))173->addActionLink($menu_button);174175return id(new PHUIObjectBoxView())176->setHeader($header)177->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)178->setTable($table);179}180181private function buildServicesTable(AlmanacDevice $device) {182$viewer = $this->getViewer();183184// NOTE: We're loading all services so we can show hidden, locked services.185// In general, we let you know about all the things the device is bound to,186// even if you don't have permission to see their details. This is similar187// to exposing the existence of edges in other applications, with the188// addition of always letting you see that locks exist.189190$services = id(new AlmanacServiceQuery())191->setViewer(PhabricatorUser::getOmnipotentUser())192->withDevicePHIDs(array($device->getPHID()))193->execute();194195$handles = $viewer->loadHandles(mpull($services, 'getPHID'));196197$icon_cluster = id(new PHUIIconView())198->setIcon('fa-sitemap');199200$rows = array();201foreach ($services as $service) {202$rows[] = array(203($service->isClusterService()204? $icon_cluster205: null),206$handles->renderHandle($service->getPHID()),207);208}209210$table = id(new AphrontTableView($rows))211->setNoDataString(pht('No services are bound to this device.'))212->setHeaders(213array(214null,215pht('Service'),216))217->setColumnClasses(218array(219null,220'wide pri',221));222223return id(new PHUIObjectBoxView())224->setHeaderText(pht('Bound Services'))225->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)226->setTable($table);227}228229230}231232233