fix(storage): Fixed a potential null value issue with not checking firstMount.

- Added a check to see if `firstMount` is null to prevent logic errors.
- Adjusted the loading logic of `GetAllRoles` and `GetAllUsers` to only execute when `firstMount` is non-null.
- Fixed the `usedBy` check logic to ensure that an error message is returned under the correct conditions.
- Optimized code structure to reduce unnecessary execution paths.
pull/9322/head
okatu-loli 2025-09-12 14:22:13 +08:00
parent 3e2be10cf7
commit 66fd8d5ec8
1 changed files with 23 additions and 21 deletions

View File

@ -280,30 +280,32 @@ func DeleteStorageById(ctx context.Context, id uint) error {
return errors.WithMessage(err, "failed get storage") return errors.WithMessage(err, "failed get storage")
} }
firstMount := firstPathSegment(storage.MountPath) firstMount := firstPathSegment(storage.MountPath)
roles, err := db.GetAllRoles() if firstMount != "" {
if err != nil { roles, err := db.GetAllRoles()
return errors.WithMessage(err, "failed to load roles") if err != nil {
} return errors.WithMessage(err, "failed to load roles")
users, err := db.GetAllUsers() }
if err != nil { users, err := db.GetAllUsers()
return errors.WithMessage(err, "failed to load users") if err != nil {
} return errors.WithMessage(err, "failed to load users")
var usedBy []string }
for _, r := range roles { var usedBy []string
for _, entry := range r.PermissionScopes { for _, r := range roles {
if firstPathSegment(entry.Path) == firstMount { for _, entry := range r.PermissionScopes {
usedBy = append(usedBy, "role:"+r.Name) if firstPathSegment(entry.Path) == firstMount {
break usedBy = append(usedBy, "role:"+r.Name)
break
}
} }
} }
} for _, u := range users {
for _, u := range users { if firstPathSegment(u.BasePath) == firstMount {
if firstPathSegment(u.BasePath) == firstMount { usedBy = append(usedBy, "user:"+u.Username)
usedBy = append(usedBy, "user:"+u.Username) }
}
if len(usedBy) > 0 {
return errors.Errorf("storage is used by %s, please cancel usage first", strings.Join(usedBy, ", "))
} }
}
if len(usedBy) > 0 {
return errors.Errorf("storage is used by %s, please cancel usage first", strings.Join(usedBy, ", "))
} }
if !storage.Disabled { if !storage.Disabled {
storageDriver, err := GetStorageByMountPath(storage.MountPath) storageDriver, err := GetStorageByMountPath(storage.MountPath)