session.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. ArozOS Desktop - Session, host and accounts
  3. Connection checks, the signed in user and host information, startup
  4. sound, logout, restart / shutdown and the stored alternative accounts
  5. shown in the quick access panel.
  6. Part of desktop.html, see README.md in this folder for the load order.
  7. */
  8. function handleManualCheckReconnect(button){
  9. $(button).addClass("loading");
  10. checkConnection(undefined, function(status){
  11. let oldText = $(button).text();
  12. $(button).removeClass("loading");
  13. if (status == false){
  14. //Still unable to connect. Show cross icon
  15. $(button).html(`<i class="remove icon"></i>`);
  16. setTimeout(function(){
  17. //Restore its original text after 5 sec
  18. $(button).text(oldText);
  19. }, 5000);
  20. }
  21. });
  22. }
  23. function checkConnection(timeout=15000, callback=undefined){
  24. $.ajax({
  25. url: 'system/auth/checkLogin',
  26. success: function(data) {
  27. if (data == true) {
  28. //Continue session
  29. updateBackgroundgTaskList();
  30. } else {
  31. //Session timeout. Redirect to system index
  32. loggingOut = true;
  33. window.location.href = "index.html";
  34. }
  35. $("#connectionLost").hide();
  36. document.title = hostInfo.Hostname;
  37. if (callback != undefined){
  38. callback(true);
  39. }
  40. },
  41. error: function(evt) {
  42. //Server closed or freezed?
  43. $("#connectionLost").show();
  44. document.title = "Reconnecting...";
  45. if (callback != undefined){
  46. callback(false);
  47. }
  48. },
  49. timeout: timeout
  50. });
  51. }
  52. function initStartupSounds(){
  53. $.ajax({
  54. url: "system/desktop/preference",
  55. method: "POST",
  56. data: {preference: "startup-audio"},
  57. success: function(data){
  58. if (data == undefined || data == ""){
  59. return;
  60. }
  61. var currentGlobalVol = localStorage.getItem("global_volume");
  62. if (currentGlobalVol != null && currentGlobalVol != undefined && currentGlobalVol != ""){
  63. }else{
  64. currentGlobalVol = 0;
  65. }
  66. var audio = new Audio("media?file=" + data);
  67. audio.volume = currentGlobalVol;
  68. audio.play();
  69. }
  70. });
  71. }
  72. function initDesktopUserInfo(callback=undefined){
  73. $.get("system/desktop/user", function(data){
  74. if (data.error !== undefined){
  75. alert(data.error);
  76. }else{
  77. userInfo = data;
  78. listAllStoredAccounts();
  79. //Update the user tag
  80. $("#username").text(userInfo.Username);
  81. $("#usergroups").text("@" + userInfo.UserGroups.join("/"));
  82. $("#usergroups").attr("title",userInfo.UserGroups.join(" / "));
  83. if (data.UserIcon !== ""){
  84. $(".usericon").attr("src",data.UserIcon);
  85. }else{
  86. $(".usericon").attr("src", "img/desktop/system_icon/user.svg")
  87. }
  88. if (data.IsAdmin == false){
  89. //Hide the power buttons
  90. $(".hardware").hide();
  91. }else{
  92. //User is admin. Add admin icon
  93. $("#username").append(`<i style="margin-left: 0.4em; color: #${desktopThemeColor}" class="small shield alternate icon themed text"></i>`);
  94. }
  95. if (callback != undefined){
  96. callback();
  97. }
  98. }
  99. });
  100. }
  101. function initDesktopHostInfo(){
  102. //Load the data into variable
  103. $.get("system/desktop/host",function(data){
  104. hostInfo = data;
  105. //Update the ui element as well
  106. $(".hostname").text(hostInfo.Hostname);
  107. document.title = hostInfo.Hostname;
  108. $("#sysinfo").text(hostInfo.DeviceModel + " firmware:" + hostInfo.BuildVersion + " " + hostInfo.InternalVersion);
  109. qaRenderFacts();
  110. qaHostInfoLoaded();
  111. });
  112. //Volume and brightness are kept in localStorage (0 to 1), which WebApps
  113. //read too; the quick access sliders show and change them (quickaccess.js)
  114. if (localStorage.getItem("global_volume") == null || localStorage.getItem("global_volume") === ""){
  115. localStorage.setItem("global_volume", 0);
  116. }
  117. if (localStorage.getItem("brightness") == null || localStorage.getItem("brightness") === ""){
  118. localStorage.setItem("brightness", 1);
  119. }
  120. qaSyncSliders();
  121. //Check if hardware management mode is enabled. Hide buttons if needed
  122. $.get("system/power/accessCheck",function(data){
  123. if (data == true){
  124. //Hardware management mode is on
  125. powerman = true;
  126. }else{
  127. //Hardware management mode is off
  128. $(".item.powerman").remove();
  129. powerman = false;
  130. }
  131. });
  132. }
  133. function logout() {
  134. loggingOut = true;
  135. if (confirm("Exiting Session. Confirm?")){
  136. $.get("system/auth/logout", function() {
  137. window.location.href = "./";
  138. });
  139. }
  140. hideAllContextMenus();
  141. }
  142. function openSwitchAccountPanel(){
  143. var uuid = newFloatWindow({
  144. url: 'SystemAO/advance/switchAccount.html',
  145. width: 470,
  146. height: 680,
  147. appicon: "SystemAO/desktop/img/account-switch.png",
  148. title: "Switch Account"
  149. });
  150. }
  151. function restart(){
  152. if (!powerman){
  153. return
  154. }
  155. var apiObject = {
  156. api: "/system/power/restart",
  157. data: {},
  158. title: applocale.getString("power/restart/text", "Password Required"),
  159. desc: applocale.getString("power/restart/instruction", "for restarting the ArozOS Host"),
  160. thisuser: true, //This username as default, set to false for entering other user
  161. method: "GET"
  162. }
  163. apiObject = encodeURIComponent(JSON.stringify(apiObject));
  164. newFloatWindow({
  165. url: "SystemAO/security/authreq.html#" + apiObject,
  166. width: 480,
  167. height: 300,
  168. appicon: "SystemAO/security/img/lock.svg",
  169. title: applocale.getString("power/restart/title", "Restart - Authentication Required"),
  170. parent: ao_module_windowID,
  171. callback: "handleRestartCallback"
  172. });
  173. }
  174. function shutdown(){
  175. if (!powerman){
  176. return
  177. }
  178. var apiObject = {
  179. api: "system/power/shutdown",
  180. data: {},
  181. title: `<i class='red exclamation triangle icon'></i> ${applocale.getString("power/shutdown/text", "Shutdown Host")} <i class='red exclamation triangle icon'></i>`,
  182. desc: applocale.getString("power/shutdown/instruction", "Please enter your password to confirm operation."),
  183. thisuser: true, //This username as default, set to false for entering other user
  184. method: "GET",
  185. success: "SystemAO/boot/shutdown.html" //Success redirection
  186. }
  187. apiObject = encodeURIComponent(JSON.stringify(apiObject));
  188. newFloatWindow({
  189. url: "SystemAO/security/authreq.html#" + apiObject,
  190. width: 480,
  191. height: 300,
  192. appicon: "SystemAO/security/img/lock.svg",
  193. title: applocale.getString("power/shutdown/title", "Shutdown - Authentication Required"),
  194. parent: ao_module_windowID,
  195. callback: "handleShutdownCallback"
  196. });
  197. }
  198. /*
  199. Alternative account manager
  200. */
  201. /*
  202. The other accounts signed in on this browser, listed in the launcher's
  203. account panel (#alternativeAccountList). A row switches to its account
  204. (switchAccount); an expired session opens the sign in window instead.
  205. Avatars use their own class: initDesktopUserInfo sets every .usericon to
  206. the signed in user's picture.
  207. */
  208. function listAllStoredAccounts(){
  209. var list = $("#alternativeAccountList");
  210. list.empty();
  211. var showNone = function(){
  212. list.empty().append($('<div class="accountEmpty"></div>').text(applocale.getString("account/switch/noAlternative", "No other account stored on this browser")));
  213. $("#signoutAllButton").addClass("disabled");
  214. };
  215. //Request server side for the account pool
  216. $.get("system/auth/u/list", function(data){
  217. if (data.error != undefined || !Array.isArray(data) || data.length <= 1){
  218. showNone();
  219. return;
  220. }
  221. $("#signoutAllButton").removeClass("disabled");
  222. data.forEach(function(account){
  223. if (account.Username == userInfo.Username){
  224. //Skip
  225. return;
  226. }
  227. var row = $('<div class="accountRow alternativeAccount" onclick="switchAccount(this);">' +
  228. '<img class="accountAvatar" draggable="false"><div class="accountRowText">' +
  229. '<div class="accountRowName"></div><div class="accountRowState"></div></div></div>');
  230. row.attr("acname", account.Username).toggleClass("expired", account.IsExpired === true);
  231. row.find(".accountAvatar").attr("src", "img/desktop/system_icon/user.svg");
  232. row.find(".accountRowName").text(account.Username);
  233. row.find(".accountRowState").text(account.IsExpired ?
  234. applocale.getString("account/switch/sessionExpired", "Session Expired") :
  235. applocale.getString("account/switch/sessionValid", "Session Valid"));
  236. list.append(row);
  237. $.get("system/desktop/user?target=" + encodeURIComponent(account.Username), function(info){
  238. if (info && info.UserIcon){
  239. row.find(".accountAvatar").attr("src", info.UserIcon);
  240. }
  241. if (info && info.IsAdmin){
  242. row.find(".accountRowName").append($('<i class="shield alternate icon"></i>').attr("title", "Admin"));
  243. }
  244. });
  245. });
  246. if (list.children().length == 0){
  247. showNone();
  248. }
  249. });
  250. }
  251. function switchAccount(object){
  252. let targetUsername = $(object).attr("acname");
  253. if (targetUsername == undefined || targetUsername == ""){
  254. console.log("Unable to load username from element")
  255. return;
  256. }
  257. //Check if it is expired
  258. if ($(object).hasClass("expired")){
  259. openSwitchAccountPanel();
  260. return;
  261. }
  262. //Close all floatWindows
  263. $(".floatWindow").each(function(){
  264. let thisFloatWindowID = $(this).attr("windowid");
  265. closeFloatWindowViaID(thisFloatWindowID);
  266. })
  267. $.ajax({
  268. url: "system/auth/u/switch",
  269. data: {
  270. "username": targetUsername,
  271. },
  272. success: function(data){
  273. if (data.error != undefined){
  274. alert(data.error);
  275. }else{
  276. initDesktop();
  277. }
  278. }
  279. })
  280. }
  281. function logoutAllAccounts(){
  282. if (confirm(applocale.getString("account/switch/logout/confirm", "This will logout all other accounts from this browser. Confirm?"))){
  283. $.ajax({
  284. url: "system/auth/u/logoutAll",
  285. success: function(data){
  286. if (data.error != undefined){
  287. alert(data.error);
  288. }else{
  289. //Reset the browser pool id
  290. localStorage.removeItem("ao_acc");
  291. listAllStoredAccounts();
  292. hideToolPanel();
  293. }
  294. }
  295. })
  296. }
  297. }