From 7287116eb802ba1f7cbf0bc36783318ad5288c70 Mon Sep 17 00:00:00 2001 From: samilliken Date: Wed, 4 Mar 2015 09:09:55 -0500 Subject: [PATCH] Checkpointing all more routines added for #2 --- repo/api/index.php | 117 +++++++++++++++++++++++++++++--------- repo/create.sql | 34 ++++++++++-- repo/repo.inc.php | 136 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 243 insertions(+), 44 deletions(-) diff --git a/repo/api/index.php b/repo/api/index.php index 3821f47..079cd48 100644 --- a/repo/api/index.php +++ b/repo/api/index.php @@ -9,15 +9,18 @@ // $app->get( '/cdutemplate', 'getCDUTemplate' ); // $app->get( '/cdutemplate/byid/:cdutemplateid', 'getCDUTemplateByID' ); - // $app->get( '/devicetemplate', 'getDeviceTemplate' ); - // $app->get( '/devicetemplate/byid/:templateid', 'getDeviceTemplateByID' ); - // $app->get( '/devicetemplate/bymanufacturer/:manufacturerid', 'getDeviceTemplateByManufacturer' ); + $app->get( '/devicetemplate', 'getDeviceTemplate' ); + $app->get( '/devicetemplate/byid/:templateid', 'getDeviceTemplateByID' ); + $app->get( '/devicetemplate/bymanufacturer/:manufacturerid', 'getDeviceTemplateByManufacturer' ); $app->get( '/manufacturer', 'getManufacturer' ); $app->get( '/manufacturer/byid/:manufacturerid', 'getManufacturerByID' ); $app->get( '/manufacturer/pending', 'getPendingManufacturer' ); $app->get( '/manufacturer/pending/byid/:requestid', 'getPendingManufacturerByID' ); $app->put( '/manufacturer', 'authenticate', 'queueManufacturer' ); + $app->put( '/manufacturer/approve', 'authenticate', 'approveManufacturer' ); + + $app->put( '/devicetemplate', 'authenticate', 'queueDeviceTemplate' ); /** * Need to accept all options requests for PUT calls to work via jquery @@ -81,19 +84,26 @@ function echoRespnse($status_code, $response) { * Checking if the request has valid api key in the 'Authorization' header */ function authenticate(\Slim\Route $route) { + global $currUser; + $currUser = new Users(); // If being called from the same server, short circuit this process if ( $_SERVER["REMOTE_ADDR"] == "127.0.0.1" ) { return; } + // If the Session variable 'userid' exists, this is an interactive session + // Rights are adminstered by the UI, rather than the API + if ( isset( $_SESSION['userid'] ) ) { + $currUser->UserID = $_SESSION['userid']; + return; + } + // Getting request headers $headers = apache_request_headers(); $response = array(); $app = \Slim\Slim::getInstance(); - global $currUser; - $u = new Users(); // Verifying Authorization Header if (isset($headers['APIKey'])) { @@ -103,7 +113,7 @@ function authenticate(\Slim\Route $route) { // validating api key // An API key was passed, so check to see if it's real or not - if (! $currUser = $u->verifyAPIKey($apikey, $ipaddress)) { + if (! $currUser->verifyAPIKey($apikey, $ipaddress)) { // api key is not present in users table $response["error"] = true; $response["errorcode"] = 401; @@ -146,25 +156,47 @@ function getDeviceTemplate() { echoRespnse( 200, $response ); } + function getDeviceTemplateById( $templateid ) { + $dt = new DeviceTemplates(); + $dtList = $dt->getDeviceTemplateById( $templateid ); + + $response['error'] = false; + $response['errorcode'] = 200; + $response['devicetemplates'] = array(); + $response['error'] = false; + $response['errorcode'] = 200; + $response['devicetemplates'] = array(); + foreach ( $dtList as $devtmp ) { + $tmp = array(); + foreach ( $devtmp as $prop=>$value ) { + $tmp[$prop] = $value; + } + array_push( $response['devicetemplates'], $tmp ); + } + + echoRespnse( 200, $response ); + + } + function getDeviceTemplateByManufacturer( $manufacturerid ) { $dt = new DeviceTemplates(); $dtList = $dt->getDeviceTemplateByMFG( $manufacturerid ); - $response['error'] = false; - $response['errorcode'] = 200; - $response['devicetemplates'] = array(); - $response['error'] = false; - $response['errorcode'] = 200; - $response['devicetemplates'] = array(); - foreach ( $dtList as $devtmp ) { - $tmp = array(); - foreach ( $devtmp as $prop=>$value ) { - $tmp[$prop] = $value; - } - array_push( $response['devicetemplates'], $tmp ); - } + $response['error'] = false; + $response['errorcode'] = 200; + $response['devicetemplates'] = array(); + $response['error'] = false; + $response['errorcode'] = 200; + $response['devicetemplates'] = array(); + foreach ( $dtList as $devtmp ) { + $tmp = array(); + foreach ( $devtmp as $prop=>$value ) { + $tmp[$prop] = $value; + } + array_push( $response['devicetemplates'], $tmp ); + } - echoRespnse( 200, $response ); + echoRespnse( 200, $response ); } // @@ -258,10 +290,10 @@ function getManufacturerByID($ManufacturerID) { // Returns: 200 if successful // function queueManufacturer() { - $request = \Slim\Slim::getInstance()->request(); + $app = \Slim\Slim::getInstance(); $response = array(); $m = new ManufacturersQueue(); - $m->Name = $request->put('Name'); + $m->Name = $app->request->put('Name'); if ( $m->queueManufacturer() ) { $response['error'] = false; $response['errorcode'] = 200; @@ -276,12 +308,34 @@ function queueManufacturer() { } } + function approveManufacturer() { + global $currUser; + + $app = \Slim\Slim::getInstance(); + $response = array(); + $m = new ManufacturersQueue(); + $vars = json_decode( $app->request->getBody() ); + $m->Name = $vars->Name; + $m->RequestID = $vars->RequestID; + if ( $m->approveRequest( $currUser ) ) { + $response['error'] = false; + $response['errorcode'] = 200; + $response['message'] = 'Manufacturer has been approved.'; + echoRespnse( 200, $response ); + } else { + $response['error'] = true; + $response['errorcode'] = 403; + $response['message'] = 'Error processing request.'; + echoRespnse( 403, $response ); + } + } + function queueDeviceTemplate() { - $request = Slim::getInstance()->request(); + global $currUser; + $app = \Slim\Slim::getInstance(); $response = array(); - -/* $t = new DeviceTemplateQueue(); + $t = new DeviceTemplatesQueue(); $t->ManufacturerID = $app->request->put('ManufacturerID'); $t->Model = $app->request->put('Model'); $t->Height = $app->request->put('Height'); @@ -296,7 +350,18 @@ function queueDeviceTemplate() { $t->ChassisSlots = $app->request->put('ChassisSlots'); $t->RearChassisSlots = $app->request->put('RearChassisSlots'); $t->SubmittedBy = $currUser->UserID; -*/ + + if ( $t->queueDeviceTemplate() ) { + $response['error'] = false; + $response['errorcode'] = 200; + $response['message'] = 'Device template queued for approval.'; + } else { + $response['error'] = true; + $response['errorcode'] = 403; + $response['message'] = 'Error processing request.'; + } + + echoRespnse( $response['errorcode'], $response ); } $app->run(); diff --git a/repo/create.sql b/repo/create.sql index b36d39b..3687c3a 100644 --- a/repo/create.sql +++ b/repo/create.sql @@ -52,7 +52,7 @@ CREATE TABLE DeviceTemplatesQueue ( Height int(11) NOT NULL, Weight int(11) NOT NULL, Wattage int(11) NOT NULL, - DeviceType enum('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure') NOT NULL default 'Server', + DeviceType varchar(23) NOT NULL default 'Server', PSCount int(11) NOT NULL, NumPorts int(11) NOT NULL, Notes text NOT NULL, @@ -69,14 +69,13 @@ CREATE TABLE DeviceTemplatesQueue ( DROP TABLE IF EXISTS DeviceTemplates; CREATE TABLE DeviceTemplates ( - RequestID INT(11) NOT NULL AUTO_INCREMENT, - TemplateID INT(11) NOT NULL, + TemplateID INT(11) NOT NULL AUTO_INCREMENT, ManufacturerID int(11) NOT NULL, Model varchar(80) NOT NULL, Height int(11) NOT NULL, Weight int(11) NOT NULL, Wattage int(11) NOT NULL, - DeviceType enum('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure') NOT NULL default 'Server', + DeviceType varchar(23) NOT NULL default 'Server', PSCount int(11) NOT NULL, NumPorts int(11) NOT NULL, Notes text NOT NULL, @@ -85,7 +84,7 @@ CREATE TABLE DeviceTemplates ( ChassisSlots SMALLINT(6) NOT NULL, RearChassisSlots SMALLINT(6) NOT NULL, LastModified DATETIME NOT NULL, - PRIMARY KEY (RequestID) + PRIMARY KEY (TemplateID) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; @@ -123,6 +122,31 @@ CREATE TABLE TemplatePortQueues ( ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS CDUTemplates; +CREATE TABLE fac_CDUTemplate ( + TemplateID int(11) NOT NULL AUTO_INCREMENT, + ManufacturerID int(11) NOT NULL, + Model varchar(80) NOT NULL, + Managed int(1) NOT NULL, + ATS int(1) NOT NULL, + SNMPVersion varchar(2) NOT NULL DEFAULT '2c', + VersionOID varchar(80) NOT NULL, + Multiplier varchar(6), + OID1 varchar(80) NOT NULL, + OID2 varchar(80) NOT NULL, + OID3 varchar(80) NOT NULL, + ATSStatusOID varchar(80) NOT NULL, + ATSDesiredResult varchar(80) NOT NULL, + ProcessingProfile enum('SingleOIDWatts','SingleOIDAmperes','Combine3OIDWatts','Combine3OIDAmperes','Convert3PhAmperes'), + Voltage int(11) NOT NULL, + Amperage int(11) NOT NULL, + NumOutlets int(11) NOT NULL, + GlobalID int(11) NOT NULL, + ShareToRepo tinyint(1) NOT NULL DEFAULT 0, + KeepLocal tinyint(1) NOT NULL DEFAULT 0, + PRIMARY KEY (TemplateID), + KEY ManufacturerID (ManufacturerID), + UNIQUE KEY (ManufacturerID, Model) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS SensorTemplates; diff --git a/repo/repo.inc.php b/repo/repo.inc.php index 5be6fe9..64264ad 100644 --- a/repo/repo.inc.php +++ b/repo/repo.inc.php @@ -76,18 +76,21 @@ function lastInsertId() { } function queueManufacturer() { + global $currUser; + $this->Name = sanitize( $this->Name ); $st = $this->prepare( "select * from Manufacturers where UCASE(Name)=UCASE(:Name)" ); $st->execute( array( ":Name" => $this->Name ) ); - $st->setFetchMode( PDO::FETCH_CLASS, "ManufacturersQueue" ); + error_log( "Searching for existing Mfg = " . $this->Name ); + $st->setFetchMode( PDO::FETCH_CLASS, "Manufacturers" ); $row = $st->fetch(); - if ( $row->ManufacturerID > 0 ) { + if ( @$row->ManufacturerID > 0 ) { error_log( "Table Manufacturers collision: Name=>" . $this->Name ); return false; } - $st = $this->prepare( "insert into ManufacturersQueue set Name=:Name, SubmittedBy='scott@themillikens.com', SubmissionDate=now()" ); - if ( ! $st->execute( array( ":Name" => $this->Name ) ) ) { + $st = $this->prepare( "insert into ManufacturersQueue set Name=:Name, SubmittedBy=:UserID, SubmissionDate=now()" ); + if ( ! $st->execute( array( ":Name" => $this->Name, ":UserID"=>$currUser->UserID ) ) ) { return null; } @@ -101,7 +104,7 @@ function viewStatus( $RequestID = null, $UserID = null ) { $st = $this->prepare( "select * from ManufacturersQueue where RequestID=:RequestID" ); $st->execute( array( ":RequestID"=>$RequestID ) ); } else { - $st = $this->prepare( "select * from ManufacturersQueue order by Name ASC, RequestID ASC" ); + $st = $this->prepare( "select * from ManufacturersQueue where ApprovedBy='' order by Name ASC, RequestID ASC" ); $st->execute(); } @@ -114,20 +117,18 @@ function viewStatus( $RequestID = null, $UserID = null ) { return $mfgList; } - function approveRequest( $RequestID ) { - global $currUser; - + function approveRequest( $currUser ) { $st = $this->prepare( "select * from ManufacturersQueue where RequestID=:RequestID" ); - $st->execute( array( ":RequestID"=>$RequestID ) ); + $st->execute( array( ":RequestID" => $this->RequestID ) ); $st->setFetchMode( PDO::FETCH_CLASS, "ManufacturersQueue" ); if ( $req = $st->fetch() ) { // If the ManufacturerID is set in the request, this is an update if ( $this->ManufacturerID > 0 ) { - $st = prepare( "update Manufacturers set Name=:Name, LastModified=now() where + $st = $this->prepare( "update Manufacturers set Name=:Name, LastModified=now() where ManufacturerID=:ManufacturerID" ); $st->execute( array( ":Name"=>$this->Name, ":ManufacturerID"=>$this->ManufacturerID ) ); } else { - $st->prepare( "insert into Manufacturers set Name=:Name, LastModified=now()" ); + $st = $this->prepare( "insert into Manufacturers set Name=:Name, LastModified=now()" ); $st->execute( array( ":Name"=>$this->Name ) ); $this->ManufacturerID=$this->lastInsertId(); } @@ -135,9 +136,11 @@ function approveRequest( $RequestID ) { $this->ApprovedBy = $currUser->UserID; $st = $this->prepare( "update ManufacturersQueue set ApprovedBy=:UserID, - ManufacturerID=:ManufacturerID, ApprovedTime=now() where RequestID=:RequestID" ); + ManufacturerID=:ManufacturerID, ApprovedDate=now() where RequestID=:RequestID" ); $st->execute( array( ":UserID"=>$currUser->UserID, ":ManufacturerID"=>$this->ManufacturerID, ":RequestID"=>$this->RequestID ) ); + } else { + error_log( "Fetch failed for request=" . $this->RequestID ); } return true; @@ -184,8 +187,21 @@ function getDeviceTemplate( $TemplateID = null ) { return $templateList; } + function getDeviceTemplatebyId( $templateid ) { + $st = $this->prepare( "select * from DeviceTemplates where TemplateID=:TemplateID" ); + $st->execute( array( ":TemplateID"=>$templateid ) ); + + $st->setFetchMode( PDO::FETCH_CLASS, "DeviceTemplates" ); + $templateList = array(); + while ( $t = $st->fetch() ) { + $templateList[] = $t; + } + + return $templateList; + } + function getDeviceTemplateByMFG( $manufacturerid ) { - $st = $this->prepare( "select * from DeviceTemplates where ManufacturerID=:ManufacturerID" ); + $st = $this->prepare( "select * from DeviceTemplates where ManufacturerID=:ManufacturerID order by Model ASC" ); $st->execute( array( ":ManufacturerID"=>$manufacturerid ) ); $templateList = array(); @@ -200,6 +216,100 @@ function getDeviceTemplateByMFG( $manufacturerid ) { } +class DeviceTemplatesQueue { + var $TemplateID; + var $ManufacturerID; + var $Model; + var $Height; + var $Weight; + var $Wattage; + var $DeviceType; + var $PSCount; + var $NumPorts; + var $Notes; + var $FrontPictureFile; + var $RearPictureFile; + var $ChassisSlots; + var $RearChassisSlots; + var $SubmittedBy; + var $SubmissionDate; + var $ApprovedBy; + var $ApprovedDate; + + function prepare( $sql ) { + global $dbh; + return $dbh->prepare( $sql ); + } + + function lastInsertId() { + global $dbh; + return $dbh->lastInsertId(); + } + + function makeSafe() { + $this->TemplateID = intval( $this->TemplateID ); + $this->ManufacturerID = intval( $this->ManufacturerID ); + $this->Model = sanitize( $this->Model ); + $this->Height = intval( $this->Height ); + $this->Weight = intval( $this->Weight ); + $this->Wattage = intval( $this->Wattage ); + $this->DeviceType = intval( $this->DeviceType ); + $this->PSCount = intval( $this->PSCount ); + $this->NumPorts = intval( $this->NumPorts ); + $this->Notes = sanitize( $this->Notes ); + $this->FrontPictureFile = sanitize( $this->FrontPictureFile ); + $this->RearPictureFile = sanitize( $this->RearPictureFile ); + $this->ChassisSlots = intval( $this->ChassisSlots ); + $this->RearChassisSlots = intval( $this->RearChassisSlots ); + } + + function queueDeviceTemplate() { + $this->makeSafe(); + + // Make sure that we don't violate unique keys + // If the TemplateID > 0, this is an update to a record + if ( $this->TemplateID == 0 ) { + $st = $this->prepare( "select count(*) as Total from DeviceTemplates where ManufacturerID=:ManufacturerID and ucase(Model)=ucase(:Model)" ); + $st->execute( array( ":ManufacturerID"=>$this->ManufacturerID, ":Model"=>$this->Model ) ); + $row = $st->fetch(); + + if ( $row["Total"] > 0 ) { + return false; + } + } + + // At this stage, there's no difference in the queueing other than the + // fact that we add in the TemplateID for existing records. + $st = $this->prepare( "insert into DeviceTemplatesQueue set TemplateID=:TemplateID, + ManufacturerID=:ManufacturerID, Model=:Model, Height=:Height, + Weight=:Weight, Wattage=:Wattage, DeviceType=:DeviceType, + PSCount=:PSCount, NumPorts=:NumPorts, Notes=:Notes, + FrontPictureFile=:FrontPictureFile, RearPictureFile=:RearPictureFile, + ChassisSlots=:ChassisSlots, RearChassisSlots=:RearChassisSlots, + SubmittedBy=:SubmittedBy, SubmissionDate=now()"); + $st->execute( array( ":TemplateID"=>$this->TemplateID, + ":ManufacturerID"=>$this->ManufacturerID, + ":Model"=>$this->Model, + ":Height"=>$this->Height, + ":Weight"=>$this->Weight, + ":Wattage"=>$this->Wattage, + ":DeviceType"=>$this->DeviceType, + ":PSCount"=>$this->PSCount, + ":NumPorts"=>$this->NumPorts, + ":Notes"=>$this->Notes, + ":FrontPictureFile"=>$this->FrontPictureFile, + ":RearPictureFile"=>$this->RearPictureFile, + ":ChassisSlots"=>$this->ChassisSlots, + ":RearChassisSlots"=>$this->RearChassisSlots, + ":SubmittedBy"=>$this->SubmittedBy ) ); + + $this->RequestID = $this->lastInsertId(); + + return $this->RequestID; + } +} + + class Moderators { /* Simple authorization schema: If you are an Administrator, you can do anything