From b8181be096d503a92e0de79ae7221b233c434b79 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 18 Oct 2020 15:08:49 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Cleanup=20shares=20when=20deleting?= =?UTF-8?q?=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd_user.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cmd_user.go b/cmd_user.go index 9c0ba55..a930d1f 100644 --- a/cmd_user.go +++ b/cmd_user.go @@ -26,6 +26,7 @@ package main import ( "fmt" + "os" "golang.org/x/crypto/bcrypt" ) @@ -127,5 +128,23 @@ type CmdUserDelete struct { } 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 }