Parcourir la source

Added desktop code for account switch

Toby Chui il y a 2 ans
Parent
commit
ef5db3243f

+ 6 - 2
mod/filesystem/abstractions/webdavfs/webdavfs.go

@@ -161,7 +161,7 @@ func (e WebDAVFileSystem) IsDir(filename string) bool {
 	return s.IsDir()
 }
 
-//Notes: This is not actual Glob function. This just emulate Glob using ReadDir with max depth 1 layer
+// Notes: This is not actual Glob function. This just emulate Glob using ReadDir with max depth 1 layer
 func (e WebDAVFileSystem) Glob(wildcard string) ([]string, error) {
 	wildcard = filepath.ToSlash(filepath.Clean(wildcard))
 
@@ -172,8 +172,8 @@ func (e WebDAVFileSystem) Glob(wildcard string) ([]string, error) {
 	chunks := strings.Split(strings.TrimPrefix(wildcard, "/"), "/")
 	results, err := e.globpath("/", chunks, 0)
 	return results, err
-
 }
+
 func (e WebDAVFileSystem) GetFileSize(filename string) int64 {
 	filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
 	s, err := e.Stat(filename)
@@ -184,6 +184,7 @@ func (e WebDAVFileSystem) GetFileSize(filename string) int64 {
 
 	return s.Size()
 }
+
 func (e WebDAVFileSystem) GetModTime(filename string) (int64, error) {
 	filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
 	s, err := e.Stat(filename)
@@ -193,10 +194,12 @@ func (e WebDAVFileSystem) GetModTime(filename string) (int64, error) {
 
 	return s.ModTime().Unix(), nil
 }
+
 func (e WebDAVFileSystem) WriteFile(filename string, content []byte, mode os.FileMode) error {
 	filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
 	return e.c.Write(filename, content, mode)
 }
+
 func (e WebDAVFileSystem) ReadFile(filename string) ([]byte, error) {
 	filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
 	bytes, err := e.c.Read(filename)
@@ -205,6 +208,7 @@ func (e WebDAVFileSystem) ReadFile(filename string) ([]byte, error) {
 	}
 	return bytes, nil
 }
+
 func (e WebDAVFileSystem) ReadDir(filename string) ([]fs.DirEntry, error) {
 	filename = filterFilepath(filepath.ToSlash(filepath.Clean(filename)))
 	fis, err := e.c.ReadDir(filename)

+ 1 - 1
web/SystemAO/advance/switchAccount.html

@@ -141,7 +141,7 @@
             }
 
             $.ajax({
-                url: "/system/auth/u/switch",
+                url: "../../system/auth/u/switch",
                 data: {
                     "username": targetUsername,
                     "pid": browserAccountPoolUUID,

+ 103 - 2
web/desktop.system

@@ -1204,7 +1204,7 @@
             </div>
             <div class="ui divider"></div>
             <div class="item" style="padding-left: 8px;">
-                <div class="ui small items" style="margin-bottom: 0; width: 100%;">
+                <div id="alternativeAccountList" class="ui small items" style="margin-bottom: 0; width: 100%;">
                     <div class="alternativeUsableAccount item"  style="padding-left: 0px;">
                         <div class="ui mini image">
                             <img class="accountIcon" src="img/desktop/system_icon/user.svg">
@@ -1667,7 +1667,7 @@
                     alert(data.error);
                 }else{
                     userInfo = data;
-                   
+                    listAllStoredAccounts();
 
                     //Update the user tag
                     $("#username").text(userInfo.Username);
@@ -7242,6 +7242,107 @@
                 }
             });
         }
+
+        /*
+            Alternative account manager
+        */
+        function getBrowserAccountPoolUUID(){
+            return localStorage.getItem("ao_acc");
+        }
+        function listAllStoredAccounts(){
+            $("#alternativeAccountList").empty();
+            if (getBrowserAccountPoolUUID() == undefined){
+                 //Empty or no stored accounts
+                 $("#alternativeAccountList").append(`<div class="ui message">
+                    <i class="ui green check circle icon"></i> No other account stored on this browser
+                </div>`);
+                $("#signoutAllButton").addClass('disabled');
+                return;
+            }else{
+                //Request server side for the account pool
+                $.get("system/auth/u/list?pid=" + getBrowserAccountPoolUUID(), function(data){
+                    if (data.error != undefined){
+                        $("#alternativeAccountList").append(`<div class="ui message">
+                            <i class="ui green check circle icon"></i> No other account stored on this browser
+                        </div>`);
+                    }else{
+                        if (data.length > 0){
+                            data.forEach(function(account){
+                                if (account.Username == userInfo.Username){
+                                    //Skip
+                                    return;
+                                }
+                               
+                                $.get("system/desktop/user?target=" + account.Username, function(data){
+                                    let userIcon = data.UserIcon;
+                                    if (userIcon == ""){
+                                        userIcon = "img/desktop/system_icon/user.svg"
+                                    }
+                                    $("#alternativeAccountList").append(`
+                                        <div class="alternativeAccount ${account.IsExpired?"expired":""}" acname="${account.Username}" onclick="switchAccount(this);" style="margin-top: 0.4em;">
+                                            <div class="ui header">
+                                                <img class="usericon" src="${userIcon}">
+                                                <div class="content" style="font-size: 95% !important;">
+                                                    <span class="username">${account.Username}</span> ${(data.IsAdmin)?'<i style="margin-left: 0.4em; color: rgb(38, 50, 56);" title="Admin" class="small shield alternate icon themed text"></i>':""}
+                                                    <div class="sub header usergroup">${!account.IsExpired?"<i class='ui green check circle icon' style='margin-right: 0px;'></i> Session Valid":"<i class='ui red times circle icon' style='margin-right: 0px;'></i> Session Expired"}</div>
+                                                </div>
+                                            </div>
+                                        </div>
+                                    `);
+                                });
+                            });
+                            $("#signoutAllButton").removeClass('disabled');
+                        }else{
+                            $("#signoutAllButton").addClass('disabled');
+                            $("#alternativeAccountList").append(`<div class="ui message">
+                                <i class="ui green check circle icon"></i> No other account stored on this browser
+                            </div>`);
+                            return;
+                        }
+                    }
+                })
+            }
+        }
+
+        function switchAccount(object){
+            alert("WIP")
+            return;
+            let targetUsername = $(object).attr("acname");
+            if (targetUsername == undefined || targetUsername == ""){
+                console.log("Unable to load username from element")
+                return;
+            }
+
+            //Check if it is expired
+            if ($(object).hasClass("expired")){
+                $("#username").val(targetUsername);
+                $("#restoreSessionMessage").show();
+                return;
+            }
+
+            $.ajax({
+                url: "system/auth/u/switch",
+                data: {
+                    "username": targetUsername,
+                    "pid": browserAccountPoolUUID,
+                },
+                success: function(data){
+                    if (data.error != undefined){
+                        showError(data.error);
+                    }else{
+                        hideError();
+                        initCurrentAccountInfo(function(){
+                            listAllStoredAccounts();
+                            if(ao_module_virtualDesktop){
+                                parent.initDesktop();
+                            }
+                        });
+                    }
+                }
+            })
+
+        }
+       
     </script>
 </body>