editUser.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <html>
  2. <head>
  3. <title>Edit User</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
  6. <link rel="stylesheet" href="../../script/semantic/semantic.css">
  7. <link rel="stylesheet" href="../../script/ao.css">
  8. <script type="text/javascript" src="../../script/jquery.min.js"></script>
  9. <script type="text/javascript" src="../../script/semantic/semantic.js"></script>
  10. <script type="text/javascript" src="../../script/ao_module.js"></script>
  11. <style>
  12. body{
  13. background-color:white;
  14. }
  15. .themebackground{
  16. background-color:#588ce0 !important;
  17. color:white !important;
  18. background-image: url("/img/public/slate.png") !important;
  19. background-repeat: no-repeat !important;
  20. background-attachment: fixed !important;
  21. height:100px;
  22. border: 0px solid transparent !important;
  23. padding:24px !important;
  24. }
  25. .required{
  26. color:red;
  27. }
  28. .actionbtns{
  29. text-align:right;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="ui fluid attached segment themebackground" >
  35. <h24 class="ui inverted header">
  36. <i class="edit icon"></i>
  37. <div class="content">
  38. Edit User
  39. <div class="sub header">Edit user profile and permissions</div>
  40. </div>
  41. </h4>
  42. </div>
  43. <div class="ui container">
  44. <div id="confirmUpdate" class="ui green inverted segment" style="display:none; margin-top:12px;">
  45. <i class="checkmark icon"></i> Profile Updated
  46. </div>
  47. <div id="updateError" class="ui red inverted segment" style="display:none; margin-top:12px;">
  48. <i class="remove icon"></i> <span id="errmsg"></span>
  49. </div>
  50. <h4>User Profile & Permission</h4>
  51. <form class="ui form" onsubmit="handleUserProfileUpdate(event);">
  52. <div class="field">
  53. <label>Username</label>
  54. <input id="username" type="text" name="username" placeholder="Username" readonly=true>
  55. </div>
  56. <div class="field">
  57. <div class="ui divder"></div>
  58. <label>Permission Groups</label>
  59. <select id="pgroups" multiple="" class="ui fluid dropdown">
  60. </select>
  61. </div>
  62. <table class="ui celled table">
  63. <thead>
  64. <tr><th>Group Name</th>
  65. <th>Is Admin</th>
  66. <th>Accessible Modules</th>
  67. </tr></thead>
  68. <tbody id="permissionTable">
  69. </tbody>
  70. </table>
  71. <div class="two fields">
  72. <div class="field">
  73. <label>Storage Quota <span class="required">*</span></label>
  74. <input id="quota" type="text" value="15">
  75. <small>Set to 0 for READONLY and -1 for Unlimited Storage</small>
  76. </div>
  77. <div class="field">
  78. <label>Unit <span class="required">*</span></label>
  79. <select class="ui fluid search dropdown" id="unit">
  80. <option value="1">Bytes</option>
  81. <option value="1024">KB</option>
  82. <option value="1048576">MB</option>
  83. <option value="1073741824">GB</option>
  84. <option value="1099511627776">TB</option>
  85. <option value="1125899906842624">PB</option>
  86. </select>
  87. </div>
  88. </div>
  89. <button class="ui right floated button" onclick="event.preventDefault(); handleEditorExit();">Close</button>
  90. <button class="ui primary right floated button" type="submit">Update</button>
  91. <br><br>
  92. <div class="ui divider"></div>
  93. <h4>Password & Security</h4>
  94. <p>Reset Password</p>
  95. <button class="ui red button" onclick="restartPassword();">RESET PASSWORD</button>
  96. <p>A temporary password will be generated once reset is requested. Administrator should provide the user with the password reset link generated below for the user to reset their password.</p>
  97. <div id="tmppwui" class="ui green segment" style="margin-top:12px; display:none;">
  98. <p><i class="checkmark icon"></i> Password Reset Link Generated</p>
  99. <div class="ui labeled fluid input">
  100. <label for="tmpw" class="ui label"><i class="key icon"></i></label>
  101. <input id="tmppw" type="text" placeholder="Temporary Password" id="tmpw">
  102. </div>
  103. <small>Please provide the password reset link listed above to the user and request user to update their password through the password reset tool.</small>
  104. </div>
  105. </div>
  106. <script>
  107. var currentEditingUsername = "";
  108. var userdata;
  109. var groupList = {};
  110. //Get username from window hash
  111. if (window.location.hash.length > 0){
  112. var username = window.location.hash.substring(1);
  113. username = decodeURIComponent(username);
  114. $("#username").val(username);
  115. currentEditingUsername = username;
  116. }
  117. $("#pgroups").on("change", function(e){
  118. updatePGTable();
  119. });
  120. $("#unit").dropdown("set selected","GB");
  121. function bytesToSize(bytes) {
  122. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB'];
  123. if (bytes == 0) return '0 Byte';
  124. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  125. return [bytes / Math.pow(1024, i), sizes[i]];
  126. }
  127. initContents(function(){
  128. initGroupList(function(){
  129. $('.ui.dropdown').dropdown();
  130. });
  131. });
  132. function handleEditorExit(){
  133. ao_module_parentCallback(true);
  134. ao_module_close();
  135. }
  136. function initContents(callback=undefined){
  137. //Update the table contents
  138. $.ajax({
  139. url: "../../system/users/editUser",
  140. data: {username: currentEditingUsername},
  141. method:"POST",
  142. success: function(data){
  143. if (data.error !== undefined){
  144. alert(data.error);
  145. }else{
  146. userdata = data;
  147. var displaysize = bytesToSize(data.Quota)
  148. if (data.Quota == -1){
  149. //Unlimited
  150. $("#quota").val(-1);
  151. }else if (data.Quota == -2){
  152. //Not initialized. Set it to 0
  153. $("#quota").val(0);
  154. }else{
  155. $("#quota").val(displaysize[0]);
  156. }
  157. $("#unit").dropdown("set selected",displaysize[1])
  158. if (callback !== undefined){
  159. callback();
  160. }
  161. }
  162. }
  163. });
  164. }
  165. function initGroupList(callback=undefined){
  166. //Initialize group list
  167. $.ajax({
  168. url: "../../system/permission/listgroup?showper=true",
  169. success: function(data){
  170. $("#pgroups").html("");
  171. if (data.error !== undefined){
  172. alert(data.error)
  173. }else{
  174. groupList = data;
  175. for (const [key, value] of Object.entries(data)) {
  176. $("#pgroups").append(`<option value="${key}">${key}</option>`);
  177. }
  178. $('#pgroups').dropdown('set selected', userdata.Usergroup);
  179. updatePGTable();
  180. if (callback !== undefined){
  181. callback();
  182. }
  183. }
  184. }
  185. });
  186. }
  187. function updatePGTable(){
  188. $("#permissionTable").html("");
  189. var pgList = $("#pgroups").val();
  190. pgList.forEach(pgid => {
  191. var groupInfo = groupList[pgid];
  192. var isAdmin = '<i class="green checkmark icon"></i>';
  193. if (groupInfo[1] == false){
  194. isAdmin = '<i class="red remove icon"></i>';
  195. }
  196. $("#permissionTable").append(`
  197. <tr>
  198. <td data-label="groupname">${pgid}</td>
  199. <td data-label="isdamin">${isAdmin}</td>
  200. <td data-label="modules">${groupInfo[0].join("/")}</td>
  201. </tr>`);
  202. });
  203. }
  204. function restartPassword(){
  205. var username = $("#username").val();
  206. $.ajax({
  207. url: "../../system/users/editUser",
  208. data: {opr: "resetPassword", username: username},
  209. method: "POST",
  210. success: function(data){
  211. if (data.error !== undefined){
  212. alert(data.error);
  213. }else{
  214. //Update suceed
  215. initContents();
  216. $("#confirmUpdate").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
  217. console.log(data);
  218. $("#tmppw").val(location.protocol + '//' + location.host + "/reset.system?acc=" + username + "&rkey=" + data);
  219. $("#tmppwui").slideDown('fast');
  220. }
  221. }
  222. });
  223. }
  224. function handleUserProfileUpdate(e){
  225. e.preventDefault();
  226. //Get all the value from the form
  227. var username = $("#username").val();
  228. var pgs = JSON.stringify($("#pgroups").val());
  229. //Parse Quota
  230. var userStorageQuota = parseFloat($("#quota").val()) * $("#unit").val();
  231. if (isNaN(userStorageQuota)){
  232. $("#quota").parent().addClass("error");
  233. return
  234. }else{
  235. $("#quota").parent().removeClass("error");
  236. }
  237. if (parseInt($("#quota").val()) == -1){
  238. userStorageQuota = -1;
  239. }
  240. $.ajax({
  241. url: "../../system/users/editUser",
  242. data: {opr: "updateUserGroup", newgroup: pgs, username: username, quota: userStorageQuota},
  243. method: "POST",
  244. success: function(data){
  245. if (data.error !== undefined){
  246. $("#errmsg").text(data.error);
  247. $("#updateError").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
  248. }else{
  249. //Update suceed
  250. initContents();
  251. updatePGTable();
  252. $("#confirmUpdate").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
  253. ao_module_parentCallback(true);
  254. }
  255. window.scrollTo(0,0);
  256. }
  257. });
  258. }
  259. </script>
  260. </body>
  261. </html>