FRMLoginF.cpp 14 KB

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