Advertisement
andreil

Untitled

Sep 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. #include "logindialog.h"
  2. #include "ui_logindialog.h"
  3. #include <QKeyEvent>
  4. #include "networkrequestsender.h"
  5. #include <QJsonDocument>
  6. #include <QJsonObject>
  7. #include <QCryptographicHash>
  8. #include <QMessageBox>
  9.  
  10. #include "network.h"
  11.  
  12. LoginDialog::LoginDialog(QWidget *parent) :
  13. QDialog(parent),
  14. ui(new Ui::LoginDialog),
  15. m_logged (false),
  16. m_user_id (-1),
  17. m_user_session ("")
  18. {
  19. ui->setupUi(this);
  20.  
  21. move(parentWidget()->window()->frameGeometry().topLeft() +
  22. parentWidget()->window()->rect().center() -
  23. rect().center());
  24.  
  25. ui->lAccountNotExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
  26. ui->lPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
  27. ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
  28. ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
  29.  
  30. ui->pbReg->setEnabled(false);
  31. }
  32.  
  33. LoginDialog::~LoginDialog()
  34. {
  35. delete ui;
  36. }
  37.  
  38. void LoginDialog::keyPressEvent(QKeyEvent *e)
  39. {
  40. if(e->key() != Qt::Key_Escape)
  41. QDialog::keyPressEvent(e);
  42. }
  43.  
  44. void LoginDialog::on_pbCancel_clicked()
  45. {
  46. m_logged = false;
  47. emit finished(QDialog::Rejected);
  48. }
  49.  
  50. void LoginDialog::on_pbLogin_clicked()
  51. {
  52. QString login = ui->leLogin->text();
  53. QString pass = ui->lePass->text();
  54.  
  55. QCryptographicHash md5(QCryptographicHash::Md5);
  56. md5.addData(login.toLatin1());
  57. md5.addData(pass.toLatin1());
  58. md5.addData(login.toLatin1());
  59. md5.addData(pass.toLatin1());
  60. QString md5_str = md5.result().toHex();
  61.  
  62. QCryptographicHash sha256(QCryptographicHash::Sha256);
  63. md5.addData(login.toLatin1());
  64. md5.addData(pass.toLatin1());
  65. md5.addData(login.toLatin1());
  66. md5.addData(pass.toLatin1());
  67. QString sha256_str = sha256.result().toHex();
  68.  
  69. NetworkRequest req(NW_LOGIN_URL);
  70. req.addParam("req", "login");
  71. req.addParam("login", login);
  72. req.addParam("pass", md5_str + ":" + sha256_str);
  73.  
  74.  
  75. NetworkRequestSender* sender = new NetworkRequestSender();
  76. QByteArray data = sender->get(req);
  77.  
  78. QJsonDocument doc = QJsonDocument::fromJson(data);
  79. QJsonObject obj = doc.object();
  80. QString reply = obj["ret"].toString();
  81.  
  82. if (reply.compare("OK") == 0)
  83. {
  84. m_user_id = obj["user_id"].toInt();
  85. m_user_login = login;
  86. m_user_session = obj["session"].toString();
  87. emit finished(QDialog::Accepted);
  88. }
  89. else
  90. {
  91. QMessageBox::critical(this, tr("Ошибка"), tr("Ошибка!\nНет такого пользователя или неверный пароль!"),
  92. QMessageBox::Ok, QMessageBox::NoButton);
  93. }
  94.  
  95. delete sender;
  96. }
  97.  
  98. void LoginDialog::on_leRegLogin_editingFinished()
  99. {
  100. //проверка наличия такого логина
  101. NetworkRequest req(NW_LOGIN_URL);
  102. req.addParam("req", "login_check");
  103. req.addParam("login", ui->leRegLogin->text());
  104.  
  105. NetworkRequestSender* sender = new NetworkRequestSender();
  106. QByteArray data = sender->get(req);
  107.  
  108. QJsonDocument doc = QJsonDocument::fromJson(data);
  109. QString reply = doc.object()["ret"].toString();
  110.  
  111. if (reply.compare("USER_EXIST") == 0)
  112. {
  113. ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 255);");
  114. ui->pbReg->setEnabled(false);
  115. }
  116. else
  117. {
  118. ui->lRegAccountExists->setStyleSheet("color: rgba(255, 0, 0, 0);");
  119. ui->pbReg->setEnabled(true);
  120. }
  121.  
  122. delete sender;
  123. }
  124.  
  125. void LoginDialog::on_leRegPass_textChanged(const QString &arg1)
  126. {
  127. if (arg1.compare(ui->leRegPassCheck->text()) == 0)
  128. ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
  129. else
  130. ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 255);");
  131. }
  132.  
  133. void LoginDialog::on_leRegPassCheck_textChanged(const QString &arg1)
  134. {
  135. if (arg1.compare(ui->leRegPass->text()) == 0)
  136. ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 0);");
  137. else
  138. ui->lRegPassIncorrect->setStyleSheet("color: rgba(255, 0, 0, 255);");
  139. }
  140.  
  141. void LoginDialog::on_pbReg_clicked()
  142. {
  143. QString login = ui->leRegLogin->text();
  144. QString pass = ui->leRegPass->text();
  145.  
  146. QCryptographicHash md5(QCryptographicHash::Md5);
  147. md5.addData(login.toLatin1());
  148. md5.addData(pass.toLatin1());
  149. md5.addData(login.toLatin1());
  150. md5.addData(pass.toLatin1());
  151. QString md5_str = md5.result().toHex();
  152.  
  153. QCryptographicHash sha256(QCryptographicHash::Sha256);
  154. md5.addData(login.toLatin1());
  155. md5.addData(pass.toLatin1());
  156. md5.addData(login.toLatin1());
  157. md5.addData(pass.toLatin1());
  158. QString sha256_str = sha256.result().toHex();
  159.  
  160. NetworkRequest req(NW_LOGIN_URL);
  161. req.addParam("req", "reg");
  162. req.addParam("login", ui->leRegLogin->text());
  163. req.addParam("pass", md5_str + ":" + sha256_str);
  164.  
  165.  
  166. NetworkRequestSender* sender = new NetworkRequestSender();
  167. QByteArray data = sender->get(req);
  168.  
  169. QJsonDocument doc = QJsonDocument::fromJson(data);
  170. QString reply = doc.object()["ret"].toString();
  171.  
  172. if (reply.compare("OK") == 0)
  173. {
  174. QMessageBox::information(this, tr("Регистрация"),
  175. tr("Новый пользователь был зарегестрирован.\nТеперь можно выполнить вход на предыдущей вкладке."),
  176. QMessageBox::Ok, QMessageBox::NoButton);
  177. }
  178. else if (reply.compare("USER_EXIST") == 0)
  179. {
  180. QMessageBox::critical(this, tr("Ошибка"), tr("Пользователь с таким именем уже существует."),
  181. QMessageBox::Ok, QMessageBox::NoButton);
  182. }
  183. else
  184. {
  185. QMessageBox::critical(this, tr("Ошибка"), tr("Ошибка при добавлении пользователя, попробуйте позже."),
  186. QMessageBox::Ok, QMessageBox::NoButton);
  187. }
  188.  
  189. delete sender;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement