switchAccount.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <title>Add Account</title>
  7. <link rel="stylesheet" href="../../script/semantic/semantic.css">
  8. <link rel="stylesheet" href="../../script/ao.css">
  9. <script type="application/javascript" src="../../script/jquery.min.js"></script>
  10. <script type="application/javascript" src="../../script/ao_module.js"></script>
  11. <script type="application/javascript" src="../../script/semantic/semantic.js"></script>
  12. <script type="text/javascript" src="../../script/applocale.js"></script>
  13. <style>
  14. body{
  15. background-color: var(--body_background);
  16. }
  17. .alternativeAccount{
  18. cursor: pointer;
  19. padding: 0.6em;
  20. border: 1px solid rgb(236, 236, 236);
  21. border-radius: 0.4em;
  22. margin-top: 0.4em;
  23. }
  24. .alternativeAccount.expired{
  25. opacity: 0.6;
  26. }
  27. .alternativeAccount:hover{
  28. opacity: 0.6;
  29. }
  30. /*
  31. Darktheme overwrite
  32. */
  33. body.darkTheme .ui.segment{
  34. background-color: var(--body_background_active);
  35. color: var(--text_color);
  36. }
  37. body.darkTheme div, body.darkTheme button, body.darkTheme span, body.darkTheme i{
  38. color: var(--text_color);
  39. }
  40. body.darkTheme .ui.header .sub.header{
  41. color: var(--text_color_secondary);
  42. }
  43. body.darkTheme .ui.basic.buttons .button,body.darkTheme .ui.basic.button{
  44. color: var(--text_color_secondary);
  45. border: 1px solid var(--text_color_secondary);
  46. }
  47. body.darkTheme .ui.basic.buttons .button,body.darkTheme .ui.basic.button:hover{
  48. background-color: var(--body_background_active) !important;
  49. opacity: 0.8;
  50. }
  51. body.darkTheme .ui.form .field > label{
  52. color: var(--text_color_secondary) !important;
  53. }
  54. body.darkTheme .ui.divider{
  55. border-bottom: 1px solid var(--divider) !important;
  56. }
  57. body.darkTheme .ui.form input:not([type]),body.darkTheme .ui.form input[type="date"],body.darkTheme .ui.form input[type="datetime-local"],body.darkTheme .ui.form input[type="email"],body.darkTheme .ui.form input[type="number"],body.darkTheme .ui.form input[type="password"],body.darkTheme .ui.form input[type="search"], .ui.form input[type="tel"],body.darkTheme .ui.form input[type="time"],body.darkTheme .ui.form input[type="text"],body.darkTheme .ui.form input[type="file"],body.darkTheme .ui.form input[type="url"]{
  58. background: var(--body_background_secondary);
  59. border: 1px solid var(--text_color_invert);
  60. color: var(--text_color);
  61. }
  62. body.darkTheme .ui.message{
  63. background-color: #1B1C1D;
  64. color: rgba(255, 255, 255, 0.9);
  65. }
  66. body.darkTheme #isAdminLogo{
  67. color: var(--text_color) !important;
  68. }
  69. body.darkTheme .alternativeAccount .content i.isAdminIcon{
  70. color: var(--text_color) !important;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <br>
  76. <div class="ui container" align="center">
  77. <div class="ui segment" style="max-width:400px;" align="left">
  78. <!-- Current In Use Account -->
  79. <p locale="desc/currentAccount">Current account</p>
  80. <div class="ui small basic right floated button" onclick="logout();">
  81. <i class="log out icon"></i> <span locale="button/logout">Logout</span>
  82. </div>
  83. <div class="ui header">
  84. <img id="currentUserIcon" src="/images/icons/plugin.png">
  85. <div class="content">
  86. <span id="currentUsername"><i class="ui loading spinner icon"></i></span> <i id="isAdminLogo" style="margin-left: 0.4em; color: rgb(38, 50, 56);" title="Admin" class="small shield alternate icon themed text"></i>
  87. <div id="currentUserGroups" class="sub header"></div>
  88. </div>
  89. </div>
  90. <div class="ui divider"></div>
  91. <p locale="desc/savedAccount">Saved accounts on this browser</p>
  92. <div id="alternativeAccountList">
  93. </div>
  94. <div style="margin-top: 1em !important;">
  95. <div id="signoutAllButton" class="ui fluid small black basic button" onclick="logoutAllAccounts();"><i class="log out icon icon"></i> <span locale="desc/signoutAll">Sign-out all accounts</span></div>
  96. </div>
  97. <div class="ui divider"></div>
  98. <p locale="desc/sign-in-new">Sign-in to new account</p>
  99. <form class="ui form" onsubmit="handleFormSubmit(event, this);">
  100. <div class="field">
  101. <label locale="desc/username">Username</label>
  102. <input id="username" type="text" name="username" value="">
  103. </div>
  104. <div class="field">
  105. <label locale="desc/password">Password</label>
  106. <input id="magic" type="password" name="magic">
  107. </div>
  108. <button id="submitbtn" class="ui basic button"><i class="ui green sign in icon"></i> <span locale="desc/addAccount">Add Local Account</span></button>
  109. </form>
  110. <div id="restoreSessionMessage" class="ui blue inverted segment" style="display:none;">
  111. <span locale="desc/enterPassword">Enter password to resume session</span>
  112. </div>
  113. <div id="errmsg" class="ui red inverted segment" style="display:none;">
  114. <i class="remove icon"></i> <span id="errtext">Internal Server Error</span>
  115. </div>
  116. <br>
  117. </div>
  118. </div>
  119. <script>
  120. //Username is just for display purpose. Even if anyone hacked this
  121. //and change to another user account, it is still based on the session value
  122. //matched by cookie ao_auth on server side
  123. let currentUserInfo = {};
  124. //Initalized localization
  125. if (typeof(applocale) != "undefined"){
  126. applocale.init("../../SystemAO/locale/switchAccount.json", function(){
  127. applocale.translate();
  128. initCurrentAccountInfo(function(){
  129. listAllStoredAccounts();
  130. });
  131. });
  132. }else{
  133. //Applocale not found
  134. var applocale = {};
  135. applocale.getString = function(key, defaultString){
  136. return defaultString;
  137. }
  138. initCurrentAccountInfo(function(){
  139. listAllStoredAccounts();
  140. });
  141. }
  142. //Initialize theme
  143. $.get("../../system/file_system/preference?key=file_explorer/theme", function(data){
  144. if (data == "darkTheme"){
  145. $("body").addClass("darkTheme");
  146. }else{
  147. $("body").addClass("whiteTheme");
  148. }
  149. });
  150. function handleFormSubmit(event, form){
  151. event.preventDefault();
  152. let username = $("#username").val();
  153. let password = $("#magic").val();
  154. //Login to the new account
  155. $.ajax({
  156. url: "../../system/auth/u/switch",
  157. method: "POST",
  158. data: {
  159. username: username,
  160. password: password,
  161. },
  162. success: function(data){
  163. if (data.error != undefined){
  164. $("#errtext").text(data.error);
  165. $("#errmsg").show();
  166. }else{
  167. //Refresh the page
  168. $("#errmsg").hide();
  169. initCurrentAccountInfo(function(){
  170. listAllStoredAccounts();
  171. if(ao_module_virtualDesktop){
  172. parent.initDesktop();
  173. }
  174. });
  175. }
  176. $("#restoreSessionMessage").hide();
  177. },
  178. });
  179. }
  180. function switchAccount(object){
  181. let targetUsername = $(object).attr("acname");
  182. if (targetUsername == undefined || targetUsername == ""){
  183. console.log("Unable to load username from element")
  184. return;
  185. }
  186. //Check if it is expired
  187. if ($(object).hasClass("expired")){
  188. $("#username").val(targetUsername);
  189. $("#restoreSessionMessage").show();
  190. return;
  191. }
  192. $.ajax({
  193. url: "../../system/auth/u/switch",
  194. data: {
  195. "username": targetUsername,
  196. },
  197. success: function(data){
  198. if (data.error != undefined){
  199. showError(data.error);
  200. }else{
  201. hideError();
  202. initCurrentAccountInfo(function(){
  203. listAllStoredAccounts();
  204. if(ao_module_virtualDesktop){
  205. parent.initDesktop();
  206. }
  207. });
  208. }
  209. }
  210. })
  211. }
  212. function logoutAllAccounts(){
  213. if (confirm(applocale.getString("msg/logout/confirm", "This will logout all other accounts from this browser. Confirm?"))){
  214. $.ajax({
  215. url: "/system/auth/u/logoutAll",
  216. success: function(data){
  217. if (data.error != undefined){
  218. showError(data.error);
  219. }else{
  220. //Reset the browser pool id
  221. hideError();
  222. listAllStoredAccounts();
  223. if(ao_module_virtualDesktop){
  224. parent.initDesktop();
  225. }
  226. }
  227. }
  228. })
  229. }
  230. }
  231. function showError(message){
  232. function capitalizeFirstLetter(string) {
  233. return string.charAt(0).toUpperCase() + string.slice(1);
  234. }
  235. $("#errtext").text(capitalizeFirstLetter(message));
  236. $("#errmsg").show();
  237. }
  238. function hideError(){
  239. $("#errmsg").hide();
  240. }
  241. function listAllStoredAccounts(){
  242. $("#alternativeAccountList").empty();
  243. //Request server side for the account pool
  244. $.get("../../system/auth/u/list", function(data){
  245. if (data.error != undefined){
  246. $("#signoutAllButton").addClass('disabled');
  247. $("#alternativeAccountList").append(`<div class="ui message">
  248. <i class="ui green check circle icon"></i> ${applocale.getString("desc/noAlternative", "No other account stored on this browser")}
  249. </div>`);
  250. return;
  251. }else{
  252. if (data.length > 1){
  253. data.forEach(function(account){
  254. if (account.Username == currentUserInfo.username){
  255. //Skip
  256. return;
  257. }
  258. $.get("../../system/desktop/user?target=" + account.Username, function(data){
  259. let userIcon = data.UserIcon;
  260. if (userIcon == ""){
  261. userIcon = "../../img/desktop/system_icon/user.svg"
  262. }
  263. $("#alternativeAccountList").append(`
  264. <div class="alternativeAccount ${account.IsExpired?"expired":""}" acname="${account.Username}" onclick="switchAccount(this);">
  265. <div class="ui header">
  266. <img class="usericon" src="${userIcon}">
  267. <div class="content" style="font-size: 95% !important;">
  268. <span class="username">${account.Username}</span> ${(data.IsAdmin)?'<i style="margin-left: 0.4em; color: rgb(38, 50, 56);" class="small shield alternate icon themed text isAdminIcon"></i>':""}
  269. <div class="sub header usergroup">${!account.IsExpired?"<i class='ui green check circle icon' style='margin-right: 0px;'></i> " + applocale.getString("desc/sessionValid", "Session Valid"):"<i class='ui red times circle icon' style='margin-right: 0px;'></i> " + applocale.getString("desc/sessionExpired", "Session Expired")}</div>
  270. </div>
  271. </div>
  272. </div>
  273. `);
  274. });
  275. });
  276. $("#signoutAllButton").removeClass('disabled');
  277. }else{
  278. $("#signoutAllButton").addClass('disabled');
  279. $("#alternativeAccountList").append(`<div class="ui message">
  280. <i class="ui green check circle icon"></i> ${applocale.getString("desc/noAlternative", "No other account stored on this browser")}
  281. </div>`);
  282. return;
  283. }
  284. }
  285. })
  286. }
  287. function initCurrentAccountInfo(callback=undefined){
  288. $.get("../../system/desktop/user", function(data){
  289. if (data.error != undefined){
  290. alert(data.error);
  291. return
  292. }
  293. let userIcon = data.UserIcon;
  294. if (userIcon == ""){
  295. userIcon = "../../img/desktop/system_icon/user.svg"
  296. }
  297. $("#currentUserIcon").attr("src", userIcon);
  298. $("#currentUsername").text(data.Username);
  299. $("#currentUserGroups").text("@" + data.UserGroups.join(", "));
  300. if (data.IsAdmin){
  301. $("#isAdminLogo").show();
  302. }else{
  303. $("#isAdminLogo").hide();
  304. }
  305. currentUserInfo = {
  306. "username": data.Username,
  307. "admin": data.IsAdmin,
  308. "groups": data.UserGroups,
  309. }
  310. if (callback != undefined){
  311. callback();
  312. }
  313. });
  314. }
  315. function logout() {
  316. loggingOut = true;
  317. if (confirm(applocale.getString("msg/logout/thisAcConfirm", "Exiting Session. Confirm?"))){
  318. $.get("../../system/auth/logout", function() {
  319. window.top.location.href = "../../";
  320. });
  321. }
  322. hideAllContextMenus();
  323. }
  324. </script>
  325. </body>
  326. </html>