Services.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace Swagger\Client\Radius;
  3. use Checks;
  4. use CurlOptions;
  5. use GuzzleHttp\Client;
  6. use Silex\Application;
  7. use Swagger\Client\ApiServices;
  8. use Swagger\Client\Base\Api\TenancyApi;
  9. use Swagger\Client\Base\Api\ValidationApi as ValidationApiBase;
  10. use Swagger\Client\Base\Configuration as ConfigurationBase;
  11. use Swagger\Client\Radius\Api\AccessApi;
  12. use Swagger\Client\Radius\Api\ProfileApi;
  13. use Swagger\Client\Radius\Api\ValidationApi;
  14. use Swagger\Client\Radius\Model\AccessCRUD;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Throwable;
  18. use Utils;
  19. class Services extends ApiServices
  20. {
  21. public function getProfileId(Application $app)
  22. {
  23. $app->get('/api/v1/radius/profile', function (Application $app, Request $request) {
  24. $ch = new Checks();
  25. $codeResponse = 400;
  26. try {
  27. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  28. $apiInstance = new ProfileApi(
  29. new Client(),
  30. $config
  31. );
  32. try {
  33. $id = $ch->integer($request, "id");
  34. $apiInstance->setDisabledTenancy(true);
  35. $result = $apiInstance->getById($id);
  36. $apiInstance->setDisabledTenancy(false);
  37. if ($result) {
  38. $content = $result->__toString();
  39. $content = (new CurlOptions())->clearNull($content);
  40. $codeResponse = 200;
  41. } else {
  42. $content = "No se encontro el id = " . $id;
  43. }
  44. } catch (Throwable $e) {
  45. $content = $e->getCode() . " - " . $e->getMessage();
  46. }
  47. } catch (Throwable $t) {
  48. $content = $t->getCode() . " - " . $t->getMessage();
  49. }
  50. return new Response($content, $codeResponse);
  51. });
  52. }
  53. public function getProfiles(Application $app)
  54. {
  55. $app->get('/api/v1/radius/profiles', function (Application $app, Request $request) {
  56. $ch = new Checks();
  57. $codeResponse = 400;
  58. try {
  59. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  60. $apiInstance = new ProfileApi(
  61. new Client(),
  62. $config
  63. );
  64. try {
  65. $page = $ch->integer($request, "page", false);
  66. $limit = $ch->integer($request, "limit", false);
  67. $id = $ch->integer($request, "id", false);
  68. $name = $ch->string($request, "name", false);
  69. $tenancyId = $ch->integer($request, "tenancyId", false);
  70. $qb_criteria = $ch->integer($request, "qb-criteria", false, null);
  71. $content = "[";
  72. $result = $apiInstance->getList($id, $name, $tenancyId, $qb_criteria, $limit, $page);
  73. foreach ($result as $v) {
  74. // se eliminan los null
  75. $content .= $v->__toString() . ",";
  76. }
  77. if (strlen($content) > 1) {
  78. $content = substr($content, 0, strlen($content) - 1);
  79. }
  80. $content .= "]";
  81. $content = (new CurlOptions())->clearNull($content);
  82. $codeResponse = 200;
  83. } catch (Throwable $e) {
  84. $content = $e->getCode() . " - " . $e->getMessage();
  85. }
  86. } catch (Throwable $t) {
  87. $content = $t->getCode() . " - " . $t->getMessage();
  88. }
  89. return new Response($content, $codeResponse);
  90. });
  91. }
  92. public function getAccessId(Application $app)
  93. {
  94. $app->get('/api/v1/radius/access', function (Application $app, Request $request) {
  95. $ch = new Checks();
  96. $codeResponse = 400;
  97. try {
  98. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  99. $apiInstance = new AccessApi(
  100. new Client(),
  101. $config
  102. );
  103. try {
  104. $id = $ch->integer($request, "id");
  105. $apiInstance->setDisabledTenancy(true);
  106. $result = $apiInstance->getById($id);
  107. $apiInstance->setDisabledTenancy(false);
  108. if ($result) {
  109. $content = $result->__toString();
  110. $content = (new CurlOptions())->clearNull($content);
  111. $codeResponse = 200;
  112. } else {
  113. $content = "No se encontro el id = " . $id;
  114. }
  115. } catch (Throwable $e) {
  116. $content = $e->getCode() . " - " . $e->getMessage();
  117. }
  118. } catch (Throwable $t) {
  119. $content = $t->getCode() . " - " . $t->getMessage();
  120. }
  121. return new Response($content, $codeResponse);
  122. });
  123. }
  124. public function getAccesses(Application $app)
  125. {
  126. $app->get('/api/v1/radius/accesses', function (Application $app, Request $request) {
  127. $ch = new Checks();
  128. $codeResponse = 400;
  129. try {
  130. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  131. $apiInstance = new AccessApi(
  132. new Client(),
  133. $config
  134. );
  135. try {
  136. $page = $ch->integer($request, "page", false);
  137. $limit = $ch->integer($request, "limit", false);
  138. $id = $ch->integer($request, "id", false);
  139. $name = $ch->string($request, "name", false);
  140. $profile = $ch->integer($request, "profile", false);
  141. $clientId = $ch->string($request, "clientId", false);
  142. $tenancyId = $ch->integer($request, "tenancyId", false);
  143. $qb_criteria = $ch->integer($request, "qb-criteria", false, null);
  144. $content = "[";
  145. $result = $apiInstance->getList($id, $name, $profile, $clientId, $tenancyId, $qb_criteria, $limit, $page);
  146. foreach ($result as $v) {
  147. // se eliminan los null
  148. $content .= $v->__toString() . ",";
  149. }
  150. if (strlen($content) > 1) {
  151. $content = substr($content, 0, strlen($content) - 1);
  152. }
  153. $content .= "]";
  154. $content = (new CurlOptions())->clearNull($content);
  155. $codeResponse = 200;
  156. } catch (Throwable $e) {
  157. $content = $e->getCode() . " - " . $e->getMessage();
  158. }
  159. } catch (Throwable $t) {
  160. $content = $t->getCode() . " - " . $t->getMessage();
  161. }
  162. return new Response($content, $codeResponse);
  163. });
  164. }
  165. public function postAccess(Application $app)
  166. {
  167. $app->post('/api/v1/radius/access', function (Application $app, Request $request) {
  168. $codeResponse = 400;
  169. try {
  170. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  171. $configBase = $this->setUserPasswordConfig($request, ConfigurationBase::getDefaultConfiguration());
  172. $apiInstance = new AccessApi(
  173. new Client(),
  174. $config
  175. );
  176. $apiValidation = new ValidationApi(
  177. new Client(),
  178. $config
  179. );
  180. $data = json_decode(file_get_contents('php://input'), true);
  181. $utils = new Utils();
  182. $instance = $utils->loadData(new AccessCRUD(), $data);
  183. $errors = $instance->listInvalidProperties();
  184. $content = null;
  185. if (is_null($instance->getName()) || trim($instance->getName()) == "") {
  186. $content = "Debe cargar el nombre del acceso.";
  187. } elseif (is_null($instance->getPassword()) || trim($instance->getPassword()) == "") {
  188. $content = "Debe cargar la contraseña del acceso.";
  189. } elseif (count($errors) === 0) {
  190. // chequeo la tenencia
  191. $apiTenancy = new TenancyApi(
  192. new Client(),
  193. $configBase
  194. );
  195. // verifico si existe la tenencia
  196. if (!is_null($instance->getTenancyId()) && !is_null($apiTenancy->getById($instance->getTenancyId()))) {
  197. $apiValidationBase = new ValidationApiBase(
  198. new Client(),
  199. $configBase
  200. );
  201. // busco si el externalid ya esta cargado
  202. if (!is_null($instance->getClientId()) && $instance->getClientId() > 0) {
  203. // chequeo el cliente exista
  204. if (!$apiValidationBase->validClient($instance->getClientId())) {
  205. $content = "El id del cliente no existe.";
  206. }
  207. }
  208. if (is_null($content)) {
  209. if (is_null($instance->getProfile()) || $instance->getProfile() <= 0) {
  210. $content = "El id de perfil no existe.";
  211. } else {
  212. $result = $apiValidation->validProfile($instance->getProfile());
  213. if (is_null($result)) {
  214. $content = "El id de perfil no existe.";
  215. }
  216. }
  217. if (is_null($content)) {
  218. $apiInstance->create($instance);
  219. // busco el id del cliente, buscandolo por externalid
  220. $result = $apiValidation->existsAccess($instance->getName(), $instance->getTenancyId());
  221. if (!is_null($result)) {
  222. // busco por external id
  223. $content = $result->__toString();
  224. $codeResponse = 200;
  225. } else {
  226. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  227. }
  228. }
  229. }
  230. } else {
  231. $content = "El id de tenencia es incorrecto.";
  232. }
  233. } else {
  234. $content = json_encode($errors);
  235. }
  236. } catch (Throwable $t) {
  237. $content = $t->getCode() . " - " . $t->getMessage();
  238. }
  239. return new Response($content, $codeResponse);
  240. });
  241. }
  242. public function putAccess(Application $app)
  243. {
  244. $app->put('/api/v1/radius/access', function (Application $app, Request $request) {
  245. $codeResponse = 400;
  246. try {
  247. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  248. $configBase = $this->setUserPasswordConfig($request, ConfigurationBase::getDefaultConfiguration());
  249. $apiInstance = new AccessApi(
  250. new Client(),
  251. $config
  252. );
  253. $apiValidation = new ValidationApi(
  254. new Client(),
  255. $config
  256. );
  257. $data = json_decode(file_get_contents('php://input'), true);
  258. $utils = new Utils();
  259. $instance = $utils->loadData(new AccessCRUD(), $data);
  260. $errors = $instance->listInvalidProperties();
  261. $content = null;
  262. if (!is_null($instance->getName()) && trim($instance->getName()) == "") {
  263. $content = "Debe cargar el nombre del acceso.";
  264. } elseif (!is_null($instance->getPassword()) && trim($instance->getPassword()) == "") {
  265. $content = "Debe cargar la contraseña del acceso.";
  266. } elseif (count($errors) === 0) {
  267. // chequeo la tenencia
  268. $apiTenancy = new TenancyApi(
  269. new Client(),
  270. $configBase
  271. );
  272. // verifico si existe la tenencia
  273. if (!is_null($instance->getTenancyId()) && !is_null($apiTenancy->getById($instance->getTenancyId()))) {
  274. $apiValidationBase = new ValidationApiBase(
  275. new Client(),
  276. $configBase
  277. );
  278. // busco si el externalid ya esta cargado
  279. if (!is_null($instance->getClientId()) && $instance->getClientId() > 0) {
  280. // chequeo el cliente exista
  281. if (!$apiValidationBase->validClient($instance->getClientId())) {
  282. $content = "El id del cliente no existe.";
  283. }
  284. }
  285. if (is_null($content)) {
  286. if (is_null($instance->getProfile()) || $instance->getProfile() <= 0) {
  287. $content = "El id de perfil no existe.";
  288. } else {
  289. $result = $apiValidation->validProfile($instance->getProfile());
  290. if (is_null($result)) {
  291. $content = "El id de perfil no existe.";
  292. }
  293. }
  294. if (is_null($content)) {
  295. $apiInstance->update($instance->getId(), $instance);
  296. // busco el id del cliente, buscandolo por externalid
  297. $result = $apiValidation->validAccess($instance->getId());
  298. if (!is_null($result)) {
  299. // busco por external id
  300. $content = $result->__toString();
  301. $codeResponse = 200;
  302. } else {
  303. $content = "No se pudo obtener el acceso cargado. Verifique por sistema si la carga se realizo correctamente.";
  304. }
  305. }
  306. }
  307. } else {
  308. $content = "El id de tenencia es incorrecto.";
  309. }
  310. } else {
  311. $content = json_encode($errors);
  312. }
  313. } catch (Throwable $t) {
  314. $content = $t->getCode() . " - " . $t->getMessage();
  315. }
  316. return new Response($content, $codeResponse);
  317. });
  318. }
  319. public function deleteAccess(Application $app)
  320. {
  321. $app->delete('/api/v1/radius/access', function (Application $app, Request $request) {
  322. $codeResponse = 400;
  323. try {
  324. $config = $this->setUserPasswordConfig($request, Configuration::getDefaultConfiguration());
  325. $apiInstance = new AccessApi(
  326. new Client(),
  327. $config
  328. );
  329. $ch = new Checks();
  330. $id = $ch->integer($request, "id", true);
  331. $apiInstance->setDisabledTenancy(true);
  332. $result = $apiInstance->getById($id);
  333. $apiInstance->setDisabledTenancy(false);
  334. if (!is_null($result)) {
  335. $apiInstance->delete($id);
  336. $content = $result->__toString();
  337. $codeResponse = 200;
  338. } else {
  339. $content = "No se encontro el id = " . $id;
  340. }
  341. } catch (Throwable $t) {
  342. $content = $t->getCode() . " - " . $t->getMessage();
  343. }
  344. return new Response($content, $codeResponse);
  345. });
  346. }
  347. }