예제 #1
0
 /**
  * Export all clients
  */
 public function exportAction()
 {
     $request = $this->getRequest();
     $directory = $request->getParam('directory');
     $validate = $request->getParam('validate') || $request->getParam('v');
     if (!is_dir($directory) or !is_writable($directory)) {
         $model = new \Zend\Mvc\Console\View\ViewModel();
         $model->setErrorLevel(10);
         $model->setResult("Directory '{$directory}' does not exist or is not writable.\n");
         return $model;
     }
     $clients = $this->_clientManager->getClients(null, 'IdString');
     foreach ($clients as $client) {
         $id = $client['IdString'];
         $this->console->writeLine("Exporting {$id}");
         $document = $client->toDomDocument();
         $document->save($directory . '/' . $document->getFilename());
         if ($validate and !$document->isValid()) {
             $model = new \Zend\Mvc\Console\View\ViewModel();
             $model->setErrorLevel(11);
             $model->setResult("Validation failed for {$id}.\n");
             return $model;
         }
     }
 }
예제 #2
0
 /**
  * Show excluded computers
  *
  * @return array sorting, group, computers, order, direction
  */
 public function excludedAction()
 {
     $this->setActiveMenu('Groups');
     $vars['sorting'] = $this->getOrder('InventoryDate', 'desc');
     $vars['group'] = $this->_currentGroup;
     $vars['computers'] = $this->_clientManager->getClients(array('Name', 'UserName', 'InventoryDate'), $vars['sorting']['order'], $vars['sorting']['direction'], 'ExcludedFrom', $this->_currentGroup);
     return $vars;
 }
예제 #3
0
 public function testExportActionValidate()
 {
     $this->_config->expects($this->once())->method('__get')->with('validateXml')->willReturn('1');
     $document = $this->createMock('\\Protocol\\Message\\InventoryRequest');
     $document->expects($this->once())->method('forceValid');
     $client = $this->createMock('Model\\Client\\Client');
     $client->expects($this->once())->method('toDomDocument')->willReturn($document);
     $this->_clientManager->method('getClient')->willReturn($client);
     $this->dispatch('/console/client/export/?id=1');
     $this->assertResponseStatusCode(200);
 }
 public function testExcludedAction()
 {
     $url = '/console/group/excluded/?name=test';
     $group = array('Name' => 'test');
     $clients = array(array('Id' => '1', 'Name' => 'computerName', 'UserName' => 'userName', 'InventoryDate' => new \DateTime('2014-04-09 18:56:12')));
     $this->_groupManager->expects($this->once())->method('getGroup')->with('test')->willReturn($group);
     $this->_clientManager->expects($this->once())->method('getClients')->with(array('Name', 'UserName', 'InventoryDate'), 'InventoryDate', 'desc')->willReturn($clients);
     $this->dispatch($url);
     $this->assertResponseStatusCode(200);
     $this->assertXpathQueryContentContains("//ul[@class='navigation navigation_details']/li[@class='active']/a[@href='{$url}']", 'Ausgeschlossen');
     $this->assertXpathQuery("//p[@class='textcenter'][text()='\nAnzahl Computer: 1\n']");
     $this->assertXpathQuery("//td/a[@href='/console/client/groups/?id=1'][text()='computerName']");
 }
 public function testExportAction()
 {
     $xmlContent = "xml_content\n";
     $document = $this->getMock('\\Protocol\\Message\\InventoryRequest');
     $document->expects($this->once())->method('getFilename')->will($this->returnValue('filename.xml'));
     $document->expects($this->once())->method('saveXml')->will($this->returnValue($xmlContent));
     $client = $this->getMock('Model\\Client\\Client');
     $client->expects($this->once())->method('toDomDocument')->willReturn($document);
     $this->_clientManager->method('getClient')->willReturn($client);
     $this->dispatch('/console/client/export/?id=1');
     $this->assertResponseStatusCode(200);
     $this->assertResponseHeaderContains('Content-Type', 'text/xml; charset=utf-8');
     $this->assertResponseHeaderContains('Content-Disposition', 'attachment; filename="filename.xml"');
     $this->assertResponseHeaderContains('Content-Length', strlen($xmlContent));
     $this->assertEquals($xmlContent, $this->getResponse()->getContent());
 }
