Advertisement
Guest User

Untitled

a guest
May 15th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | None | 0 0
  1. /*
  2.  * DataStore.cpp
  3.  *
  4.  *  Created on: 23 Mar 2010
  5.  *      Author: Alexander Mills agm8
  6.  */
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include "SQLiteHandler.h"
  11. #include "DataStore.h"
  12.  
  13. using namespace std;
  14. SQLiteHandler SQLite = SQLiteHandler();
  15. DataStore DS = DataStore();
  16.  
  17. int main(){
  18.     bool z;
  19.     z = DS.checkDoorCredentials("test", "inside");
  20.     if(z){
  21.         cout << "User Exists, Door Opened";
  22.     } else {
  23.         cout << "Unauthorised";
  24.     }
  25. }
  26.  
  27. bool DataStore::checkDoorCredentials(string user, string goingTo){
  28.     string isOutside;
  29.     string tempUser;
  30.     //Check if the user exists
  31.     tempUser = SQLite.RunQuery("SELECT username from users where username='" + user + "'");
  32.     if (tempUser!="NULL") { //if the exists, find out where they are
  33.         isOutside = SQLite.RunQuery("SELECT outside from users where username='" + user + "'");
  34.     }
  35.     if (goingTo == "outside" && isOutside==(string)"1" || goingTo == "inside" && isOutside==(string)"0"){
  36.         return false;
  37.     } else {
  38.         if (goingTo == "outside" && isOutside==(string)"0"){
  39.             SQLite.RunQuery("UPDATE users SET outside='1' WHERE username='" + user + "'");
  40.             return true;
  41.         } else if (goingTo=="inside" && isOutside==(string)"1"){
  42.             SQLite.RunQuery("UPDATE users SET outside='0' WHERE username='" + user + "'");
  43.             return true;
  44.         } else {
  45.             return false;
  46.         }
  47.     }
  48.     return false;
  49. }
  50.  
  51. bool DataStore::checkSecurityCredentials(string user, string pass){
  52.     string tempPass;
  53.     string tempUser;
  54.     string tempType;
  55.     //Check if the user exists
  56.     tempUser = SQLite.RunQuery("SELECT username from users where username='" + user + "'");
  57.     if (tempUser!="NULL") { //if the exist, get their password
  58.         tempPass = SQLite.RunQuery("SELECT password from users where username='" + user + "'");
  59.     } else { // if they don't exist, return false
  60.         return false;
  61.     }
  62.     if (tempUser==user && tempPass == pass){ // if the passes match what is in the db
  63.         tempType = SQLite.RunQuery("SELECT type from users where username='" + tempUser + "'");
  64.         if (tempType==(string)"0"){ // check they are a security person, otherwise say no.
  65.             return true;
  66.         } else {
  67.             return false;
  68.         }
  69.     } else {
  70.         return false;
  71.     }
  72. }
  73.  
  74.  
  75. /*
  76.  * SQLiteHandler.cpp
  77.  *
  78.  *  Created on: 23 Mar 2010
  79.  *      Author: Alexander Mills agm8
  80.  */
  81. #include <vector>
  82. #include <string>
  83. #include <iostream>
  84. #include "SQLiteHandler.h"
  85. #include "sqlite3.h"
  86.  
  87. using namespace std;
  88. sqlite3* db;
  89. sqlite3_stmt *stmt;
  90. int col, cols;
  91. int rc = 0;
  92.  
  93. string SQLiteHandler::RunQuery(string SQL){
  94.     // Open the database
  95.     rc = sqlite3_open("C:\\dataStore.db", &db);
  96.     if (rc ) { // if it returns an error
  97.         fprintf(stderr, "Can not open the database: %s \n", sqlite3_errmsg(db));
  98.     } else { // otherwise prepare the SQL
  99.         rc = sqlite3_prepare_v2(db, SQL.c_str(), -1, &stmt, 0);
  100.         if (rc){ // If there is a problem with the SQL
  101.             fprintf(stderr, "SQL Error: %d : %s\n", rc, sqlite3_errmsg(db));
  102.         } else { //Execute the statement
  103.             cols = sqlite3_column_count(stmt);
  104.             do {
  105.                 rc = sqlite3_step(stmt);
  106.                 switch (rc){
  107.                 case SQLITE_DONE:
  108.                     break;
  109.                 case SQLITE_ROW:
  110.                         const char *txt = (const char*)sqlite3_column_text(stmt, 0);
  111.                         return txt ? txt : "NULL";
  112.                     break;
  113.                 default:
  114.                     fprintf(stderr, "\nError: %d : %s\n", rc, sqlite3_errmsg(db));
  115.                     break;
  116.                 }
  117.             } while (rc == SQLITE_ROW);
  118.             sqlite3_finalize(stmt);
  119.         }
  120.         sqlite3_close(db);
  121.     }
  122.     return "NULL";
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement