auth.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // rename file to: `config.php`
  3. // @used by User class to login
  4. // - [ ] use to fetch user groups for acl
  5. /**
  6. * User object:
  7. * - ID // ADM_ID
  8. * - ADM_ACCOUNT // AUTHORIZE_USER, ADM_ACCOUNT
  9. * - ADM_NAME // ADM_NAME
  10. * - ADM_TECH_WORKER // ADM_TECH_WORKER
  11. * - ADM_COMPANY // ADM_COMPANY
  12. * - ADM_ADMIN_LEVEL // ADM_ADMIN_LEVEL
  13. * - ADM_PHONE // ADM_PHONE
  14. * - ADM_ADMIN_EXPIRE // ADM_ADMIN_EXPIRE
  15. * - ADM_ADMIN_DESC // ADM_ADMIN_DESC
  16. * - EMPLOYEE_TYPE // EMPLOYEE_TYPE
  17. * - EMAIL_IMAP_IMPORT_HOST // EMAIL_IMAP_IMPORT_HOST
  18. * - EMAIL_IMAP_IMPORT_USERNAME // EMAIL_IMAP_IMPORT_USERNAME
  19. */
  20. class Theme_Auth_panel_biall_net { // TODO: implements AuthBaseInterface {
  21. static function login($login, $pass) { // @return User object or null
  22. if (empty($login) || empty($pass)) throw new Exception("Proszę podać login i hasło!");
  23. // { // TODO: TEST
  24. // $login = "krzys.dworski@gmail.com"; // COMPANIES.user_mail_contact
  25. // $pass = "76022801989"; // COMPANIES.P_PESEL or COMPANIES.PASSWD varchar(100) --- table PANEL_KLIENTA_BN_AUTH
  26. // }
  27. $item = self::fetchUser($login);
  28. if (empty($item['PASSWD']) || 32 != strlen($item['PASSWD'])) {
  29. throw new Exception("Proszę użyć funkcji przypomnienia hasła");
  30. }
  31. if (md5($pass) !== $item['PASSWD']) {
  32. throw new Exception("Proszę podać poprawny login i hasło!");
  33. }
  34. return (object)[
  35. 'ID' => $item['ID'],
  36. 'ADM_ACCOUNT' => $item['LOGIN'], // AUTHORIZE_USER, ADM_ACCOUNT
  37. 'ADM_NAME' => implode(" ", [ $item['P_NAME'], $item['P_NAME_SECOND'] ]), // ADM_NAME
  38. 'ADM_TECH_WORKER' => "", // ADM_TECH_WORKER
  39. 'ADM_COMPANY' => $item['BILLING_OWNER'], // ADM_COMPANY
  40. 'ADM_ADMIN_LEVEL' => 5, // ADM_ADMIN_LEVEL // > 5 will show msg for Kandydat
  41. 'ADM_PHONE' => "", // ADM_PHONE
  42. 'ADM_ADMIN_EXPIRE' => "", // ADM_ADMIN_EXPIRE
  43. 'ADM_ADMIN_DESC' => "", // ADM_ADMIN_DESC
  44. 'EMPLOYEE_TYPE' => "Klient", // EMPLOYEE_TYPE // [ 'Pracownik','Kandydat','Partner','Anonymous','Kontakt','Skrypt' ]
  45. ];
  46. }
  47. static function fetchUser($login) {
  48. $item = DB::getPDO()->tryHandleException([ __CLASS__, 'preparePanelBNAuthTables' ], 'fetchFirst', [
  49. "
  50. select c.ID, c.user_mail_contact, c.P_PESEL
  51. -- , c.PASSWD
  52. , c.P_NAME, c.P_NAME_SECOND
  53. , c.BILLING_OWNER -- 1 BN, 3 NETDAY
  54. , c.is_firma
  55. , c.P_NIP
  56. , p.ID as ID_AUTH
  57. , p.LOGIN
  58. , p.PASSWD
  59. , p.REMIND_PASS_KEY
  60. , p.REMIND_PASS_VALID_TILL
  61. from COMPANIES c
  62. left join PANEL_KLIENTA_BN_AUTH p on ( p.ID_BILLING_USERS = c.ID and p.LOGIN = c.user_mail_contact )
  63. where c.user_mail_contact like :email
  64. ",
  65. [
  66. ':email' => $login,
  67. ]
  68. ]);
  69. if (!$item) throw new Exception("BŁĄD: Brak zarejestrowanego użytkownika o wprowadzonym adresie email.");
  70. if (!$item['ID_AUTH']) {
  71. $passwd = (!empty($item['P_PESEL'])) ? $item['P_PESEL'] : $item['P_NIP'];
  72. $hashPass = ($passwd) ? md5($passwd) : null;
  73. DB::getPDO()->insert('PANEL_KLIENTA_BN_AUTH', [
  74. 'ID_BILLING_USERS' => $item['ID'],
  75. 'LOGIN' => $item['user_mail_contact'],
  76. 'PASSWD' => $hashPass,
  77. 'A_RECORD_CREATE_DATE' => "NOW()",
  78. ]);
  79. $item['ID_BILLING_USERS'] = $item['ID'];
  80. $item['LOGIN'] = $item['user_mail_contact'];
  81. $item['PASSWD'] = $hashPass;
  82. }
  83. return $item;
  84. }
  85. static function generateRemindKey($email) {
  86. $remindKey = substr(md5($email . "" . date("Y-m-d H:i:s")), 0, 16);
  87. $remindTill = date("Y-m-d", mktime(0,0,0, date("m"), date("d") + 2, date("Y")));
  88. $userInfo = self::fetchUser($email);
  89. DB::getPDO()->execSql("
  90. update PANEL_KLIENTA_BN_AUTH
  91. set REMIND_PASS_KEY = :remind_key
  92. , REMIND_PASS_VALID_TILL = :remind_till
  93. , A_RECORD_UPDATE_DATE = NOW()
  94. where ID_BILLING_USERS = :id_user
  95. and LOGIN = :login
  96. ", [
  97. ':id_user' => $userInfo['ID'],
  98. ':login' => $userInfo['LOGIN'],
  99. ':remind_key' => $remindKey,
  100. ':remind_till' => $remindTill,
  101. ]);
  102. return $remindKey;
  103. }
  104. static function setPasswd($email, $newPasswd, $remindKey) {
  105. if (empty($email)) throw new Exception("Missing login!");
  106. if (empty($newPasswd)) throw new Exception("Missing password!");
  107. if (empty($remindKey)) throw new Exception("Missing remindKey!");
  108. // TODO: validate password!
  109. if (strlen($newPasswd) < 8) throw new Exception("Hasło musi się składać z co najmniej 8 znaków");
  110. $userInfo = self::fetchUser($email);
  111. // DBG::nicePrint([
  112. // 'c1' => empty($userInfo['REMIND_PASS_KEY']),
  113. // 'c2' => $userInfo['REMIND_PASS_KEY'] !== $remindKey,
  114. // 'c2.L' => $userInfo['REMIND_PASS_KEY'],
  115. // 'c2.R' => $remindKey,
  116. // 'c3' => date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL'],
  117. // 'c3.L' => date("Y-m-d"),
  118. // 'c3.R' => $userInfo['REMIND_PASS_VALID_TILL'],
  119. // 'user' => $userInfo,
  120. // ], 'DBG');
  121. if (empty($userInfo['REMIND_PASS_KEY'])
  122. || $userInfo['REMIND_PASS_KEY'] !== $remindKey
  123. || date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL']
  124. ) throw new Exception("Odnośnik do resetowania hasła wydaje się być niesprawny. Proszę użyć funkcji przypomnienia hasła.");
  125. DB::getPDO()->execSql("
  126. update PANEL_KLIENTA_BN_AUTH
  127. set REMIND_PASS_KEY = ''
  128. , REMIND_PASS_VALID_TILL = '0000-00-00'
  129. , PASSWD = :hash_passwd
  130. , A_RECORD_UPDATE_DATE = NOW()
  131. where ID_BILLING_USERS = :id_user
  132. and LOGIN = :login
  133. ", [
  134. ':id_user' => $userInfo['ID'],
  135. ':login' => $userInfo['LOGIN'],
  136. ':hash_passwd' => md5($newPasswd),
  137. ]);
  138. }
  139. static function preparePanelBNAuthTables() {
  140. DB::getPDO()->execSql("
  141. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH` (
  142. `ID` int(11) NOT NULL AUTO_INCREMENT,
  143. `ID_BILLING_USERS` int(11) NOT NULL,
  144. `LOGIN` varchar(255) NOT NULL DEFAULT '',
  145. `PASSWD` varchar(32) NOT NULL DEFAULT '',
  146. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT '',
  147. `REMIND_PASS_VALID_TILL` date NOT NULL DEFAULT '0000-00-00',
  148. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT '',
  149. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT '',
  150. `A_RECORD_CREATE_DATE` datetime NOT NULL,
  151. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  152. `A_RECORD_UPDATE_DATE` datetime NOT NULL,
  153. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  154. PRIMARY KEY (`ID`),
  155. UNIQUE KEY `COMPANY_LOGIN` (`LOGIN`, `ID_BILLING_USERS`)
  156. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  157. ");
  158. DB::getPDO()->execSql("
  159. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH_HIST` (
  160. `ID` int(11) NOT NULL AUTO_INCREMENT,
  161. `ID_USERS2` int(11) NOT NULL,
  162. `ID_BILLING_USERS` varchar(11) NOT NULL DEFAULT 'N/S;',
  163. `LOGIN` varchar(255) NOT NULL DEFAULT 'N/S;',
  164. `PASSWD` varchar(32) NOT NULL DEFAULT 'N/S;',
  165. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT 'N/S;',
  166. `REMIND_PASS_VALID_TILL` varchar(10) NOT NULL DEFAULT 'N/S;',
  167. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT 'N/S;',
  168. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT 'N/S;',
  169. `A_RECORD_CREATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  170. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  171. `A_RECORD_UPDATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  172. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  173. PRIMARY KEY (`ID`),
  174. KEY `ID_USERS2` (`ID_USERS2`)
  175. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  176. ");
  177. }
  178. }