예제 #6
0
 /**
  * Delete client, display confirmation form
  *
  * @return array|\Zend\Http\Response [client, form (Console\Form\DeleteClient)] or redirect response
  */
 public function deleteAction()
 {
     $form = $this->_formManager->get('Console\\Form\\DeleteClient');
     if ($this->getRequest()->isPost()) {
         if ($this->params()->fromPost('yes')) {
             $name = $this->_currentClient['Name'];
             try {
                 $this->_clientManager->deleteClient($this->_currentClient, (bool) $this->params()->fromPost('DeleteInterfaces'));
                 $this->flashMessenger()->addSuccessMessage(array($this->_("Client '%s' was successfully deleted.") => $name));
             } catch (\RuntimeException $e) {
                 $this->flashMessenger()->addErrorMessage(array($this->_("Client '%s' could not be deleted.") => $name));
             }
             return $this->redirectToRoute('client', 'index');
         } else {
             return $this->redirectToRoute('client', 'general', array('id' => $this->_currentClient['Id']));
         }
     } else {
         return array('client' => $this->_currentClient, 'form' => $form);
     }
 }
 /**
  * Merge clients
  *
  * This method is used to eliminate duplicates in the database. Based on the
  * last contact, the newest entry is preserved. All older entries are
  * deleted. Some information from the older entries can be preserved on the
  * remaining client.
  *
  * @param integer[] $clients IDs of clients to merge
  * @param bool $mergeCustomFields Preserve custom fields from oldest client
  * @param bool $mergeGroups Preserve manual group assignments from old clients
  * @param bool $mergePackages Preserve package assignments from old clients missing on new client
  * @throws \RuntimeException if an affected client cannot be locked
  */
 public function merge(array $clients, $mergeCustomFields, $mergeGroups, $mergePackages)
 {
     // Remove duplicate IDs
     $clients = array_unique($clients);
     if (count($clients) < 2) {
         return;
         // Nothing to do
     }
     $connection = $this->_clients->getAdapter()->getDriver()->getConnection();
     $connection->beginTransaction();
     try {
         // Lock all given clients and create a list sorted by LastContactDate.
         foreach ($clients as $id) {
             $client = $this->_clientManager->getClient($id);
             if (!$client->lock()) {
                 throw new \RuntimeException("Cannot lock client {$id}");
             }
             $timestamp = $client['LastContactDate']->getTimestamp();
             $list[$timestamp] = $client;
         }
         ksort($list);
         // Now that the list is sorted, renumber the indices
         $clients = array_values($list);
         // Newest client will be the only one not to be deleted, remove it from the list
         $newest = array_pop($clients);
         if ($mergeCustomFields) {
             // Overwrite custom fields with values from oldest client
             $newest->setCustomFields($clients[0]['CustomFields']);
         }
         if ($mergeGroups) {
             // Build list with all manual group assignments from old clients.
             // If more than 1 old client is to be merged and the clients
             // have different assignments for the same group, the result is
             // undefined.
             $groupList = array();
             foreach ($clients as $client) {
                 $groupList += $client->getGroupMemberships(\Model\Client\Client::MEMBERSHIP_MANUAL);
             }
             $newest->setGroupMemberships($groupList);
         }
         if ($mergePackages) {
             // Update the client IDs directly. Assignments from all older
             // clients are merged. Exclude packages that are already assigned.
             $id = $newest['Id'];
             $notIn = $this->_clientConfig->getSql()->select();
             $notIn->columns(array('ivalue'))->where(array('hardware_id' => $id, 'name' => 'DOWNLOAD'));
             foreach ($clients as $client) {
                 $this->_clientConfig->update(array('hardware_id' => $id), array('hardware_id' => $client['Id'], new \Zend\Db\Sql\Predicate\Operator('name', '!=', 'DOWNLOAD_SWITCH'), new \Zend\Db\Sql\Predicate\Like('name', 'DOWNLOAD%'), new \Zend\Db\Sql\Predicate\NotIn('ivalue', $notIn)));
             }
         }
         // Delete all older clients
         foreach ($clients as $client) {
             $this->_clientManager->deleteClient($client, false);
         }
         // Unlock remaining client
         $newest->unlock();
     } catch (\Exception $exception) {
         $connection->rollback();
         throw $exception;
     }
     $connection->commit();
 }