Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fbergkemper committed Jul 14, 2016
1 parent f1b9048 commit ea3716d
Show file tree
Hide file tree
Showing 28 changed files with 1,210 additions and 914 deletions.
14 changes: 9 additions & 5 deletions module/Auth/src/Auth/Controller/AuthController.php
Expand Up @@ -34,7 +34,7 @@
class AuthController extends AbstractActionController
{

protected $director;
protected $bsock = null;

public function indexAction()
{
Expand Down Expand Up @@ -71,11 +71,13 @@ public function loginAction()
$locale = $form->getInputFilter()->getValue('locale');

$config = $this->getServiceLocator()->get('Config');
$this->director = $this->getServiceLocator()->get('director');
$this->director->set_config($config['directors'][$director]);
$this->director->set_user_credentials($username, $password);

if($this->director->auth($username, $password)) {
$this->bsock = $this->getServiceLocator()->get('director');

$this->bsock->set_config($config['directors'][$director]);
$this->bsock->set_user_credentials($username, $password);

if($this->bsock->auth($username, $password)) {

$_SESSION['bareos']['director'] = $director;
$_SESSION['bareos']['username'] = $username;
Expand All @@ -93,7 +95,9 @@ public function loginAction()

} else {

$this->bsock->disconnect();
session_destroy();

$err_msg = "Sorry, can not authenticate. Wrong username and/or password.";

return new ViewModel(
Expand Down
79 changes: 58 additions & 21 deletions module/Client/src/Client/Controller/ClientController.php
Expand Up @@ -32,7 +32,8 @@
class ClientController extends AbstractActionController
{

protected $clientModel;
protected $clientModel = null;
protected $bsock = null;

public function indexAction()
{
Expand All @@ -42,34 +43,41 @@ public function indexAction()
return $this->redirect()->toRoute('auth', array('action' => 'login'), array('query' => array('req' => $this->RequestURIPlugin()->getRequestURI(), 'dird' => $_SESSION['bareos']['director'])));
}

$clients = $this->getClientModel()->getClients();
$result = null;

$action = $this->params()->fromQuery('action');

if(empty($action)) {
return new ViewModel(
array(
'clients' => $clients
)
);
return new ViewModel();
}
elseif($action == "enable") {
$clientname = $this->params()->fromQuery('client');
$result = $this->getClientModel()->enableClient($clientname);

try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->enableClient($this->bsock, $clientname);
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}
return new ViewModel(
array(
'clients' => $clients,
'result' => $result
)
);
}
elseif($action == "disable") {
$clientname = $this->params()->fromQuery('client');
$result = $this->getClientModel()->disableClient($clientname);

try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->disableClient($this->bsock, $clientname);
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}
return new ViewModel(
array(
'clients' => $clients,
'result' => $result
)
);
Expand All @@ -89,7 +97,6 @@ public function detailsAction()
'client' => $this->params()->fromRoute('id')
)
);

}

public function statusAction()
Expand All @@ -100,8 +107,18 @@ public function statusAction()
return $this->redirect()->toRoute('auth', array('action' => 'login'), array('query' => array('req' => $this->RequestURIPlugin()->getRequestURI(), 'dird' => $_SESSION['bareos']['director'])));
}

$result = null;

$clientname = $this->params()->fromQuery('client');
$result = $this->getClientModel()->statusClient($clientname);

try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->statusClient($this->bsock, $clientname);
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}

return new ViewModel(
array(
Expand All @@ -119,20 +136,40 @@ public function getDataAction()
return $this->redirect()->toRoute('auth', array('action' => 'login'), array('query' => array('req' => $this->RequestURIPlugin()->getRequestURI(), 'dird' => $_SESSION['bareos']['director'])));
}

$result = null;

$data = $this->params()->fromQuery('data');
$client = $this->params()->fromQuery('client');

if($data == "all") {
$result = $this->getClientModel()->getClients();
try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->getClients($this->bsock);
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}
}
elseif($data == "details" && isset($client)) {
$result = $this->getClientModel()->getClient($client);
try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->getClient($this->bsock, $client);
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}
}
elseif($data == "backups" && isset($client)) {
$result = $this->getClientModel()->getClientBackups($client, null, 'desc');
}
else {
$result = null;
try {
$this->bsock = $this->getServiceLocator()->get('director');
$result = $this->getClientModel()->getClientBackups($this->bsock, $client, null, 'desc');
$this->bsock->disconnect();
}
catch(Exception $e) {
echo $e->getMessage();
}
}

