Browse Source

Added switchable account cleaning to nightly tasks

Toby Chui 2 years ago
parent
commit
1c80f1f907
4 changed files with 53 additions and 1 deletions
  1. 3 0
      auth.go
  2. 41 0
      mod/auth/accountSwitch.go
  3. 6 1
      web/SystemAO/advance/switchAccount.html
  4. 3 0
      web/desktop.system

+ 3 - 0
auth.go

@@ -106,6 +106,9 @@ func AuthSettingsInit() {
 	//Register nightly task for clearup all user retry counter
 	nightlyManager.RegisterNightlyTask(authAgent.ExpDelayHandler.ResetAllUserRetryCounter)
 
+	//Register nightly task for clearup all expired switchable account pools
+	nightlyManager.RegisterNightlyTask(authAgent.SwitchableAccountManager.RunNightlyCleanup)
+
 	/*
 		Account switching functions
 	*/

+ 41 - 0
mod/auth/accountSwitch.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
+	"log"
 	"net/http"
 	"time"
 
@@ -52,9 +53,29 @@ func NewSwitchableAccountPoolManager(sysdb *database.Database, parent *AuthAgent
 		ExpireTime: 604800,
 		authAgent:  parent,
 	}
+
+	//Do an initialization cleanup
+	go func() {
+		thisManager.RunNightlyCleanup()
+	}()
+
+	//Return the manager
 	return &thisManager
 }
 
+// When called, this will clear the account switching pool in which all users session has expired
+func (m *SwitchableAccountPoolManager) RunNightlyCleanup() {
+	pools, err := m.GetAllPools()
+	if err != nil {
+		log.Println("[auth] Unable to load account switching pools. Cleaning skipped: " + err.Error())
+		return
+	}
+
+	for _, pool := range pools {
+		pool.DeletePoolIfAllUserSessionExpired()
+	}
+}
+
 // Handle switchable account listing for this browser
 func (m *SwitchableAccountPoolManager) HandleSwitchableAccountListing(w http.ResponseWriter, r *http.Request) {
 	//Get username and pool id
@@ -363,6 +384,21 @@ func (p *SwitchableAccountsPool) RemoveUser(username string) {
 	p.Save()
 }
 
+// Save changes of this pool to database
+func (p *SwitchableAccountsPool) DeletePoolIfAllUserSessionExpired() {
+	allExpred := true
+	for _, acc := range p.Accounts {
+		if !p.IsAccountExpired(acc) {
+			allExpred = false
+		}
+	}
+
+	if allExpred {
+		//All account expired. Remove this pool
+		p.Delete()
+	}
+}
+
 // Save changes of this pool to database
 func (p *SwitchableAccountsPool) Save() {
 	p.parent.Database.Write("auth_acswitch", p.UUID, p)
@@ -372,3 +408,8 @@ func (p *SwitchableAccountsPool) Save() {
 func (p *SwitchableAccountsPool) Delete() error {
 	return p.parent.Database.Delete("auth_acswitch", p.UUID)
 }
+
+// Check if an account is expired
+func (p *SwitchableAccountsPool) IsAccountExpired(acc *SwitchableAccount) bool {
+	return time.Now().Unix() > acc.LastSwitch+p.parent.ExpireTime
+}

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

@@ -229,7 +229,12 @@
                 //Request server side for the account pool
                 $.get("../../system/auth/u/list?pid=" + browserAccountPoolUUID, function(data){
                     if (data.error != undefined){
-                        alert(data.error);
+                        localStorage.removeItem("ao_acc");
+                        $("#signoutAllButton").addClass('disabled');
+                        $("#alternativeAccountList").append(`<div class="ui message">
+                            <i class="ui green check circle icon"></i> ${applocale.getString("desc/noAlternative", "No other account stored on this browser")}
+                        </div>`);
+                        return;
                     }else{
                         if (data.length > 0){
                             data.forEach(function(account){

+ 3 - 0
web/desktop.system

@@ -7280,6 +7280,9 @@
                         $("#alternativeAccountList").append(`<div class="ui message">
                             <i class="ui green check circle icon"></i> ${applocale.getString("account/switch/noAlternative", "No other account stored on this browser")}
                         </div>`);
+
+                        //This uuid no longer exists or it has been expired and removed by backend server/
+                        localStorage.removeItem("ao_acc");
                     }else{
                         if (data.length > 0){
                             data.forEach(function(account){