FRMLoginF.cpp 13 KB

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