You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandsBusiness.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Http\Business;
  3. use App\Http\DataAccess\Models\CommandTypes;
  4. use App\Http\DBO\CommandTypesDbo;
  5. use App\Http\DBO\HostsDbo;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\ClientException;
  8. use Luticate\Utils\LuBusiness;
  9. use App\Http\DataAccess\CommandsDataAccess;
  10. use App\Http\DBO\CommandsDbo;
  11. use Luticate\Utils\LuRequest;
  12. class CommandsBusiness extends LuBusiness {
  13. protected static function getDataAccess()
  14. {
  15. return new CommandsDataAccess();
  16. }
  17. protected static function checkCommand(CommandsDbo $command, $command_id = null)
  18. {
  19. $existingCommand = CommandsDataAccess::getByName($command->getName());
  20. if (!is_null($existingCommand) && $command_id != $existingCommand->getId()) {
  21. self::badInput("Command name already exists");
  22. }
  23. if (is_null($command->getName()) || strlen($command->getName()) == 0) {
  24. self::badInput("Missing command name");
  25. }
  26. HostsBusiness::getById($command->getHostId());
  27. CommandTypesBusiness::getById($command->getCommandTypeId());
  28. if (is_null($command->getDescription())) {
  29. $command->setDescription("");
  30. }
  31. if (is_null($command->getData()) || strlen($command->getData()) == 0) {
  32. $command->setData("{}");
  33. }
  34. $json = json_decode($command->getData());
  35. if (is_null($json)) {
  36. self::badInput("Command data could not be converted to json");
  37. }
  38. }
  39. public static function add(CommandsDbo $command)
  40. {
  41. self::checkCommand($command);
  42. return CommandsDataAccess::addId($command);
  43. }
  44. public static function edit(CommandsDbo $command, $command_id)
  45. {
  46. self::getById($command_id);
  47. self::checkCommand($command, $command_id);
  48. $command->setId($command_id);
  49. return CommandsDataAccess::editById($command_id, $command);
  50. }
  51. public static function exec($command_id)
  52. {
  53. /**
  54. * @var $command CommandsDbo
  55. * @var $host HostsDbo
  56. * @var $type CommandTypesDbo
  57. */
  58. $command = self::getById($command_id);
  59. $host = HostsBusiness::getById($command->getHostId());
  60. $type = CommandTypesBusiness::getById($command->getCommandTypeId());
  61. return LuRequest::proxy('POST', $host->getUrl() . "/commands/exec", [],
  62. ["command" => $command->__toString(), "type" => $type->__toString()], ["X-Token" => $host->getToken()]);
  63. }
  64. }