FRMLoginF.cpp 13 KB

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