FixUsersLongLogin.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_FixUsersLongLogin extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. }
  9. public function defaultAction() {
  10. SE_Layout::gora();
  11. SE_Layout::menu();
  12. $this->_menu();
  13. SE_Layout::dol();
  14. }
  15. private function _menu($selectedMonth) {
  16. $usersTodoList = $this->getUsersWithTooLongLogin();
  17. ?>
  18. <div class="container">
  19. <?php if (empty($usersTodoList)) : ?>
  20. <div class="alert alert-info">
  21. Brak użytwkoników z loginem dłuższym niż 20 znaków
  22. </div>
  23. <?php else : ?>
  24. <table class="table table-bordered table-hovered">
  25. <tr>
  26. <th>Lp.</th>
  27. <th>Nr</th>
  28. <th>Login</th>
  29. <th>Poziom Uprawnień</th>
  30. <th>Status</th>
  31. <th>Data synch.</th>
  32. <th>Nowy login</th>
  33. <th>Update Login and Sync.</th>
  34. </tr>
  35. <?php $i = 0; foreach ($usersTodoList as $user) : $i++ ?>
  36. <?php
  37. if (false !== strpos($user->ADM_ACCOUNT, '.')) {
  38. $newLogin = explode('.', $user->ADM_ACCOUNT, 2);
  39. $newLogin = substr($newLogin[0], 0, 1) . ".{$newLogin[1]}";
  40. if (strlen($newLogin) > 20) {
  41. $newLogin = substr($newLogin, 0, 20);
  42. }
  43. } else {
  44. $newLogin = substr($user->ADM_ACCOUNT, 0, 20);
  45. }
  46. $newLogin = strtolower($newLogin);
  47. ?>
  48. <tr>
  49. <td><?php echo $i; ?></td>
  50. <td><?php echo $user->ID; ?></td>
  51. <td style="font-family:monospace">
  52. <span style=""><?php echo substr($user->ADM_ACCOUNT, 0, 20); ?></span><span style="color:red"><?php echo substr($user->ADM_ACCOUNT, 20); ?></span>
  53. </td>
  54. <td><?php echo $user->ADM_ADMIN_LEVEL; ?></td>
  55. <td><?php echo $user->A_STATUS; ?></td>
  56. <td><?php echo $user->A_SYNC_LDAP_DATE; ?></td>
  57. <td style="font-family:monospace;color:<?php echo ($this->getUserByLogin($newLogin)?'red':'green')?>;"><?php echo $newLogin; ?></td>
  58. <td><a target="_blank"
  59. class="btn btn-xs btn-primary"
  60. title="Aktualizuj Login z <?php echo $user->ADM_ACCOUNT; ?> na <?php echo $newLogin; ?> i uruchom synchronizcję do LDAP"
  61. href="index.php?_route=FixUsersLongLogin&_task=updateLoginAndSync&currentLogin=<?php echo $user->ADM_ACCOUNT; ?>&newLogin=<?php echo $newLogin; ?>">Update and sync.</a></td>
  62. </tr>
  63. <?php endforeach; ?>
  64. </table>
  65. <?php endif; ?>
  66. </div>
  67. <?php
  68. }
  69. public function getUsersWithTooLongLogin() {
  70. $usersTodoList = array();
  71. $db = DB::getDB();
  72. $sql = <<<SQL
  73. select `ID`, `ADM_ACCOUNT`, `A_STATUS`
  74. , `ADM_ADMIN_LEVEL`
  75. , `A_SYNC_LDAP_DATE`
  76. from `ADMIN_USERS`
  77. where LENGTH(`ADM_ACCOUNT`) > 20
  78. SQL;
  79. $res = $db->query($sql);
  80. while ($r = $db->fetch($res)) {
  81. $usersTodoList[$r->ADM_ACCOUNT] = $r;
  82. }
  83. return $usersTodoList;
  84. }
  85. public function getUserByLogin($login) {
  86. $user = null;
  87. $db = DB::getDB();
  88. $sqlLogin = $db->_($login);
  89. $sql = <<<SQL
  90. select `ID`, `ADM_ACCOUNT`, `A_STATUS`
  91. , `ADM_ADMIN_LEVEL`
  92. , `A_SYNC_LDAP_DATE`
  93. from `ADMIN_USERS`
  94. where `ADM_ACCOUNT`='{$sqlLogin}'
  95. SQL;
  96. $res = $db->query($sql);
  97. if ($r = $db->fetch($res)) {
  98. $user = $r;
  99. }
  100. return $user;
  101. }
  102. public function updateLoginAndSyncAction() {
  103. $currentLogin = V::get('currentLogin', '', $_REQUEST, 'word');
  104. $newLogin = V::get('newLogin', '', $_REQUEST, 'word');
  105. $user = $this->getUserByLogin($currentLogin);
  106. SE_Layout::gora();
  107. ?>
  108. <div class="container">
  109. <?php if (!$user) : ?>
  110. <div class="alert alert-danger">
  111. Brak użytkownika z loginem <?php echo $currentLogin; ?>
  112. </div>
  113. <?php else : ?>
  114. <div class="alert alert-info">
  115. Zmiana loginu z <?php echo $currentLogin; ?> na <?php echo $newLogin; ?>
  116. </div>
  117. <?php
  118. try {
  119. $affected = $this->updateLogin($user, $currentLogin, $newLogin);
  120. if ($affected == 2) {
  121. echo '<div class="alert alert-success">' . "Login został poprawnie zmieniony na '{$newLogin}'" . '</div>';
  122. } else if ($affected == 1) {
  123. echo '<div class="alert alert-warning">' . "Nie zapisano hist" . '</div>';
  124. } else if ($affected == 0) {
  125. echo '<div class="alert alert-info">' . "Nic nie zmieniono" . '</div>';
  126. }
  127. if ($affected) $this->updateDatabase($currentLogin, $newLogin);
  128. } catch (Exception $e) { ?>
  129. <div class="alert alert-danger">
  130. <?php echo $e->getMessage(); ?>
  131. </div>
  132. <?php } ?>
  133. <a class="btn btn-primary"
  134. href="index.php?MENU_INIT=SYNC_LDAP_PERMS&syncUsr=<?php echo $newLogin; ?>">Synchronizuj do LDAP (<?php echo $newLogin; ?>)</a>
  135. <?php endif; ?>
  136. </div>
  137. <?php
  138. SE_Layout::dol();
  139. }
  140. public function updateLogin($user, $currentLogin, $newLogin) {
  141. $db = DB::getDB();
  142. if ($currentLogin != $user->ADM_ACCOUNT) {
  143. throw new Exception("Error: User ADM_ACCOUNT != '{$currentLogin}'");
  144. }
  145. $sqlObj = new stdClass();
  146. $sqlObj->ID = $user->ID;
  147. $sqlObj->ADM_ACCOUNT = $newLogin;
  148. $affected = $db->UPDATE_OBJ('ADMIN_USERS', $sqlObj);
  149. if ($affected < 0) {
  150. if ($db->has_errors()) {
  151. $dsErrors = array();
  152. $errorsSql = $db->get_errors();
  153. foreach ($errorsSql as $vErr) {
  154. if ('SQL QUERY FAILED: ' == substr($vErr, 0, 18)) {
  155. $vErr = substr($vErr, 18);
  156. }
  157. $dsErrors[] = $vErr;
  158. }
  159. if (!empty($dsErrors)) {
  160. Lib::loadClass('StorageException');
  161. throw new StorageException($dsErrors);
  162. }
  163. } else {
  164. throw new Exception("Error: nic nie zmieniono");
  165. }
  166. }
  167. return $affected;
  168. }
  169. public function updateDatabase($currentLogin, $newLogin) {
  170. $db = DB::getDB();
  171. $columns = Array("L_APPOITMENT_USER","A_RECORD_CREATE_AUTHOR","A_RECORD_UPDATE_AUTHOR");
  172. $dbName = $db->getDatabaseName();
  173. echo '<div class="alert alert-info" role="alert">';
  174. foreach ($columns as $column) {
  175. $sql = <<<SQL
  176. select cols.`TABLE_NAME`
  177. from `INFORMATION_SCHEMA`.`COLUMNS` cols
  178. join `INFORMATION_SCHEMA`.`TABLES` tabs
  179. on cols.TABLE_SCHEMA=tabs.TABLE_SCHEMA
  180. and cols.TABLE_NAME=tabs.TABLE_NAME
  181. and tabs.TABLE_TYPE!='VIEW'
  182. where cols.`TABLE_SCHEMA`='{$dbName}'
  183. and cols.`COLUMN_NAME`='{$column}'
  184. SQL;
  185. $res = $db->query($sql);
  186. while ($r = $db->fetch($res)) {
  187. $tableName = $r->TABLE_NAME;
  188. $sql = <<<SQL
  189. update `{$dbName}`.`{$tableName}` tab
  190. set tab.`{$column}`='{$newLogin}'
  191. where tab.`{$column}`='{$currentLogin}'
  192. SQL;
  193. echo 'Aktualizowanie kolumny `'.$column.'`. w tabeli `'.$tableName.'`... ';
  194. try {
  195. $db->query($sql);
  196. $affected=$db->affected_rows();
  197. $affected_all+=$affected;
  198. if ($affected) {
  199. echo '<span style="color:green">Zaktualizowano '.$affected.' rekordów.</span>';
  200. } else {
  201. echo '<span style="color:red">Nie zaktualizowano żadnego rekordu.</span>';
  202. }
  203. echo "<br>";
  204. } catch (Exception $e) { ?>
  205. <div class="alert alert-danger">
  206. <?php echo $e->getMessage(); ?>
  207. </div>
  208. <?php }
  209. }
  210. }
  211. echo "<br>Łącznie zaktualizowano {$affected_all} rekordów.";
  212. echo "</div>";
  213. }
  214. }