auth.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. $item['P_PESEL'] = trim(str_replace(' ', '', $item['P_PESEL']));
  72. $item['P_NIP'] = trim(str_replace([' ', '-'], '', $item['P_NIP']));
  73. $passwd = (!empty($item['P_PESEL'])) ? $item['P_PESEL'] : $item['P_NIP'];
  74. $hashPass = ($passwd) ? md5($passwd) : null;
  75. DB::getPDO()->insert('PANEL_KLIENTA_BN_AUTH', [
  76. 'ID_BILLING_USERS' => $item['ID'],
  77. 'LOGIN' => $item['user_mail_contact'],
  78. 'PASSWD' => $hashPass,
  79. 'A_RECORD_CREATE_DATE' => "NOW()",
  80. ]);
  81. $item['ID_BILLING_USERS'] = $item['ID'];
  82. $item['LOGIN'] = $item['user_mail_contact'];
  83. $item['PASSWD'] = $hashPass;
  84. }
  85. return $item;
  86. }
  87. static function generateRemindKey($email) {
  88. $remindKey = substr(md5($email . "" . date("Y-m-d H:i:s")), 0, 16);
  89. $remindTill = date("Y-m-d", mktime(0,0,0, date("m"), date("d") + 2, date("Y")));
  90. $userInfo = self::fetchUser($email);
  91. DB::getPDO()->execSql("
  92. update PANEL_KLIENTA_BN_AUTH
  93. set REMIND_PASS_KEY = :remind_key
  94. , REMIND_PASS_VALID_TILL = :remind_till
  95. , A_RECORD_UPDATE_DATE = NOW()
  96. where ID_BILLING_USERS = :id_user
  97. and LOGIN = :login
  98. ", [
  99. ':id_user' => $userInfo['ID'],
  100. ':login' => $userInfo['LOGIN'],
  101. ':remind_key' => $remindKey,
  102. ':remind_till' => $remindTill,
  103. ]);
  104. return $remindKey;
  105. }
  106. static function setPasswd($email, $newPasswd, $remindKey) {
  107. if (empty($email)) throw new Exception("Missing login!");
  108. if (empty($newPasswd)) throw new Exception("Missing password!");
  109. if (empty($remindKey)) throw new Exception("Missing remindKey!");
  110. // TODO: validate password!
  111. if (strlen($newPasswd) < 8) throw new Exception("Hasło musi się składać z co najmniej 8 znaków");
  112. $userInfo = self::fetchUser($email);
  113. // DBG::nicePrint([
  114. // 'c1' => empty($userInfo['REMIND_PASS_KEY']),
  115. // 'c2' => $userInfo['REMIND_PASS_KEY'] !== $remindKey,
  116. // 'c2.L' => $userInfo['REMIND_PASS_KEY'],
  117. // 'c2.R' => $remindKey,
  118. // 'c3' => date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL'],
  119. // 'c3.L' => date("Y-m-d"),
  120. // 'c3.R' => $userInfo['REMIND_PASS_VALID_TILL'],
  121. // 'user' => $userInfo,
  122. // ], 'DBG');
  123. if (empty($userInfo['REMIND_PASS_KEY'])
  124. || $userInfo['REMIND_PASS_KEY'] !== $remindKey
  125. || date("Y-m-d") > $userInfo['REMIND_PASS_VALID_TILL']
  126. ) throw new Exception("Odnośnik do resetowania hasła wydaje się być niesprawny. Proszę użyć funkcji przypomnienia hasła.");
  127. DB::getPDO()->execSql("
  128. update PANEL_KLIENTA_BN_AUTH
  129. set REMIND_PASS_KEY = ''
  130. , REMIND_PASS_VALID_TILL = '0000-00-00'
  131. , PASSWD = :hash_passwd
  132. , A_RECORD_UPDATE_DATE = NOW()
  133. where ID_BILLING_USERS = :id_user
  134. and LOGIN = :login
  135. ", [
  136. ':id_user' => $userInfo['ID'],
  137. ':login' => $userInfo['LOGIN'],
  138. ':hash_passwd' => md5($newPasswd),
  139. ]);
  140. }
  141. static function preparePanelBNAuthTables() {
  142. DB::getPDO()->execSql("
  143. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH` (
  144. `ID` int(11) NOT NULL AUTO_INCREMENT,
  145. `ID_BILLING_USERS` int(11) NOT NULL,
  146. `LOGIN` varchar(255) NOT NULL DEFAULT '',
  147. `PASSWD` varchar(32) NOT NULL DEFAULT '',
  148. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT '',
  149. `REMIND_PASS_VALID_TILL` date NOT NULL DEFAULT '0000-00-00',
  150. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT '',
  151. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT '',
  152. `A_RECORD_CREATE_DATE` datetime NOT NULL,
  153. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  154. `A_RECORD_UPDATE_DATE` datetime NOT NULL,
  155. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT '',
  156. PRIMARY KEY (`ID`),
  157. UNIQUE KEY `COMPANY_LOGIN` (`LOGIN`, `ID_BILLING_USERS`)
  158. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  159. ");
  160. DB::getPDO()->execSql("
  161. CREATE TABLE IF NOT EXISTS `PANEL_KLIENTA_BN_AUTH_HIST` (
  162. `ID` int(11) NOT NULL AUTO_INCREMENT,
  163. `ID_USERS2` int(11) NOT NULL,
  164. `ID_BILLING_USERS` varchar(11) NOT NULL DEFAULT 'N/S;',
  165. `LOGIN` varchar(255) NOT NULL DEFAULT 'N/S;',
  166. `PASSWD` varchar(32) NOT NULL DEFAULT 'N/S;',
  167. `REMIND_PASS_KEY` varchar(16) NOT NULL DEFAULT 'N/S;',
  168. `REMIND_PASS_VALID_TILL` varchar(10) NOT NULL DEFAULT 'N/S;',
  169. `A_ADM_COMPANY` varchar(64) NOT NULL DEFAULT 'N/S;',
  170. `A_CLASSIFIED` varchar(64) NOT NULL DEFAULT 'N/S;',
  171. `A_RECORD_CREATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  172. `A_RECORD_CREATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  173. `A_RECORD_UPDATE_DATE` varchar(10) NOT NULL DEFAULT 'N/S;',
  174. `A_RECORD_UPDATE_AUTHOR` varchar(20) NOT NULL DEFAULT 'N/S;',
  175. PRIMARY KEY (`ID`),
  176. KEY `ID_USERS2` (`ID_USERS2`)
  177. ) ENGINE=MyISAM DEFAULT CHARSET=latin2 ;
  178. ");
  179. }
  180. }