Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DataStore.cpp
- *
- * Created on: 23 Mar 2010
- * Author: Alexander Mills agm8
- */
- #include <iostream>
- #include <string>
- #include "SQLiteHandler.h"
- #include "DataStore.h"
- using namespace std;
- SQLiteHandler SQLite = SQLiteHandler();
- DataStore DS = DataStore();
- int main(){
- bool z;
- z = DS.checkDoorCredentials("test", "inside");
- if(z){
- cout << "User Exists, Door Opened";
- } else {
- cout << "Unauthorised";
- }
- }
- bool DataStore::checkDoorCredentials(string user, string goingTo){
- string isOutside;
- string tempUser;
- //Check if the user exists
- tempUser = SQLite.RunQuery("SELECT username from users where username='" + user + "'");
- if (tempUser!="NULL") { //if the exists, find out where they are
- isOutside = SQLite.RunQuery("SELECT outside from users where username='" + user + "'");
- }
- if (goingTo == "outside" && isOutside==(string)"1" || goingTo == "inside" && isOutside==(string)"0"){
- return false;
- } else {
- if (goingTo == "outside" && isOutside==(string)"0"){
- SQLite.RunQuery("UPDATE users SET outside='1' WHERE username='" + user + "'");
- return true;
- } else if (goingTo=="inside" && isOutside==(string)"1"){
- SQLite.RunQuery("UPDATE users SET outside='0' WHERE username='" + user + "'");
- return true;
- } else {
- return false;
- }
- }
- return false;
- }
- bool DataStore::checkSecurityCredentials(string user, string pass){
- string tempPass;
- string tempUser;
- string tempType;
- //Check if the user exists
- tempUser = SQLite.RunQuery("SELECT username from users where username='" + user + "'");
- if (tempUser!="NULL") { //if the exist, get their password
- tempPass = SQLite.RunQuery("SELECT password from users where username='" + user + "'");
- } else { // if they don't exist, return false
- return false;
- }
- if (tempUser==user && tempPass == pass){ // if the passes match what is in the db
- tempType = SQLite.RunQuery("SELECT type from users where username='" + tempUser + "'");
- if (tempType==(string)"0"){ // check they are a security person, otherwise say no.
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /*
- * SQLiteHandler.cpp
- *
- * Created on: 23 Mar 2010
- * Author: Alexander Mills agm8
- */
- #include <vector>
- #include <string>
- #include <iostream>
- #include "SQLiteHandler.h"
- #include "sqlite3.h"
- using namespace std;
- sqlite3* db;
- sqlite3_stmt *stmt;
- int col, cols;
- int rc = 0;
- string SQLiteHandler::RunQuery(string SQL){
- // Open the database
- rc = sqlite3_open("C:\\dataStore.db", &db);
- if (rc ) { // if it returns an error
- fprintf(stderr, "Can not open the database: %s \n", sqlite3_errmsg(db));
- } else { // otherwise prepare the SQL
- rc = sqlite3_prepare_v2(db, SQL.c_str(), -1, &stmt, 0);
- if (rc){ // If there is a problem with the SQL
- fprintf(stderr, "SQL Error: %d : %s\n", rc, sqlite3_errmsg(db));
- } else { //Execute the statement
- cols = sqlite3_column_count(stmt);
- do {
- rc = sqlite3_step(stmt);
- switch (rc){
- case SQLITE_DONE:
- break;
- case SQLITE_ROW:
- const char *txt = (const char*)sqlite3_column_text(stmt, 0);
- return txt ? txt : "NULL";
- break;
- default:
- fprintf(stderr, "\nError: %d : %s\n", rc, sqlite3_errmsg(db));
- break;
- }
- } while (rc == SQLITE_ROW);
- sqlite3_finalize(stmt);
- }
- sqlite3_close(db);
- }
- return "NULL";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement