1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
-
- namespace App\Http\Business;
-
- use App\Http\DataAccess\Models\CommandTypes;
- use App\Http\DBO\CommandTypesDbo;
- use App\Http\DBO\HostsDbo;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\ClientException;
- use Luticate\Auth\Business\LuticatePermissionsBusiness;
- use Luticate\Utils\LuBusiness;
- use App\Http\DataAccess\CommandsDataAccess;
- use App\Http\DBO\CommandsDbo;
- use Luticate\Utils\LuRequest;
-
- class CommandsBusiness extends LuBusiness {
- protected static function getDataAccess()
- {
- return new CommandsDataAccess();
- }
-
- protected static function checkCommand(CommandsDbo $command, $command_id = null)
- {
- $existingCommand = CommandsDataAccess::getByName($command->getName());
- if (!is_null($existingCommand) && $command_id != $existingCommand->getId()) {
- self::badInput("Command name already exists");
- }
- if (is_null($command->getName()) || strlen($command->getName()) == 0) {
- self::badInput("Missing command name");
- }
- HostsBusiness::getById($command->getHostId());
- CommandTypesBusiness::getById($command->getCommandTypeId());
- if (is_null($command->getDescription())) {
- $command->setDescription("");
- }
- if (is_null($command->getData()) || strlen($command->getData()) == 0) {
- $command->setData("{}");
- }
- $json = json_decode($command->getData());
- if (is_null($json)) {
- self::badInput("Command data could not be converted to json");
- }
- }
-
- public static function getAllLight($_user, $page = 0, $perPage = PHP_INT_MAX, $query = "")
- {
- $values = self::getAll($page, $perPage, $query);
- if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_COMMAND_EDIT")->getValue()) {
- return $values;
- }
- return $values->map(function($command)
- {
- $command->setData(null);
- return $command;
- });
- }
-
- public static function getLight($_user, $command_id)
- {
- $command = self::getById($command_id);
- if (LuticatePermissionsBusiness::getEffectivePermission($_user->getId(), "CAMOTION_COMMAND_EDIT")->getValue()) {
- return $command;
- }
- $command->setData(null);
- return $command;
- }
-
- public static function add(CommandsDbo $command)
- {
- self::checkCommand($command);
- return CommandsDataAccess::addId($command);
- }
-
- public static function edit(CommandsDbo $command, $command_id)
- {
- self::getById($command_id);
- self::checkCommand($command, $command_id);
- $command->setId($command_id);
- return CommandsDataAccess::editById($command_id, $command);
- }
-
- public static function exec($command_id)
- {
- /**
- * @var $command CommandsDbo
- * @var $host HostsDbo
- * @var $type CommandTypesDbo
- */
- $command = self::getById($command_id);
- $host = HostsBusiness::getById($command->getHostId());
- $type = CommandTypesBusiness::getById($command->getCommandTypeId());
- return LuRequest::proxy('POST', $host->getUrl() . "/commands/exec", [],
- ["command" => $command->__toString(), "type" => $type->__toString()], ["X-Token" => $host->getToken()]);
- }
- }
|