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 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Auth\Business\LuticatePermissionsBusiness;
  9. use Luticate\Utils\LuBusiness;
  10. use App\Http\DataAccess\CommandsDataAccess;
  11. use App\Http\DBO\CommandsDbo;
  12. use Luticate\Utils\LuRequest;
  13. class CommandsBusiness extends LuBusiness {
  14. protected static function getDataAccess()
  15. {
  16. return new CommandsDataAccess();
  17. }
  18. protected static function checkCommand(CommandsDbo $command, $command_id = null)
  19. {
  20. $existingCommand = CommandsDataAccess::getByName($command->getName());
  21. if (!is_null($existingCommand) && $command_id != $existingCommand->getId()) {
  22. self::badInput("Command name already exists");
  23. }
  24. if (is_null($command->getName()) || strlen($command->getName()) == 0) {
  25. self::badInput("Missing command name");
  26. }
  27. HostsBusiness::getById($command->getHostId());
  28. CommandTypesBusiness::getById($command->getCommandTypeId());
  29. if (is_null($command->getDescription())) {
  30. $command->setDescription("");
  31. }
  32. if (is_null($command->getData()) || strlen($command->getData()) == 0) {
  33. $command->setData("{}");
  34. }
  35. $json = json_decode($command->getData());
  36. if (is_null($json)) {
  37. self::badInput("Command data could not be converted to json");
  38. }
  39. }
  40. public static function getAllLight($_user, $page = 0, $perPage = PHP_INT_MAX, $query = "")
  41. {
  42. $values = self::getAll($page, $perPage, $query);
  43. if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_COMMAND_EDIT")->getValue()) {
  44. return $values;
  45. }
  46. return $values->map(function($command)
  47. {
  48. $command->setData(null);
  49. return $command;
  50. });
  51. }
  52. public static function getLight($_user, $command_id)
  53. {
  54. $command = self::getById($command_id);
  55. if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_COMMAND_EDIT")->getValue()) {
  56. return $command;
  57. }
  58. $command->setData(null);
  59. return $command;
  60. }
  61. public static function add(CommandsDbo $command)
  62. {
  63. self::checkCommand($command);
  64. return CommandsDataAccess::addId($command);
  65. }
  66. public static function edit(CommandsDbo $command, $command_id)
  67. {
  68. self::getById($command_id);
  69. self::checkCommand($command, $command_id);
  70. $command->setId($command_id);
  71. return CommandsDataAccess::editById($command_id, $command);
  72. }
  73. public static function exec($command_id)
  74. {
  75. /**
  76. * @var $command CommandsDbo
  77. * @var $host HostsDbo
  78. * @var $type CommandTypesDbo
  79. */
  80. $command = self::getById($command_id);
  81. $host = HostsBusiness::getById($command->getHostId());
  82. $type = CommandTypesBusiness::getById($command->getCommandTypeId());
  83. return LuRequest::proxy('POST', $host->getUrl() . "/commands/exec", [],
  84. ["command" => $command->__toString(), "type" => $type->__toString()], ["X-Token" => $host->getToken()]);
  85. }
  86. }