diff options
author | ramon <ramongomez@us.es> | 2017-04-24 09:41:42 +0000 |
---|---|---|
committer | ramon <ramongomez@us.es> | 2017-04-24 09:41:42 +0000 |
commit | d8b6c70eccacbbb2f8da6c9b6cb084586a8403a1 (patch) | |
tree | f5ef838716eff313b621a76896aa52fed00c80d8 | |
parent | 0dcf48ad78e304ff24764c332c4e7aaeca1c5e36 (diff) |
#708: Establecer tiempo máximo de reserva para que un equipo pueda ser usado en acceso remoto.
git-svn-id: https://opengnsys.es/svn/branches/version1.1@5275 a21b9725-9963-47de-94b9-378ad31fedc9
-rw-r--r-- | admin/Database/ogAdmBD-1.0.6b-1.1.0pre.sql | 2 | ||||
-rw-r--r-- | admin/Database/ogAdmBD-1.1.0pre.sql | 23 | ||||
-rw-r--r-- | admin/Database/ogAdmBD.sql | 2 | ||||
-rw-r--r-- | admin/WebConsole/rest/common.php | 29 | ||||
-rw-r--r-- | admin/WebConsole/rest/opengnsys-api.yml | 125 | ||||
-rw-r--r-- | admin/WebConsole/rest/remotepc.php | 80 |
6 files changed, 154 insertions, 107 deletions
diff --git a/admin/Database/ogAdmBD-1.0.6b-1.1.0pre.sql b/admin/Database/ogAdmBD-1.0.6b-1.1.0pre.sql index 9b55df50..f3a24eef 100644 --- a/admin/Database/ogAdmBD-1.0.6b-1.1.0pre.sql +++ b/admin/Database/ogAdmBD-1.0.6b-1.1.0pre.sql @@ -101,7 +101,7 @@ ALTER TABLE aulas # Nueva tabla para datos del proyecto Remote PC (ticket #708). CREATE TABLE IF NOT EXISTS remotepc ( id INT(11) NOT NULL, - reserved TINYINT(1) DEFAULT '0', + reserved DATETIME DEFAULT NULL, urllogin VARCHAR(100), urllogout VARCHAR(100), PRIMARY KEY (id) diff --git a/admin/Database/ogAdmBD-1.1.0pre.sql b/admin/Database/ogAdmBD-1.1.0pre.sql index fb3b0639..1ae81ddb 100644 --- a/admin/Database/ogAdmBD-1.1.0pre.sql +++ b/admin/Database/ogAdmBD-1.1.0pre.sql @@ -171,25 +171,26 @@ INSERT INTO tipohardwares (idtipohardware, descripcion, urlimg, nemonico) VALUES # Número de puestos del aula permite valores hasta 32768 (ticket #747) ALTER TABLE aulas - MODIFY puestos smallint DEFAULT NULL; + MODIFY puestos smallint DEFAULT NULL; # Nueva tabla para datos del proyecto Remote PC (ticket #708). CREATE TABLE IF NOT EXISTS remotepc ( - id INT(11) NOT NULL, - reserved TINYINT(1) DEFAULT '0', - urllogin VARCHAR(100), - urllogout VARCHAR(100), - PRIMARY KEY (id) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8; + id INT(11) NOT NULL, + reserved DATETIME DEFAULT NULL, + urllogin VARCHAR(100), + urllogout VARCHAR(100), + PRIMARY KEY (id) + ) ENGINE=MyISAM DEFAULT CHARSET=utf8; +ALTER TABLE remotepc + MODIFY reserved DATETIME DEFAULT NULL; # Nuevo comando "Enviar mensaje" (ticket #779) INSERT INTO `comandos` (`idcomando`, `descripcion`, `pagina`, `gestor`, `funcion`, `urlimg`, `aplicambito`, `visuparametros`, `parametros`, `comentarios`, `activo`, `submenu`) VALUES - (16, 'Enviar mensaje', '../comandos/EnviarMensaje.php', '../comandos/gestores/gestor_Comandos.php', 'EnviarMensaje', '', - 31, '', '', '', 1, '' ); + (16, 'Enviar mensaje', '../comandos/EnviarMensaje.php', '../comandos/gestores/gestor_Comandos.php', 'EnviarMensaje', '', 31, '', '', '', 1, '' ); INSERT INTO parametros (idparametro, nemonico, descripcion, nomidentificador, nomtabla, nomliteral, tipopa, visual) VALUES - (39, 'tit', 'Título', '', '', '', 0, 1), - (40, 'msj', 'Contenido', '', '', '', 0, 1); + (39, 'tit', 'Título', '', '', '', 0, 1), + (40, 'msj', 'Contenido', '', '', '', 0, 1); # Evitar error de MySQL con modo NO_ZERO_DATE (ticket #730). ALTER TABLE acciones diff --git a/admin/Database/ogAdmBD.sql b/admin/Database/ogAdmBD.sql index 290d4f23..888f68b6 100644 --- a/admin/Database/ogAdmBD.sql +++ b/admin/Database/ogAdmBD.sql @@ -741,7 +741,7 @@ CREATE TABLE IF NOT EXISTS `programaciones` ( CREATE TABLE IF NOT EXISTS `remotepc` ( `id` int(11) NOT NULL, - `reserved` tinyint(1) DEFAULT '0', + `reserved` datetime DEFAULT NULL, `urllogin` varchar(100), `urllogout` varchar(100), PRIMARY KEY (`id`) diff --git a/admin/WebConsole/rest/common.php b/admin/WebConsole/rest/common.php index 834346e3..d65a9c52 100644 --- a/admin/WebConsole/rest/common.php +++ b/admin/WebConsole/rest/common.php @@ -21,13 +21,13 @@ * @return string JSON response. */ function jsonResponse($status, $response, $opts=0) { - $app = \Slim\Slim::getInstance(); - // HTTP status code. - $app->status($status); - // Content-type HTTP header. - $app->contentType('application/json'); - // JSON response. - echo json_encode($response, $opts); + $app = \Slim\Slim::getInstance(); + // HTTP status code. + $app->status($status); + // Content-type HTTP header. + $app->contentType('application/json; charset=utf-8'); + // JSON response. + echo json_encode($response, $opts); } /** @@ -91,6 +91,21 @@ function checkParameter($param) { } /** + * @brief Check if all parameters are positive integer numbers. + * @param int id ... Identificators to check (variable number of parameters). + * @return boolean "true" if all ids are int>0, otherwise "false". + */ +function checkIds() { + $opts = Array('options' => Array('min_range' => 1)); // Check for int>0 + foreach (func_get_args() as $id) { + if (!filter_var($id, FILTER_VALIDATE_INT, $opts)) { + return false; + } + } + return true; +} + +/** * @fn sendCommand($serverip, $serverport, $reqframe, &$values) * @brief Send a command to an OpenGnsys ogAdmServer and get request. * @param string serverip Server IP address. diff --git a/admin/WebConsole/rest/opengnsys-api.yml b/admin/WebConsole/rest/opengnsys-api.yml index 3a2783c5..d510524f 100644 --- a/admin/WebConsole/rest/opengnsys-api.yml +++ b/admin/WebConsole/rest/opengnsys-api.yml @@ -394,34 +394,7 @@ paths: schema: type: array items: - type: object - properties: - id: - description: client identificator - type: integer - format: int32 - name: - description: client name - type: string - ip: - description: client IP address - type: string - mac: - description: client MAC (Ethernet) address - type: string - ou: - type: object - properties: - id: - type: integer - format: int32 - lab: - type: object - properties: - id: - type: integer - format: int32 - required: [ id, name, ip, mac, ou, lab ] + $ref: "#/definitions/SelectedClientModel" security: - apikey: [] tags: @@ -935,46 +908,28 @@ paths: parameters: - $ref: "#/parameters/ouParam" - $ref: "#/parameters/imageParam" - - name: labid - in: query - description: lab. identificator - required: false - type: integer - responses: - "200": - description: Reserve a client to boot using an operating system image. + - in: body + name: data + description: Operation data schema: type: object properties: - id: - description: selected client id + labid: + description: lab id filter (optional) type: integer format: int32 - mac: - description: client MAC address - type: string - ip: - description: client IP address - type: string - lab: - description: client's lab - type: object - properties: - id: - description: lab id - type: integer - format: int32 - required: [ id ] - ou: - description: client's OU - type: object - properties: - id: - description: OU id - type: integer - format: int32 - required: [ id ] - required: [ id, mac, ip, lab, ou ] + minimum: 1 + maxtime: + description: maximum reservation time, in h. (optional, 24 h. by default) + type: integer + format: int32 + minimum: 1 + default: 24 + responses: + "200": + description: Reserve a client to boot using an operating system image. + schema: + $ref: "#/definitions/SelectedClientModel" security: - apikey: [] tags: @@ -1146,30 +1101,35 @@ parameters: description: OU identificator required: true type: integer + minimum: 1 labParam: name: labid in: path description: Lab identificator required: true type: integer + minimum: 1 clientParam: name: clientid in: path description: Client identificator required: true type: integer + minimum: 1 repoParam: name: repoid in: path description: repository identificator required: true type: integer + minimum: 1 imageParam: name: imageid in: path description: image definition identificator required: true type: integer + minimum: 1 definitions: StatusModel: type: object @@ -1178,6 +1138,7 @@ definitions: description: client id type: integer format: int32 + minimum: 1 ip: description: client IP address type: string @@ -1189,3 +1150,41 @@ definitions: description: flag to check if an user is logged in type: boolean required: [ id, ip, status ] + SelectedClientModel: + type: object + properties: + id: + description: client id + type: integer + format: int32 + minimum: 1 + name: + description: client name + type: string + mac: + description: client MAC address + type: string + ip: + description: client IP address + type: string + lab: + description: client's lab + type: object + properties: + id: + description: lab id + type: integer + format: int32 + minimum: 1 + required: [ id ] + ou: + description: client's OU + type: object + properties: + id: + description: OU id + type: integer + format: int32 + minimum: 1 + required: [ id ] + required: [ id, name, mac, ip, lab, ou ] diff --git a/admin/WebConsole/rest/remotepc.php b/admin/WebConsole/rest/remotepc.php index bf4053ec..3bb50937 100644 --- a/admin/WebConsole/rest/remotepc.php +++ b/admin/WebConsole/rest/remotepc.php @@ -21,7 +21,7 @@ * @param integer imageid image identificator * @param integer labid lab. identificator (optional) */ -$app->post('/ous/:ouid/images/:imageid/reserve', 'validateApiKey', +$app->post('/ous/:ouid/images/:imageid/reserve(/)', 'validateApiKey', function($ouid, $imageid) use ($app) { global $cmd; global $AMBITO_ORDENADORES; @@ -33,16 +33,33 @@ $app->post('/ous/:ouid/images/:imageid/reserve', 'validateApiKey', $ogagent = Array(); // Checking parameters. - $ouid = htmlspecialchars($ouid); - $imageid = htmlspecialchars($imageid); - $labid = str_replace("%", "\%", htmlspecialchars($app->request()->params('lab'))); - if (empty($labid)) $labid = '%'; // Clients in any lab. + try { + if (!check_ids($ouid, $imageid)) { + throw new Exception("Ids. must be positive integers"); + } + // Reading POST parameters in JSON format. + $input = json_decode($app->request()->getBody()); + $labid = isset($input->labid) ? $input->labid : '%'; // Default: no lab. filter + $maxtime = isset($input->maxtime) ? $input->maxtime : 24; // Default: 24 h. + if (!filter_var($labid, FILTER_VALIDATE_INT, $opts) and $labid !== '%') { + throw new Exception("Lab id. must be positive integer"); + } + if (!filter_var($maxtime, FILTER_VALIDATE_INT, $opts)) { + throw new Exception("Time must be positive integer (in hours)"); + } + } catch (Exception $e) { + // Communication error. + $response["message"] = $e->getMessage(); + jsonResponse(400, $response); + $app->stop(); + } // Randomly choose a client with image installed and get ogAdmServer data. $cmd->texto = <<<EOD SELECT adm.idadministradorcentro, entornos.ipserveradm, entornos.portserveradm, - ordenadores.idordenador, ordenadores.ip, ordenadores.mac, ordenadores.agentkey, - ordenadores_particiones.numdisk, ordenadores_particiones.numpar, - aulas.idaula, aulas.idcentro, remotepc.reserved + ordenadores.idordenador, ordenadores.nombreordenador, ordenadores.ip, + ordenadores.mac, ordenadores.agentkey, ordenadores_particiones.numdisk, + ordenadores_particiones.numpar, aulas.idaula, aulas.idcentro, + remotepc.reserved FROM entornos, ordenadores JOIN aulas USING(idaula) RIGHT JOIN administradores_centros AS adm USING(idcentro) @@ -62,11 +79,12 @@ EOD; $rs->Primero(); if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { // Check if client is not reserved. - if ($rs->campos["reserved"] !== 1) { + if (is_null($rs->campos["reserved"])) { // Read query data. $serverip = $rs->campos["ipserveradm"]; $serverport = $rs->campos["portserveradm"]; $clntid = $rs->campos["idordenador"]; + $clntname = $rs->campos["name"]; $clntip = $rs->campos["ip"]; $clntmac = $rs->campos["mac"]; $agentkey = $rs->campos["agentkey"]; @@ -101,7 +119,7 @@ EOD; $timestamp = time(); $cmd->texto = <<<EOD INSERT INTO remotepc - SET id='$clntid', reserved=1, urllogin=NULL, urllogout=NULL + SET id='$clntid', reserved=NOW() + INTERVAL $maxtime HOUR, urllogin=NULL, urllogout=NULL ON DUPLICATE KEY UPDATE id=VALUES(id), reserved=VALUES(reserved), urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); @@ -139,6 +157,7 @@ EOD; sendCommand($serverip, $serverport, $reqframe, $values); // Compose JSON response. $response['id'] = $clntid; + $response['name'] = $clntname; $response['ip'] = $clntip; $response['mac'] = $clntmac; $response['lab']['id'] = $labid; @@ -178,11 +197,21 @@ $app->post('/ous/:ouid/labs/:labid/clients/:clntid/events', 'validateApiKey', global $userid; $response = Array(); - // Reading JSON parameters. + // Checking parameters. try { + if (!check_ids($ouid, $labid, $clntid)) { + throw new Exception("Ids. must be positive integers"); + } + // Reading JSON parameters. $input = json_decode($app->request()->getBody()); $urlLogin = htmlspecialchars($input->urlLogin); $urlLogout = htmlspecialchars($input->urlLogout); + if (!filter_var($urlLogin, FILTER_VALIDATE_URL)) { + throw new Exception("Must be a valid URL for login notification"); + } + if (!filter_var($urlLogout, FILTER_VALIDATE_URL)) { + throw new Exception("Must be a valid URL for logout notification"); + } } catch (Exception $e) { // Error message. $response["message"] = $e->getMessage(); @@ -190,10 +219,6 @@ $app->post('/ous/:ouid/labs/:labid/clients/:clntid/events', 'validateApiKey', $app->stop(); } - // Checking parameters. - $ouid = htmlspecialchars($ouid); - $labid = htmlspecialchars($labid); - $clntid = htmlspecialchars($clntid); // Select client data for UDS compatibility. $cmd->texto = <<<EOD SELECT adm.idadministradorcentro, ordenadores.idordenador, remotepc.* @@ -213,16 +238,15 @@ EOD; $rs->Primero(); if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { // Check if client is reserved. - if ($rs->campos["reserved"] == 1) { + if (! is_null($rs->campos["reserved"])) { // Updating DB if client is reserved. $cmd->CreaParametro("@urllogin", $urlLogin, 0); $cmd->CreaParametro("@urllogout", $urlLogout, 0); $cmd->texto = <<<EOD INSERT INTO remotepc - SET id='$clntid', reserved=1, urllogin=@urllogin, urllogout=@urllogout + SET id='$clntid', urllogin=@urllogin, urllogout=@urllogout ON DUPLICATE KEY UPDATE - id=VALUES(id), reserved=VALUES(reserved), - urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); + id=VALUES(id), urllogin=VALUES(urllogin), urllogout=VALUES(urllogout); EOD; if ($cmd->Ejecutar()) { // Confirm operation. @@ -260,9 +284,17 @@ $app->delete('/ous/:ouid/labs/:labid/clients/:clntid/unreserve', 'validateApiKey $ogagent = Array(); // Checking parameters. - $ouid = htmlspecialchars($ouid); - $labid = htmlspecialchars($labid); - $clntid = htmlspecialchars($clntid); + try { + if (!check_ids($ouid, $labid, $clntid)) { + throw new Exception("Ids. must be positive integers"); + } + } catch (Exception $e) { + // Error message. + $response["message"] = $e->getMessage(); + jsonResponse(400, $response); + $app->stop(); + } + // Select client data for UDS compatibility. $cmd->texto = <<<EOD SELECT adm.idadministradorcentro, ordenadores.idordenador, ordenadores.ip, ordenadores.agentkey, remotepc.reserved @@ -282,7 +314,7 @@ EOD; $rs->Primero(); if (checkAdmin($rs->campos["idadministradorcentro"]) and checkParameter($rs->campos["idordenador"])) { // Check if client is reserved. - if ($rs->campos["reserved"] == 1) { + if (! is_null($rs->campos["reserved"])) { // Read query data. $clntip = $rs->campos["ip"]; $agentkey = $rs->campos["agentkey"]; @@ -292,7 +324,7 @@ EOD; $cmd->Ejecutar(); $cmd->texto = <<<EOD UPDATE remotepc - SET reserved=0, urllogin=NULL, urllogout=NULL + SET reserved=NULL, urllogin=NULL, urllogout=NULL WHERE id='$clntid'; EOD; $cmd->Ejecutar(); |