FixUsersLongLogin.php 6.7 KB

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