Guest User

Untitled

a guest
Apr 30th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include "loginscreen.h"
  2. #include "ui_loginscreen.h"
  3. #include "globalclass.h"
  4. #include <QMessageBox>
  5.  
  6.  
  7. loginScreen::loginScreen(QWidget *parent) :
  8. QWidget(parent),
  9. ui(new Ui::loginScreen)
  10. {
  11. ui->setupUi(this);
  12. GlobalClass globalCon;
  13. //set up for the date and time labels
  14. QTimer *timer = new QTimer(this);
  15. connect(timer, SIGNAL(timeout()), this, SLOT(setupTime()));
  16. timer->start(1000);
  17. ui->showTimeLabel->setText(globalCon.dateTime());
  18.  
  19. //Check for database
  20. if(globalCon.openDB()){
  21. ui->loginStatusBar->setText("[+] Application is ready, please login!");
  22. globalCon.closeDB();
  23. }else{
  24. ui->loginStatusBar->setText("[!] Application unable to connect to the database!");
  25. return;
  26. }
  27. }
  28.  
  29. loginScreen::~loginScreen()
  30. {
  31. delete ui;
  32. }
  33.  
  34. void loginScreen::setupTime()
  35. {
  36. GlobalClass conn;
  37. ui->showTimeLabel->setText(conn.dateTime());
  38. }
  39.  
  40. void loginScreen::on_loginBTN_clicked()
  41. {
  42. GlobalClass conToDB;
  43. qDebug() << "Login clicked";
  44. QString Username, Password;
  45. Username = ui->usernameIN->text();
  46. Password = ui->passwordIN->text();
  47. if(!conToDB.openDB())
  48. {
  49. qDebug() << "ERROR:: connecting to DB!";
  50. ui->loginStatusBar->setText("[!] ERROR: Connect to the database to login!");
  51. return;
  52. }
  53. ui->loginStatusBar->setText("[+] Connected to the database to login!");
  54. qDebug() << "Prepare login query";
  55.  
  56. QSqlQuery qry;
  57. qry.prepare("SELECT Username, Password, Role FROM StaffUsers WHERE Username=:Username AND Password=:Password");
  58. qry.bindValue(":Username", Username);
  59. qry.bindValue(":Password", Password);
  60. qDebug() << qry.executedQuery();
  61. if(qry.exec())
  62. {
  63. //if the log in is valid perform qry.next
  64. qDebug() << "qry next";
  65. if(qry.next())
  66. {
  67. ui->loginStatusBar->setText("[+] Valid Login");
  68. QString msg = "Username = " + qry.value(0).toString() + " \n" +
  69. "Password = " + qry.value(1).toString() + " \n" +
  70. "Role = " +qry.value(2).toString();
  71. QMessageBox::warning(this, "Login was successsful", msg); // later add what type of user
  72.  
  73. // Add link to Admin or user or guest account window
  74. conToDB.closeDB();
  75. qDebug() << "Database closed mainwindow.cpp mainwindow::loginclicked valid user window closed admin open";
  76. QString ckRole = qry.value(2).toString();
  77. if(ckRole == "admin"){
  78. adminDialog = new Administration();
  79. adminDialog->show();
  80. this->hide();
  81. }else if(ckRole == "staff"){
  82. this->close();
  83. staffControl = new StaffAccess;
  84. staffControl->show();
  85. }else if(ckRole == "guest"){
  86. qDebug() << "Still in development";
  87. }else{
  88. if(qry.next()){
  89. QMessageBox::warning(this, "Your login has failed please seek a manager!", msg);
  90. qDebug() << "ERROR:: INVALID SIGNIN";
  91. }
  92. }
  93. // if the login is not valid perform this action
  94. }else{
  95. conToDB.closeDB();
  96. qDebug() << "INVALID";
  97. ui->loginStatusBar->setText("[-] Invalid User details!");
  98. //Add error to Logging 0109 dateTime + Username + Password + CompName + Invalid Login
  99. }
  100. }
  101. }
Add Comment
Please, Sign In to add comment