feat(patch): upgrade patch module (#7738)

* feat(patch): upgrade patch module

* chore(patch): add docs

* fix(patch): skip and rewrite invalid last launched version

* fix(patch): turn two functions into patches
This commit is contained in:
KirCute_ECT
2024-12-30 22:48:33 +08:00
committed by GitHub
parent 42243b1517
commit 5994c17b4e
9 changed files with 184 additions and 35 deletions

View File

@ -0,0 +1,35 @@
package patch
import (
"github.com/alist-org/alist/v3/internal/bootstrap/patch/v3_24_0"
"github.com/alist-org/alist/v3/internal/bootstrap/patch/v3_32_0"
"github.com/alist-org/alist/v3/internal/bootstrap/patch/v3_41_0"
)
type VersionPatches struct {
// Version means if the system is upgraded from Version or an earlier one
// to the current version, all patches in Patches will be executed.
Version string
Patches []func()
}
var UpgradePatches = []VersionPatches{
{
Version: "v3.24.0",
Patches: []func(){
v3_24_0.HashPwdForOldVersion,
},
},
{
Version: "v3.32.0",
Patches: []func(){
v3_32_0.UpdateAuthnForOldVersion,
},
},
{
Version: "v3.41.0",
Patches: []func(){
v3_41_0.GrantAdminPermissions,
},
},
}

View File

@ -0,0 +1,26 @@
package v3_24_0
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
)
// HashPwdForOldVersion encode passwords using SHA256
// First published: 75acbcc perf: sha256 for user's password (close #3552) by Andy Hsu
func HashPwdForOldVersion() {
users, _, err := op.GetUsers(1, -1)
if err != nil {
utils.Log.Fatalf("[hash pwd for old version] failed get users: %v", err)
}
for i := range users {
user := users[i]
if user.PwdHash == "" {
user.SetPassword(user.Password)
user.Password = ""
if err := db.UpdateUser(&user); err != nil {
utils.Log.Fatalf("[hash pwd for old version] failed update user: %v", err)
}
}
}
}

View File

@ -0,0 +1,25 @@
package v3_32_0
import (
"github.com/alist-org/alist/v3/internal/db"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
)
// UpdateAuthnForOldVersion updates users' authn
// First published: bdfc159 fix: webauthn logspam (#6181) by itsHenry
func UpdateAuthnForOldVersion() {
users, _, err := op.GetUsers(1, -1)
if err != nil {
utils.Log.Fatalf("[update authn for old version] failed get users: %v", err)
}
for i := range users {
user := users[i]
if user.Authn == "" {
user.Authn = "[]"
if err := db.UpdateUser(&user); err != nil {
utils.Log.Fatalf("[update authn for old version] failed update user: %v", err)
}
}
}
}

View File

@ -0,0 +1,22 @@
package v3_41_0
import (
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/pkg/utils"
)
// GrantAdminPermissions gives admin Permission 0(can see hidden) - 9(webdav manage)
// This patch is written to help users upgrading from older version better adapt to PR AlistGo/alist#7705.
func GrantAdminPermissions() {
admin, err := op.GetAdmin()
if err != nil {
utils.Log.Errorf("Cannot grant permissions to admin: %v", err)
}
if (admin.Permission & 0x3FF) == 0 {
admin.Permission |= 0x3FF
}
err = op.UpdateUser(admin)
if err != nil {
utils.Log.Errorf("Cannot grant permissions to admin: %v", err)
}
}