FRMLoginF.cpp 12 KB

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