Add cli command to delete a user

This commit is contained in:
kolaente 2021-08-09 16:43:00 +02:00
parent d1de146f8e
commit bab58fda8d
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 71 additions and 5 deletions

View File

@ -17,6 +17,7 @@
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"os" "os"
"strconv" "strconv"
@ -42,6 +43,7 @@ var (
userFlagResetPasswordDirectly bool userFlagResetPasswordDirectly bool
userFlagEnableUser bool userFlagEnableUser bool
userFlagDisableUser bool userFlagDisableUser bool
userFlagDeleteNow bool
) )
func init() { func init() {
@ -66,7 +68,10 @@ func init() {
userChangeEnabledCmd.Flags().BoolVarP(&userFlagDisableUser, "disable", "d", false, "Disable the user.") userChangeEnabledCmd.Flags().BoolVarP(&userFlagDisableUser, "disable", "d", false, "Disable the user.")
userChangeEnabledCmd.Flags().BoolVarP(&userFlagEnableUser, "enable", "e", false, "Enable the user.") userChangeEnabledCmd.Flags().BoolVarP(&userFlagEnableUser, "enable", "e", false, "Enable the user.")
userCmd.AddCommand(userListCmd, userCreateCmd, userUpdateCmd, userResetPasswordCmd, userChangeEnabledCmd) // User deletion flags
userDeleteCmd.Flags().BoolVarP(&userFlagDeleteNow, "now", "n", false, "If provided, deletes the user immediately instead of sending them an email first.")
userCmd.AddCommand(userListCmd, userCreateCmd, userUpdateCmd, userResetPasswordCmd, userChangeEnabledCmd, userDeleteCmd)
rootCmd.AddCommand(userCmd) rootCmd.AddCommand(userCmd)
} }
@ -300,3 +305,61 @@ var userChangeEnabledCmd = &cobra.Command{
fmt.Printf("User status successfully changed, status is now \"%s\"\n", u.Status) fmt.Printf("User status successfully changed, status is now \"%s\"\n", u.Status)
}, },
} }
var userDeleteCmd = &cobra.Command{
Use: "delete [user id]",
Short: "Delete an existing user.",
Long: "Kick off the user deletion process. If call without the --now flag, this command will only trigger an email to the user in order for them to confirm and start the deletion process. With the flag the user is deleted immediately. USE WITH CAUTION.",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
initialize.FullInit()
},
Run: func(cmd *cobra.Command, args []string) {
if userFlagDeleteNow {
fmt.Println("You requested to delete the user immediately. Are you sure?")
fmt.Println(`To confirm, please type "yes, I confirm" in all uppercase:`)
cr := bufio.NewReader(os.Stdin)
text, err := cr.ReadString('\n')
if err != nil {
log.Fatalf("could not read confirmation message: %s", err)
}
if text != "YES, I CONFIRM\n" {
log.Fatalf("invalid confirmation message")
}
}
s := db.NewSession()
defer s.Close()
if err := s.Begin(); err != nil {
log.Fatalf("Count not start transaction: %s", err)
}
u := getUserFromArg(s, args[0])
if userFlagDeleteNow {
err := models.DeleteUser(s, u)
if err != nil {
_ = s.Rollback()
log.Fatalf("Error removing the user: %s", err)
}
} else {
err := user.RequestDeletion(s, u)
if err != nil {
_ = s.Rollback()
log.Fatalf("Could not request user deletion: %s", err)
}
}
if err := s.Commit(); err != nil {
log.Fatalf("Error saving everything: %s", err)
}
if userFlagDeleteNow {
fmt.Println("User deleted successfully.")
} else {
fmt.Println("User scheduled for deletion successfully.")
}
},
}

View File

@ -54,17 +54,17 @@ func deleteUsers() {
return return
} }
for _, user := range users { for _, u := range users {
err = s.Begin() err = s.Begin()
if err != nil { if err != nil {
log.Errorf("Could not start transaction: %s", err) log.Errorf("Could not start transaction: %s", err)
return return
} }
err = deleteUser(s, user) err = DeleteUser(s, u)
if err != nil { if err != nil {
_ = s.Rollback() _ = s.Rollback()
log.Errorf("Could not delete user %d: %s", user.ID, err) log.Errorf("Could not delete u %d: %s", u.ID, err)
return return
} }
@ -76,7 +76,10 @@ func deleteUsers() {
} }
} }
func deleteUser(s *xorm.Session, u *user.User) (err error) { // DeleteUser completely removes a user and all their associated lists, namespaces and tasks.
// This action is irrevocable.
// Public to allow deletion from the CLI.
func DeleteUser(s *xorm.Session, u *user.User) (err error) {
if u.DeletionScheduledAt.Before(time.Now()) { if u.DeletionScheduledAt.Before(time.Now()) {
// TODO: Proper error // TODO: Proper error
log.Debugf("User %d is not yet scheduled for deletion.", u.ID) log.Debugf("User %d is not yet scheduled for deletion.", u.ID)