users.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Users List</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1" >
  7. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  8. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css" />
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.js"></script>
  10. <style>
  11. body{
  12. background-color: rgb(243, 243, 243);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <br>
  18. <div class="ui container">
  19. <div class="ui segment">
  20. <h4 class="ui header">
  21. <img src="img/user.svg">
  22. <div class="content">
  23. <span id="currentUsername"><i class="ui loading spinner icon"></i> Loading</span>
  24. <div class="sub header">Current Account Information</div>
  25. </div>
  26. </h4>
  27. </div>
  28. <div class="ui segment">
  29. <p>List of users registered in this WebStick</p>
  30. <table class="ui unstackable basic celled table">
  31. <thead>
  32. <tr><th>Username</th>
  33. <th>Actions</th>
  34. </tr></thead>
  35. <tbody id="userTable">
  36. <tr>
  37. <td colspan="2"><i class="ui loading spinner icon"></i> Loading</td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <div class="ui divider"></div>
  42. <form id="newUserForm" class="ui form" onsubmit="handleNewUser(event);">
  43. <div class="field">
  44. <label>Username</label>
  45. <input id="username" type="text">
  46. </div>
  47. <div class="field">
  48. <label>Password</label>
  49. <input id="password" type="password">
  50. </div>
  51. <div class="field">
  52. <label>Confirm Password</label>
  53. <input id="confirmpw" type="password">
  54. </div>
  55. <button class="ui basic button"><i class="ui green add icon"></i> Create User</button>
  56. </form>
  57. </div>
  58. </div>
  59. <script>
  60. var currentUserInfo = {};
  61. //Load current login user info from server
  62. function initUserInfo(){
  63. $.get("/api/user/info", function(data){
  64. currentUserInfo = data;
  65. var html = data.username;
  66. if (data.admin){
  67. html += ` <i class="yellow shield alternate icon"></i>`;
  68. }else{
  69. //Hide the new user section
  70. $("#newUserForm").remove();
  71. }
  72. $("#currentUsername").html(html);
  73. initUserList();
  74. })
  75. }
  76. initUserInfo();
  77. //Load user list from WebStick
  78. function initUserList(){
  79. $("#userTable").html(`<tr>
  80. <td colspan="2"><i class="ui loading spinner icon"></i> Loading</td>
  81. </tr>`);
  82. $.get("/api/user/list", function(data){
  83. if (data.error != undefined){
  84. alert(data.error);
  85. }else{
  86. $("#userTable").html(``);
  87. data.forEach(function(user){
  88. let username = user.Username;
  89. if (!currentUserInfo.admin){
  90. //Not admin
  91. var actionField = '<small style="opacity: 0.3;">No available actions</small>';
  92. if (username == currentUserInfo.username){
  93. actionField = `<button class="ui basic button" onclick="changePassword('${username}');"><i class="ui blue edit icon"></i> Change Password</button>`;
  94. }
  95. $("#userTable").append(`<tr>
  96. <td>${username}</td>
  97. <td>
  98. ${actionField}
  99. </td>
  100. </tr>`);
  101. }else{
  102. //admin
  103. $("#userTable").append(`<tr>
  104. <td>${username}</td>
  105. <td userid="${username}">
  106. <button class="ui basic button" onclick="changePassword('${username}');"><i class="ui blue edit icon"></i> Change Password</button>
  107. <button class="ui basic button" onclick="delUser('${username}');"><i class="ui red trash icon"></i> Remove</button>
  108. </td>
  109. </tr>`);
  110. }
  111. });
  112. if (data.length == 0){
  113. $("#userTable").append(`<tr>
  114. <td colspan="2"><i class="green circle check icon"></i> No registered user</td>
  115. </tr>`);
  116. }
  117. }
  118. })
  119. }
  120. //Create new user
  121. function handleNewUser(e){
  122. e.preventDefault();
  123. let username = $("#username").val().trim();
  124. let password = $("#password").val().trim();
  125. let confirmpw = $("#confirmpw").val().trim();
  126. if (password != confirmpw){
  127. //Mismatch
  128. $("#confirmpw").parent().addClass("error");
  129. return;
  130. }else{
  131. $("#confirmpw").parent().removeClass("error");
  132. }
  133. //Create the user
  134. $.post("/api/user/new?username=" + username + "&password=" + password, function(data){
  135. if (data.error != undefined){
  136. alert(data.error);
  137. }else{
  138. //User added
  139. initUserList();
  140. $("#username").val("");
  141. $("#password").val("");
  142. $("#confirmpw").val("");
  143. }
  144. })
  145. }
  146. //Server side will check for if you are admin
  147. function delUser(username){
  148. $.post("/api/user/del?username=" + username, function(data){
  149. if (data.error != undefined){
  150. alert(data.error);
  151. }else{
  152. //ok!
  153. initUserList();
  154. }
  155. })
  156. }
  157. //Change password, admin can change all user's password,
  158. //user can only change their own password
  159. function changePassword(username){
  160. let newpassword = prompt("New password", "");
  161. if (newpassword == "" || newpassword== null){
  162. return;
  163. }
  164. let confirmNewPassword = prompt("Confirm new password", "");
  165. if (newpassword != confirmNewPassword){
  166. alert("Confirm password not match!");
  167. return;
  168. }
  169. //Request server side to change user password
  170. $.post("/api/user/chpw?username=" + username + "&newpw=" + newpassword, function(data){
  171. if (data.error != undefined){
  172. alert(data.error);
  173. }else{
  174. alert("Password updated!");
  175. }
  176. })
  177. }
  178. </script>
  179. </body>
  180. </html>