Auth.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('ProcesHelper');
  4. Lib::loadClass('Config');
  5. class Route_Auth extends RouteBase {
  6. public function handleAuth() {
  7. if (!User::logged()) {
  8. // throw new HttpException('Unauthorized', 401);
  9. }
  10. }
  11. public function defaultAction() {
  12. SE_Layout::gora();
  13. if (!User::logged()) {
  14. ?>
  15. <div class="container">
  16. <h1>Unauthorized</h1>
  17. <ul>
  18. <li><a href="index.php?_route=Auth&_task=register">Register</a></li>
  19. </ul>
  20. </div>
  21. <?php
  22. } else {
  23. ?>
  24. <div class="container">
  25. <h1>Auth</h1>
  26. ...
  27. </div>
  28. <?php
  29. }
  30. SE_Layout::dol();
  31. }
  32. public function registerAction() {
  33. SE_Layout::gora();
  34. $formFields = array();
  35. $overrideLabels = array();
  36. $formFields[] = 'ADM_NAME';// Imię i nazwisko
  37. $formFields[] = 'EMAIL';// Adres e-mail
  38. $formFields[] = 'ADM_PASSWD';// Hasło
  39. // Potwierdź hasło
  40. // TODO: stanowisko - if allowed (TODO: in config? '.cnf--auth-{host}.ini.php')
  41. $overrideLabels['ADM_NAME'] = 'Imię i nazwisko';
  42. $overrideLabels['EMAIL'] = "Email";
  43. $overrideLabels['ADM_PASSWD'] = "Hasło";
  44. $tblAcl = $this->_getUsersTableAcl($formFields, $overrideLabels);
  45. //$dataSource = $tblAcl->getDataSource();
  46. $cols = array();
  47. $fieldsList = array();
  48. $fieldsListAll = $tblAcl->getFields();
  49. foreach ($formFields as $vColName) {
  50. $vColID = $tblAcl->getFieldIdByName($vColName);
  51. if (isset($fieldsListAll[$vColID])) {
  52. $fieldsList[$vColID] = $fieldsListAll[$vColID];
  53. $cols[$vColID] = '';
  54. //$cols[$kID] = V::get($vCol['name'], '', $row);
  55. $cols[$vColID] = V::get("f{$vColID}", $cols[$vColID], $_POST);
  56. }
  57. else {
  58. ?>
  59. <div class="alert alert-danger">
  60. <h4>Error!</h4>
  61. Brak uprawnień do pola <?php echo $vColName; ?>
  62. </div>
  63. <?php
  64. }
  65. }
  66. DBG::_(true, true, "cols", $cols, __CLASS__, __FUNCTION__, __LINE__);
  67. $tableHash = 'routeAuthRegister';
  68. ?>
  69. <div class="container AjaxFrmHorizontalEdit">
  70. <div id="CREATE_FRM_<?php echo $tableHash; ?>_MSGS"></div>
  71. <form class="form-horizontal" action="" method="post" id="CREATE_FRM_<?php echo $tableHash; ?>">
  72. <fieldset>
  73. <legend>Zarejestruj</legend>
  74. <?php $tabindex = 0; foreach ($fieldsList as $kID => $vCol) : ?>
  75. <?php if ($tblAcl->isAllowed($kID, 'C')) : ?>
  76. <div class="form-group">
  77. <label class="col-xs-12 col-sm-3 col-md-2 control-label" for="<?php echo "f{$kID}"; ?>"><?php echo $vCol['label']; ?>
  78. <i class="glyphicon glyphicon-info-sign frm-help" data-toggle="popover" data-trigger="hover" title="" data-content="<?php echo htmlspecialchars($vCol['opis']); ?>" data-original-title="<?php echo "[{$kID}] {$vCol['name']}"; ?>"></i>
  79. <?php $perms = $tblAcl->getFieldPerms($kID); SE_Layout::hotKeyDBG($perms); ?>
  80. </label>
  81. <div class="col-xs-12 col-sm-9 col-md-10">
  82. <?php
  83. $fieldParams = array('appendBack'=>true, 'tabindex'=>(++$tabindex), 'maxGrid'=>8);
  84. echo $tblAcl->showFormItem('C', $kID, "f{$kID}", $cols[$kID], $fieldParams);
  85. ?>
  86. </div>
  87. </div>
  88. <?php endif; ?>
  89. <?php endforeach; ?>
  90. <div class="form-group">
  91. <div class="col-xs-offset-0 col-xs-12 col-sm-offset-3 col-sm-9 col-md-offset-2 col-md-10">
  92. <button type="submit" class="btn btn-primary" tabindex="<?php echo (++$tabindex); ?>">Zarejestruj</button>
  93. </div>
  94. </div>
  95. </fieldset>
  96. </form>
  97. </div>
  98. <script>
  99. jQuery(document).ready(function(){
  100. jQuery('textarea').autosize();
  101. jQuery('.frm-help').popover({trigger:'hover'});
  102. jQuery('#CREATE_FRM_<?php echo $tableHash; ?>').on('submit', function(e) {
  103. var data = $(this).serialize(),
  104. formNode = $('#CREATE_FRM_<?php echo $tableHash; ?>'),
  105. msgsNode = $('#CREATE_FRM_<?php echo $tableHash; ?>_MSGS')
  106. ;
  107. $.ajax({
  108. data: data,
  109. dataType: 'json',
  110. type: "POST",
  111. async: true,
  112. url: 'index.php?_route=Auth&_task=createSave'
  113. })
  114. .always(function(dataOrJqXHR){
  115. var data;
  116. if (dataOrJqXHR && 'readyState' in dataOrJqXHR && 'status' in dataOrJqXHR) {
  117. if ('responseJSON' in dataOrJqXHR) {
  118. data = dataOrJqXHR.responseJSON;
  119. } else {
  120. data = {};
  121. data.msg = dataOrJqXHR.responseText || 'Nieznany błąd';
  122. if (dataOrJqXHR.status == 404) {
  123. data.type = 'error';
  124. } else {
  125. data.type = 'warning';
  126. }
  127. }
  128. } else {
  129. data = dataOrJqXHR;
  130. }
  131. console.log('L.<?php echo __LINE__; ?> data', data);
  132. if (data.type == 'error' || data.type == 'warning') {
  133. var out = '<div class="container">';
  134. out += '<div class="alert alert-danger">' +
  135. '<h4>Wystąpiły błędy!</h4>' + data.msg +
  136. (('errors' in data)? '<p>' + data.errors + '</p>' : '') +
  137. '</div>';
  138. out += '</div>';
  139. // TODO: show all fields errors
  140. $(out).appendTo(msgsNode);
  141. }
  142. else if (data.type == 'success') {
  143. var msg = '';
  144. if (data.id && data.id > 0) {
  145. msg = 'Utworzono pomyślnie konto w systemie';
  146. } else if (data.msg) {
  147. msg = data.msg;
  148. } else {
  149. msg = 'OK';
  150. }
  151. var out = '<div class="container">';
  152. out += '<div class="alert alert-success">' + msg + '</div>';
  153. out += '</div>';
  154. $(out).appendTo(msgsNode);
  155. formNode.hide();
  156. }
  157. });
  158. console.log('L.<?php echo __LINE__; ?>');return false;
  159. return false;
  160. });
  161. });
  162. </script>
  163. <?php
  164. SE_Layout::dol();
  165. }
  166. public function _getUsersTableAcl($formFields, $overrideLabels = array()) {
  167. $idTable = ProcesHelper::getZasobTableID('ADMIN_USERS');
  168. if (!$idTable) throw new Exception("Brak id tabeli");
  169. //DBG::_(true, true, "idTable", $idTable, __CLASS__, __FUNCTION__, __LINE__);
  170. $userAcl = User::getAcl();
  171. //DBG::_(true, true, "userAcl", $userAcl, __CLASS__, __FUNCTION__, __LINE__);
  172. if (!$userAcl->hasTableAcl($idTable)) {
  173. // .cnf--auth-{host}.ini.php
  174. $conf = Config::getConfFile('auth');
  175. if (!$conf) throw new Exception("Config file for 'auth' not found!");
  176. $isRegisterAllowed = V::get('allow_register', false, $conf);
  177. //DBG::_(true, true, "conf (isRegisterAllowed={$isRegisterAllowed})", $conf, __CLASS__, __FUNCTION__, __LINE__);
  178. if (!$isRegisterAllowed) throw new Exception("Brak uprawnień do rejestracji");
  179. $zasobTblInfo = ProcesHelper::getZasobTableInfoByUri($tblUri = "default_db/ADMIN_USERS");
  180. if (!$zasobTblInfo) throw new HttpException("Brak zasobu dla tabeli użytkowników", 404);
  181. //DBG::_(true, true, "zasobTblInfo", $zasobTblInfo, __CLASS__, __FUNCTION__, __LINE__);
  182. {
  183. $tableConfig = array();
  184. $tableConfig['ID_TABLE'] = $idTable;
  185. $tableConfig['db'] = $zasobTblInfo->P__ID;
  186. $tableConfig['name'] = $zasobTblInfo->DESC;
  187. $tableConfig['label'] = $zasobTblInfo->DESC_PL;
  188. $tableConfig['opis'] = $zasobTblInfo->OPIS;
  189. //DBG::_(true, true, "formFields", $formFields, __CLASS__, __FUNCTION__, __LINE__);
  190. //DBG::_(true, true, "tableConfig", $tableConfig, __CLASS__, __FUNCTION__, __LINE__);
  191. }
  192. {
  193. $fieldsConfig = array();
  194. $fldsInfo = ProcesHelper::getZasobTableFieldsInfo($idTable);
  195. //DBG::_(true, true, "fldsInfo", $fldsInfo, __CLASS__, __FUNCTION__, __LINE__);
  196. foreach ($fldsInfo as $fldInfo) {
  197. if (!in_array($fldInfo->DESC, $formFields)) continue;
  198. $fldConf = array();
  199. $fldConf['ID_CELL'] = $fldInfo->ID;
  200. $fldConf['CELL_NAME'] = $fldInfo->DESC;
  201. $fldConf['CELL_DESC'] = (array_key_exists($fldInfo->DESC, $overrideLabels))? $overrideLabels[$fldInfo->DESC] : $fldInfo->OPIS;
  202. $fldConf['CELL_LABEL'] = (array_key_exists($fldInfo->DESC, $overrideLabels))? $overrideLabels[$fldInfo->DESC] : $fldInfo->DESC_PL;
  203. $fldConf['SORT_PRIO'] = $fldInfo->SORT_PRIO;
  204. $fldConf['FORM_TREAT'] = 'RWXC';
  205. $fieldsConfig[$fldInfo->ID] = $fldConf;
  206. }
  207. //DBG::_(true, true, "fieldsConfig", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  208. }
  209. {// TODO: init and save in session default perms
  210. DBG::_(true, true, "ses tbl cache[{$idTable}]", $_SESSION['TableAcl_cache'][$idTable], __CLASS__, __FUNCTION__, __LINE__);
  211. $tblAcl = TableAcl::buildInstance($idTable, $tableConfig);
  212. $tblAcl->initFieldsFromConfig($fieldsConfig);
  213. $tblAcl->save();
  214. DBG::_(true, true, "ses tbl cache[{$idTable}]", $_SESSION['TableAcl_cache'][$idTable], __CLASS__, __FUNCTION__, __LINE__);
  215. }
  216. //throw new Exception("Brak uprawnień do tabeli ID={$idTable}");
  217. }
  218. //DBG::_(true, true, "_SESSION['UserAcl_cache']['foundTables']", $_SESSION['UserAcl_cache']['foundTables'], __CLASS__, __FUNCTION__, __LINE__);
  219. //$tblAcl = $userAcl->getTableAcl($idTable);
  220. if (!$tblAcl) throw new Exception("Brak tabeli");
  221. $tblAcl->init();
  222. DBG::_(true, true, "tblAcl", $tblAcl, __CLASS__, __FUNCTION__, __LINE__);
  223. return $tblAcl;
  224. }
  225. }