FixUsersLongLogin.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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: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.</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. } catch (Exception $e) { ?>
  127. <div class="alert alert-danger">
  128. <?php echo $e->getMessage(); ?>
  129. </div>
  130. <?php } ?>
  131. <a class="btn btn-primary"
  132. href="index.php?MENU_INIT=SYNC_LDAP_PERMS&syncUsr=<?php echo $newLogin; ?>">Synchronizuj do LDAP (<?php echo $newLogin; ?>)</a>
  133. <?php endif; ?>
  134. </div>
  135. <?php
  136. SE_Layout::dol();
  137. }
  138. public function updateLogin($user, $currentLogin, $newLogin) {
  139. $db = DB::getDB();
  140. if ($currentLogin != $user->ADM_ACCOUNT) {
  141. throw new Exception("Error: User ADM_ACCOUNT != '{$currentLogin}'");
  142. }
  143. $sqlObj = new stdClass();
  144. $sqlObj->ID = $user->ID;
  145. $sqlObj->ADM_ACCOUNT = $newLogin;
  146. $affected = $db->UPDATE_OBJ('ADMIN_USERS', $sqlObj);
  147. if ($affected < 0) {
  148. if ($db->has_errors()) {
  149. $dsErrors = array();
  150. $errorsSql = $db->get_errors();
  151. foreach ($errorsSql as $vErr) {
  152. if ('SQL QUERY FAILED: ' == substr($vErr, 0, 18)) {
  153. $vErr = substr($vErr, 18);
  154. }
  155. $dsErrors[] = $vErr;
  156. }
  157. if (!empty($dsErrors)) {
  158. Lib::loadClass('StorageException');
  159. throw new StorageException($dsErrors);
  160. }
  161. } else {
  162. throw new Exception("Error: nic nie zmieniono");
  163. }
  164. }
  165. return $affected;
  166. }
  167. }