Cleanup shares when deleting user

This commit is contained in:
Andreas Schneider 2020-10-18 15:08:49 +02:00
parent 8f49cccfeb
commit b8181be096
1 changed files with 20 additions and 1 deletions

View File

@ -26,6 +26,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
@ -127,5 +128,23 @@ type CmdUserDelete struct {
} }
func (cmd CmdUserDelete) Run(app *app) error { func (cmd CmdUserDelete) Run(app *app) error {
return app.userStore.RemoveUser(cmd.Username) if err := app.userStore.RemoveUser(cmd.Username); err != nil {
return fmt.Errorf("cannot remove user: %w", err)
}
sharesByUser, err := app.shareStore.FindSharesByUser(cmd.Username)
if err != nil {
return fmt.Errorf("cannot get shares of user: %w", err)
}
allSuccessful := true
for _, userShare := range sharesByUser {
if err := app.shareStore.RemoveShare(userShare.UUID); err != nil {
fmt.Fprintf(os.Stderr, "User %q cannot be removed from Share %q: %v\n", cmd.Username, userShare.UUID.String(), err)
allSuccessful = false
}
}
if !allSuccessful {
return fmt.Errorf("could not remove user from all shares")
}
return nil
} }