$response = $this->getResponse();
Expand Down
84 changes: 32 additions & 52 deletions module/Client/src/Client/Model/ClientModel.php
Expand Up @@ -5,7 +5,7 @@
* bareos-webui - Bareos Web-Frontend
*
* @link https://github.com/bareos/bareos-webui for the canonical source repository
* @copyright Copyright (c) 2013-2015 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @copyright Copyright (c) 2013-2016 Bareos GmbH & Co. KG (http://www.bareos.org/)
* @license GNU Affero General Public License (http://www.gnu.org/licenses/)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -25,62 +25,45 @@

namespace Client\Model;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ClientModel implements ServiceLocatorAwareInterface
class ClientModel
{
protected $serviceLocator;
protected $director;

public function __construct()
{
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}

public function getServiceLocator()
public function getClients(&$bsock=null)
{
return $this->serviceLocator;
}

public function getClients()
{
$cmd = 'llist clients';
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 2, null);
$clients = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
return $clients['result']['clients'];
if(isset($bsock)) {
$cmd = 'llist clients';
$result = $bsock->send_command($cmd, 2, null);
$clients = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
return $clients['result']['clients'];
}
else {
throw new \Exception('Missing argument.');
}
}

public function getClient($client=null)
public function getClient(&$bsock=null, $client=null)
{
if(isset($client)) {
if(isset($bsock, $client)) {
$cmd = 'llist client="'.$client.'"';
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 2, null);
$result = $bsock->send_command($cmd, 2, null);
$client = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
return $client['result']['clients'];
}
else {
return false;
throw new \Exception('Missing argument.');
}
}

public function getClientBackups($client=null, $limit=null, $order=null)
public function getClientBackups(&$bsock=null, $client=null, $limit=null, $order=null)
{
if(isset($client)) {
if(isset($bsock, $client)) {
if(isset($limit, $order)) {
$cmd = 'llist backups client="'.$client.'" limit='.$limit.' order='.$order.'';
}
else {
$cmd = 'llist backups client="'.$client.'"';
}
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 2, null);
$result = $bsock->send_command($cmd, 2, null);
if(preg_match("/Select/", $result)) {
return null;
}
Expand All @@ -90,46 +73,43 @@ public function getClientBackups($client=null, $limit=null, $order=null)
}
}
else {
return false;
throw new \Exception('Missing argument.');
}
}

public function statusClient($name=null)
public function statusClient(&$bsock=null, $name=null)
{
if(isset($name)) {
if(isset($bsock, $name)) {
$cmd = 'status client="'.$name;
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 0, null);
$result = $bsock->send_command($cmd, 0, null);
return $result;
}
else {
return false;
throw new \Exception('Missing argument.');
}
}

public function enableClient($name=null)
public function enableClient(&$bsock=null, $name=null)
{
if(isset($name)) {
if(isset($bsock, $name)) {
$cmd = 'enable client="'.$name.'" yes';
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 0, null);
$result = $bsock->send_command($cmd, 0, null);
return $result;
}
else {
return false;
throw new \Exception('Missing argument.');
}
}

public function disableClient($name=null)
public function disableClient(&$bsock=null, $name=null)
{
if(isset($name)) {
if(isset($bsock, $name)) {
$cmd = 'disable client="'.$name.'" yes';
$this->director = $this->getServiceLocator()->get('director');
$result = $this->director->send_command($cmd, 0, null);
$result = $bsock->send_command($cmd, 0, null);
return $result;
}
else {
return false;
throw new \Exception('Missing argument.');
}
}
}

0 comments on commit ea3716d

Please sign in to comment.