FRMLoginF.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "AppGlobalF.h"
  4. #include "ITSUtilF.h"
  5. #include "ITSDbF.h"
  6. #include "EncryptionF.h"
  7. #pragma hdrstop
  8. #include <ADODB.hpp>
  9. #include <DB.hpp>
  10. #include <winsock.h>
  11. #include "FrmLoginF.h"
  12. #include "FrmPswdChngeF.h"
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. #pragma link "cxButtons"
  16. #pragma link "cxContainer"
  17. #pragma link "cxControls"
  18. #pragma link "cxEdit"
  19. #pragma link "cxGraphics"
  20. #pragma link "cxLabel"
  21. #pragma link "cxLookAndFeelPainters"
  22. #pragma link "cxLookAndFeels"
  23. #pragma link "dxSkinBlack"
  24. #pragma link "dxSkinBlue"
  25. #pragma link "dxSkinsCore"
  26. #pragma link "dxGDIPlusClasses"
  27. #pragma link "dxSkinMcSkin"
  28. #pragma resource "*.dfm"
  29. TFRMLogin *FRMLogin = NULL;
  30. //---------------------------------------------------------------------------
  31. __fastcall TFRMLogin::TFRMLogin(TComponent* Owner)
  32. : TForm(Owner)
  33. {
  34. //ITSSkin_Load(this);
  35. m_nLoginTryCnt = 0;
  36. m_sUserName = ""; //사용자 명
  37. m_sUserID = ""; //사용자 ID
  38. m_sUserRightID = ""; //사용자 권한 ID
  39. m_sUserRightName = ""; //사용자 권한 명
  40. m_sLoginTime = "";
  41. m_bLogin = false;
  42. LbMessage->Caption = "";
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TFRMLogin::BtnCloseClick(TObject *Sender)
  46. {
  47. ModalResult = mrCancel;
  48. Close();
  49. }
  50. //---------------------------------------------------------------------------
  51. void __fastcall TFRMLogin::BtnPswdChngeClick(TObject *Sender)
  52. {
  53. try
  54. {
  55. FRMPswdChnge = new TFRMPswdChnge(this);
  56. FRMPswdChnge->EdUserID->Text = EdUserID->Text;
  57. FRMPswdChnge->ShowModal();
  58. EdUserID->Text = FRMPswdChnge->EdUserID->Text.Trim();
  59. EdPswd->Text = FRMPswdChnge->EdCnfmNewPswd->Text.Trim();
  60. delete FRMPswdChnge;
  61. FRMPswdChnge = NULL;
  62. }
  63. catch(...)
  64. {
  65. }
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TFRMLogin::BtnLoginClick(TObject *Sender)
  69. {
  70. String sUserID = Trim(EdUserID->Text);
  71. String sUserPswd = Trim(EdPswd->Text);
  72. //c4ca4238a0b923820dcc509a6f75849b == "1"
  73. if (sUserID.Compare("") == 0)
  74. {
  75. Application->NormalizeTopMosts();
  76. if (g_sLang == "kr") Application->MessageBox(L"아이디를 입력하세요.", L"아이디 입력 오류", MB_OK|MB_ICONERROR); // ID를 입력하세요.
  77. else Application->MessageBox(L"Please enter Use ID.", L"User ID Error", MB_OK|MB_ICONERROR); // ID를 입력하세요.
  78. Application->RestoreTopMosts();
  79. EdUserID->SetFocus();
  80. return;
  81. }
  82. if (sUserPswd.Compare("") == 0)
  83. {
  84. Application->NormalizeTopMosts();
  85. if (g_sLang == "kr") Application->MessageBox(L"비밀번호를 입력하세요.", L"비밀번호 입력 오류", MB_OK|MB_ICONERROR); // 비밀번호를 입력하세요.
  86. else Application->MessageBox(L"Please enter User Password.", L"Password Error", MB_OK|MB_ICONERROR); // 비밀번호를 입력하세요.
  87. Application->RestoreTopMosts();
  88. EdPswd->SetFocus();
  89. return;
  90. }
  91. int nRes = GetUserPassword(sUserID, sUserPswd);
  92. if (LOGIN_DB_ERR == nRes)
  93. {
  94. return;
  95. }
  96. if (LOGIN_OK == nRes)
  97. {
  98. m_bLogin = true;
  99. ModalResult = mrOk;
  100. //Close();
  101. }
  102. else
  103. {
  104. String sErrMsg;
  105. m_bLogin = false;
  106. if (LOGIN_ID_ERR == nRes)
  107. {
  108. if (g_sLang == "kr") sErrMsg = "등록되지 않은 사용자 입니다.";
  109. else sErrMsg = "Unregistered user.";//"등록되지 않은 사용자 입니다.";
  110. Application->NormalizeTopMosts();
  111. if (g_sLang == "kr") Application->MessageBox(sErrMsg.c_str(), L"로그인 오류", MB_OK|MB_ICONERROR); // 등록되지 않은 사용자 입니다.
  112. else Application->MessageBox(sErrMsg.c_str(), L"Login Error", MB_OK|MB_ICONERROR); // 등록되지 않은 사용자 입니다.
  113. Application->RestoreTopMosts();
  114. EdUserID->SetFocus();
  115. }
  116. if (LOGIN_PSWD_ERR == nRes)
  117. {
  118. if (g_sLang == "kr") sErrMsg = "비밀번호가 정확하지 않습니다.";
  119. else sErrMsg = "The password is incorrect.";//"비밀번호가 정확하지 않습니다.";
  120. Application->NormalizeTopMosts();
  121. if (g_sLang == "kr") Application->MessageBox(sErrMsg.c_str(), L"로그인 오류", MB_OK|MB_ICONERROR); // 비밀번호가 정확하지 않습니다.
  122. else Application->MessageBox(sErrMsg.c_str(), L"Login Error", MB_OK|MB_ICONERROR); // 비밀번호가 정확하지 않습니다.
  123. Application->RestoreTopMosts();
  124. EdPswd->SetFocus();
  125. }
  126. m_nLoginTryCnt++;
  127. if (g_sLang == "kr") LbMessage->Caption = sErrMsg + "\r\n접속실패 3회면 프로그램이 종료됩니다.\r\n현재 "+String(m_nLoginTryCnt) +" 회";
  128. else LbMessage->Caption = sErrMsg + "\r\nIf the connection fails 3 times, the program ends.\r\nCurrent "+String(m_nLoginTryCnt) +" Trys";
  129. if (m_nLoginTryCnt >= USER_LOGIN_TRY_CNT)
  130. {
  131. Application->NormalizeTopMosts();
  132. if (g_sLang == "kr") Application->MessageBox(L"사용자 로그인 오류 횟수(3회) 초과 오류입니다.", L"로그인 오류", MB_OK|MB_ICONERROR); // 사용자 인증오류입니다. 프로그램을 종료합니다.
  133. else Application->MessageBox(L"Error exceeding user login failure count (3).", L"Login Error", MB_OK|MB_ICONERROR); // 사용자 인증오류입니다. 프로그램을 종료합니다.
  134. Application->RestoreTopMosts();
  135. ModalResult = mrCancel;
  136. //Close();
  137. }
  138. }
  139. }
  140. //---------------------------------------------------------------------------
  141. /*
  142. * 입력된 아이디로 비밀번호를 얻어온다.
  143. * parameter
  144. * strUserID : 사용자 ID
  145. * strPswd : 조회한 사용자 비밀번호
  146. * return
  147. * int : 성공이면 1, 실패면 0
  148. */
  149. int __fastcall TFRMLogin::GetUserPassword(String sUserID, String sPswd)
  150. {
  151. int nRes;
  152. String sTmpPswd;
  153. String sEncPswd;
  154. String sQry;
  155. nRes = LOGIN_OK;
  156. sTmpPswd = "";
  157. if (!ITSDb_IsOpen())
  158. {
  159. if (!ITSDb_Open())
  160. {
  161. Application->NormalizeTopMosts();
  162. if (g_sLang == "kr") Application->MessageBox(L"데이터베이스에 연결할 수 없습니다.", L"데이터베이스 연결 오류", MB_OK|MB_ICONERROR); // 비밀번호를 입력하세요.
  163. else Application->MessageBox(L"Unable to connect to database.", L"Database Connect Error", MB_OK|MB_ICONERROR); // 비밀번호를 입력하세요.
  164. Application->RestoreTopMosts();
  165. EdPswd->SetFocus();
  166. return LOGIN_DB_ERR;
  167. }
  168. }
  169. if (!ITSDb_IsOpen())
  170. {
  171. return LOGIN_DB_ERR;
  172. }
  173. sEncPswd = sPswd;
  174. sEncPswd = sPswd;//String(ITSSHA256_Encrpyt(AnsiString(sPswd)));
  175. sEncPswd = GetPassword(sPswd);
  176. sQry = "SELECT a.*, \r\n"
  177. " a.NAME as levelname, \r\n"
  178. " a.PWD as passwordname, \r\n"
  179. " a.GROP_ID as RIGHTID, \r\n"
  180. " TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') as logintime \r\n"
  181. " FROM TB_USER_INFR a \r\n"
  182. " WHERE USER_ID = :p01 \r\n"
  183. " AND DEL_YN <> 'Y' \r\n";
  184. TADOQuery *pADO = NULL;
  185. try
  186. {
  187. try
  188. {
  189. pADO = new TADOQuery(NULL);
  190. pADO->Connection = ITSDb_GetConnection();
  191. pADO->Close();
  192. pADO->SQL->Text = sQry;
  193. pADO->Parameters->ParamByName("p01")->Value = sUserID;
  194. pADO->Prepared = true;
  195. pADO->Open();
  196. if (!pADO->Eof)
  197. {
  198. pADO->First();
  199. sTmpPswd = pADO->FieldByName("passwordname")->AsString;
  200. if (sTmpPswd != sEncPswd)
  201. {
  202. nRes = LOGIN_PSWD_ERR;
  203. }
  204. else
  205. {
  206. m_sUserID = pADO->FieldByName("USER_ID")->AsString;
  207. m_sUserName = pADO->FieldByName("NAME")->AsString;
  208. m_sUserRightID = pADO->FieldByName("NAME")->AsString;
  209. m_sUserRightName = pADO->FieldByName("levelname")->AsString;
  210. m_sLoginTime = pADO->FieldByName("logintime")->AsString;
  211. }
  212. }
  213. else
  214. {
  215. nRes = LOGIN_ID_ERR;
  216. }
  217. pADO->Close();
  218. }
  219. catch(EDatabaseError &E)
  220. {
  221. nRes = LOGIN_DB_ERR;
  222. throw Exception(String(E.ClassName())+E.Message);
  223. }
  224. catch (Exception &exception)
  225. {
  226. nRes = LOGIN_DB_ERR;
  227. throw Exception(String(exception.ClassName())+exception.Message);
  228. }
  229. catch(...)
  230. {
  231. nRes = LOGIN_DB_ERR;
  232. if (g_sLang == "kr") throw Exception("알수없는 오류가 발생하였습니다.");
  233. else throw Exception("Unknown Error.");
  234. }
  235. }
  236. __finally
  237. {
  238. if (pADO)
  239. {
  240. pADO->Close();
  241. delete pADO;
  242. }
  243. }
  244. return nRes;
  245. }
  246. //---------------------------------------------------------------------------
  247. String __fastcall TFRMLogin::GetPassword(String APswd)
  248. {
  249. int nRes;
  250. String sEncPswd = "";
  251. String sQry;
  252. sQry = "SELECT SCP.HASH_B64('71', :p01) AS PWD FROM DUAL \r\n";
  253. TADOQuery *pADO = NULL;
  254. try
  255. {
  256. try
  257. {
  258. pADO = new TADOQuery(NULL);
  259. pADO->Connection = ITSDb_GetConnection();
  260. pADO->Close();
  261. pADO->SQL->Text = sQry;
  262. pADO->Parameters->ParamByName("p01")->Value = APswd;
  263. pADO->Prepared = true;
  264. pADO->Open();
  265. if (!pADO->Eof)
  266. {
  267. pADO->First();
  268. sEncPswd = pADO->FieldByName("PWD")->AsString;
  269. }
  270. pADO->Close();
  271. }
  272. catch(EDatabaseError &E)
  273. {
  274. nRes = LOGIN_DB_ERR;
  275. throw Exception(String(E.ClassName())+E.Message);
  276. }
  277. catch (Exception &exception)
  278. {
  279. nRes = LOGIN_DB_ERR;
  280. throw Exception(String(exception.ClassName())+exception.Message);
  281. }
  282. catch(...)
  283. {
  284. nRes = LOGIN_DB_ERR;
  285. if (g_sLang == "kr") throw Exception("알수없는 오류가 발생하였습니다.");
  286. else throw Exception("Unknown Error.");
  287. }
  288. }
  289. __finally
  290. {
  291. if (pADO)
  292. {
  293. pADO->Close();
  294. delete pADO;
  295. }
  296. }
  297. return sEncPswd;
  298. }
  299. //---------------------------------------------------------------------------
  300. String __fastcall TFRMLogin::GetLocalIp()
  301. {
  302. char slocal[256];
  303. WSAData wsaData;
  304. if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
  305. return "";
  306. if (gethostname(slocal, 256) == SOCKET_ERROR)
  307. return "";
  308. hostent *hos = gethostbyname(slocal);
  309. if (hos == NULL)
  310. return "";
  311. char *addr = *(hos->h_addr_list);
  312. String LocalIP = String((unsigned char)addr[0]) + '.'
  313. + String((unsigned char)addr[1]) + '.'
  314. + String((unsigned char)addr[2]) + '.'
  315. + String((unsigned char)addr[3]);
  316. WSACleanup();
  317. return LocalIP;
  318. }
  319. //---------------------------------------------------------------------------
  320. void __fastcall TFRMLogin::FormShow(TObject *Sender)
  321. {
  322. if (EdUserID->Text != "")
  323. {
  324. EdPswd->SetFocus();
  325. }
  326. else
  327. {
  328. EdUserID->SetFocus();
  329. }
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TFRMLogin::EdUserIDKeyPress(TObject *Sender, char &Key)
  333. {
  334. if (Key == 13)
  335. {
  336. EdPswd->SetFocus();
  337. }
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TFRMLogin::EdPswdKeyPress(TObject *Sender, char &Key)
  341. {
  342. if (Key == 13)
  343. {
  344. BtnLoginClick(BtnLogin);
  345. }
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TFRMLogin::FormCreate(TObject *Sender)
  349. {
  350. /*
  351. * 스킨 변경
  352. */
  353. Application->ShowMainForm = false;
  354. try {
  355. Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
  356. } catch(...) { ShowMessage("LoadFromResourceName failed"); }
  357. SetLocalSkin();
  358. }
  359. //---------------------------------------------------------------------------
  360. /*
  361. * 공통으로 처리되지 않는 스킨을 변경한다.
  362. * arguments
  363. *
  364. * return
  365. * void
  366. */
  367. void __fastcall TFRMLogin::SetLocalSkin()
  368. {
  369. }
  370. //---------------------------------------------------------------------------
  371. /*
  372. * 캡션타입틀 마우스 클릭시 화면 이동하기
  373. * arguments
  374. *
  375. * return
  376. * void
  377. */
  378. void __fastcall TFRMLogin::Image1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  379. {
  380. ReleaseCapture();
  381. SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
  382. }
  383. //---------------------------------------------------------------------------
  384. void __fastcall TFRMLogin::FormClose(TObject *Sender, TCloseAction &Action)
  385. {
  386. //Action = caFree;
  387. }
  388. //---------------------------------------------------------------------------