123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace Swagger\Client\Radius;
- use Checks;
- use CurlOptions;
- use GuzzleHttp\Client;
- use Silex\Application;
- use Swagger\Client\ApiServices;
- use Swagger\Client\Base\Api\TenancyApi;
- use Swagger\Client\Base\Api\ValidationApi as ValidationApiBase;
- use Swagger\Client\Base\Configuration as ConfigurationBase;
- use Swagger\Client\Radius\Api\AccessApi;
- use Swagger\Client\Radius\Api\ProfileApi;
- use Swagger\Client\Radius\Api\ValidationApi;
- use Swagger\Client\Radius\Model\AccessCRUD;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Throwable;
- use Utils;
- class Services extends ApiServices
- {
- public function getProfileId(Application $app)
- {
- $app->get('/api/v1/radius/profile', function (Application $app, Request $request) {
- $ch = new Checks();
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $apiInstance = new ProfileApi(
- new Client(),
- $config
- );
- try {
- $id = $ch->integer($request, "id");
- $apiInstance->setDisabledTenancy(true);
- $result = $apiInstance->getById($id);
- $apiInstance->setDisabledTenancy(false);
- if ($result) {
- $content = $result->__toString();
- $content = (new CurlOptions())->clearNull($content);
- $codeResponse = 200;
- } else {
- $content = "No se encontro el id = " . $id;
- }
- } catch (Throwable $e) {
- $content = $e->getCode() . " - " . $e->getMessage();
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function getProfiles(Application $app)
- {
- $app->get('/api/v1/radius/profiles', function (Application $app, Request $request) {
- $ch = new Checks();
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $apiInstance = new ProfileApi(
- new Client(),
- $config
- );
- try {
- $page = $ch->integer($request, "page", false);
- $limit = $ch->integer($request, "limit", false);
- $id = $ch->integer($request, "id", false);
- $name = $ch->string($request, "name", false);
- $tenancyId = $ch->integer($request, "tenancyId", false);
- $qb_criteria = $ch->integer($request, "qb-criteria", false, null);
- $content = "[";
- $result = $apiInstance->getList($id, $name, $tenancyId, $qb_criteria, $limit, $page);
- foreach ($result as $v) {
- // se eliminan los null
- $content .= $v->__toString() . ",";
- }
- if (strlen($content) > 1) {
- $content = substr($content, 0, strlen($content) - 1);
- }
- $content .= "]";
- $content = (new CurlOptions())->clearNull($content);
- $codeResponse = 200;
- } catch (Throwable $e) {
- $content = $e->getCode() . " - " . $e->getMessage();
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function getAccessId(Application $app)
- {
- $app->get('/api/v1/radius/access', function (Application $app, Request $request) {
- $ch = new Checks();
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $apiInstance = new AccessApi(
- new Client(),
- $config
- );
- try {
- $id = $ch->integer($request, "id");
- $apiInstance->setDisabledTenancy(true);
- $result = $apiInstance->getById($id);
- $apiInstance->setDisabledTenancy(false);
- if ($result) {
- $content = $result->__toString();
- $content = (new CurlOptions())->clearNull($content);
- $codeResponse = 200;
- } else {
- $content = "No se encontro el id = " . $id;
- }
- } catch (Throwable $e) {
- $content = $e->getCode() . " - " . $e->getMessage();
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function getAccesses(Application $app)
- {
- $app->get('/api/v1/radius/accesses', function (Application $app, Request $request) {
- $ch = new Checks();
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $apiInstance = new AccessApi(
- new Client(),
- $config
- );
- try {
- $page = $ch->integer($request, "page", false);
- $limit = $ch->integer($request, "limit", false);
- $id = $ch->integer($request, "id", false);
- $name = $ch->string($request, "name", false);
- $profile = $ch->integer($request, "profile", false);
- $clientId = $ch->string($request, "clientId", false);
- $tenancyId = $ch->integer($request, "tenancyId", false);
- $qb_criteria = $ch->integer($request, "qb-criteria", false, null);
- $content = "[";
- $result = $apiInstance->getList($id, $name, $profile, $clientId, $tenancyId, $qb_criteria, $limit, $page);
- foreach ($result as $v) {
- // se eliminan los null
- $content .= $v->__toString() . ",";
- }
- if (strlen($content) > 1) {
- $content = substr($content, 0, strlen($content) - 1);
- }
- $content .= "]";
- $content = (new CurlOptions())->clearNull($content);
- $codeResponse = 200;
- } catch (Throwable $e) {
- $content = $e->getCode() . " - " . $e->getMessage();
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function postAccess(Application $app)
- {
- $app->post('/api/v1/radius/access', function (Application $app, Request $request) {
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $configBase = $this->setUserPasswordConfig($request, ConfigurationBase::getDefaultConfiguration());
- $apiInstance = new AccessApi(
- new Client(),
- $config
- );
- $apiValidation = new ValidationApi(
- new Client(),
- $config
- );
- $data = json_decode(file_get_contents('php://input'), true);
- $utils = new Utils();
- $instance = $utils->loadData(new AccessCRUD(), $data);
- $errors = $instance->listInvalidProperties();
- $content = null;
- if (is_null($instance->getName()) || trim($instance->getName()) == "") {
- $content = "Debe cargar el nombre del acceso.";
- } elseif (is_null($instance->getPassword()) || trim($instance->getPassword()) == "") {
- $content = "Debe cargar la contraseña del acceso.";
- } elseif (count($errors) === 0) {
- // chequeo la tenencia
- $apiTenancy = new TenancyApi(
- new Client(),
- $configBase
- );
- // verifico si existe la tenencia
- if (!is_null($instance->getTenancyId()) && !is_null($apiTenancy->getById($instance->getTenancyId()))) {
- $apiValidationBase = new ValidationApiBase(
- new Client(),
- $configBase
- );
- // busco si el externalid ya esta cargado
- if (!is_null($instance->getClientId()) && $instance->getClientId() > 0) {
- // chequeo el cliente exista
- if (!$apiValidationBase->validClient($instance->getClientId())) {
- $content = "El id del cliente no existe.";
- }
- }
- if (is_null($content)) {
- if (is_null($instance->getProfile()) || $instance->getProfile() <= 0) {
- $content = "El id de perfil no existe.";
- } else {
- $result = $apiValidation->validProfile($instance->getProfile());
- if (is_null($result)) {
- $content = "El id de perfil no existe.";
- }
- }
- if (is_null($content)) {
- $apiInstance->create($instance);
- // busco el id del cliente, buscandolo por externalid
- $result = $apiValidation->existsAccess($instance->getName(), $instance->getTenancyId());
- if (!is_null($result)) {
- // busco por external id
- $content = $result->__toString();
- $codeResponse = 200;
- } else {
- $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
- }
- }
- }
- } else {
- $content = "El id de tenencia es incorrecto.";
- }
- } else {
- $content = json_encode($errors);
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function putAccess(Application $app)
- {
- $app->put('/api/v1/radius/access', function (Application $app, Request $request) {
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $configBase = $this->setUserPasswordConfig($request, ConfigurationBase::getDefaultConfiguration());
- $apiInstance = new AccessApi(
- new Client(),
- $config
- );
- $apiValidation = new ValidationApi(
- new Client(),
- $config
- );
- $data = json_decode(file_get_contents('php://input'), true);
- $utils = new Utils();
- $instance = $utils->loadData(new AccessCRUD(), $data);
- $errors = $instance->listInvalidProperties();
- $content = null;
- if (!is_null($instance->getName()) && trim($instance->getName()) == "") {
- $content = "Debe cargar el nombre del acceso.";
- } elseif (!is_null($instance->getPassword()) && trim($instance->getPassword()) == "") {
- $content = "Debe cargar la contraseña del acceso.";
- } elseif (count($errors) === 0) {
- // chequeo la tenencia
- $apiTenancy = new TenancyApi(
- new Client(),
- $configBase
- );
- // verifico si existe la tenencia
- if (!is_null($instance->getTenancyId()) && !is_null($apiTenancy->getById($instance->getTenancyId()))) {
- $apiValidationBase = new ValidationApiBase(
- new Client(),
- $configBase
- );
- // busco si el externalid ya esta cargado
- if (!is_null($instance->getClientId()) && $instance->getClientId() > 0) {
- // chequeo el cliente exista
- if (!$apiValidationBase->validClient($instance->getClientId())) {
- $content = "El id del cliente no existe.";
- }
- }
- if (is_null($content)) {
- if (is_null($instance->getProfile()) || $instance->getProfile() <= 0) {
- $content = "El id de perfil no existe.";
- } else {
- $result = $apiValidation->validProfile($instance->getProfile());
- if (is_null($result)) {
- $content = "El id de perfil no existe.";
- }
- }
- if (is_null($content)) {
- $apiInstance->update($instance->getId(), $instance);
- // busco el id del cliente, buscandolo por externalid
- $result = $apiValidation->validAccess($instance->getId());
- if (!is_null($result)) {
- // busco por external id
- $content = $result->__toString();
- $codeResponse = 200;
- } else {
- $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
- }
- }
- }
- } else {
- $content = "El id de tenencia es incorrecto.";
- }
- } else {
- $content = json_encode($errors);
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- public function deleteAccess(Application $app)
- {
- $app->delete('/api/v1/radius/access', function (Application $app, Request $request) {
- $codeResponse = 400;
- try {
- $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
- $apiInstance = new AccessApi(
- new Client(),
- $config
- );
- $ch = new Checks();
- $id = $ch->integer($request, "id", true);
- $apiInstance->setDisabledTenancy(true);
- $result = $apiInstance->getById($id);
- $apiInstance->setDisabledTenancy(false);
- if (!is_null($result)) {
- $apiInstance->delete($id);
- $content = $result->__toString();
- $codeResponse = 200;
- } else {
- $content = "No se encontro el id = " . $id;
- }
- } catch (Throwable $t) {
- $content = $t->getCode() . " - " . $t->getMessage();
- }
- return new Response($content, $codeResponse);
- });
- }
- }
|