[cli] Add API to delete-user

This commit is contained in:
Neeraj Gupta 2024-03-14 16:18:08 +05:30
parent 6af62d8727
commit bc47368a01
3 changed files with 60 additions and 1 deletions

View file

@ -58,6 +58,28 @@ var _disable2faCmd = &cobra.Command{
},
}
var _deleteUser = &cobra.Command{
Use: "delete-user",
Short: "Delete a user",
RunE: func(cmd *cobra.Command, args []string) error {
recoverWithLog()
var flags = &model.AdminActionForUser{}
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Name == "admin-user" {
flags.AdminEmail = f.Value.String()
}
if f.Name == "user" {
flags.UserEmail = f.Value.String()
}
})
if flags.UserEmail == "" {
return fmt.Errorf("user email is required")
}
return ctrl.DeleteUser(context.Background(), *flags)
},
}
var _listUsers = &cobra.Command{
Use: "list-users",
Short: "List all users",
@ -108,9 +130,11 @@ func init() {
_listUsers.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_disable2faCmd.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_disable2faCmd.Flags().StringP("user", "u", "", "The email of the user to disable 2FA for. (required)")
_deleteUser.Flags().StringP("admin-user", "a", "", "The email of the admin user. ")
_deleteUser.Flags().StringP("user", "u", "", "The email of the user to delete. (required)")
_updateFreeUserStorage.Flags().StringP("admin-user", "a", "", "The email of the admin user.")
_updateFreeUserStorage.Flags().StringP("user", "u", "", "The email of the user to update subscription for. (required)")
// add a flag with no value --no-limit
_updateFreeUserStorage.Flags().String("no-limit", "True", "When true, sets 100TB as storage limit, and expiry to current date + 100 years")
_adminCmd.AddCommand(_userDetailsCmd, _disable2faCmd, _updateFreeUserStorage, _listUsers)
_adminCmd.AddCommand(_userDetailsCmd, _disable2faCmd, _updateFreeUserStorage, _listUsers, _deleteUser)
}

View file

@ -47,6 +47,24 @@ func (c *Client) ListUsers(ctx context.Context) ([]models.User, error) {
return res.Users, nil
}
func (c *Client) DeleteUser(ctx context.Context, email string) error {
r, err := c.restClient.R().
SetContext(ctx).
SetQueryParam("email", email).
Delete("/admin/user/delete")
if err != nil {
return err
}
if r.IsError() {
return &ApiError{
StatusCode: r.StatusCode(),
Message: r.String(),
}
}
return nil
}
func (c *Client) Disable2Fa(ctx context.Context, userID int64) error {
var res interface{}

View file

@ -44,6 +44,23 @@ func (c *ClICtrl) ListUsers(ctx context.Context, params model.AdminActionForUser
return nil
}
func (c *ClICtrl) DeleteUser(ctx context.Context, params model.AdminActionForUser) error {
accountCtx, err := c.buildAdminContext(ctx, params.AdminEmail)
if err != nil {
return err
}
err = c.Client.DeleteUser(accountCtx, params.UserEmail)
if err != nil {
if apiErr, ok := err.(*api.ApiError); ok && apiErr.StatusCode == 400 && strings.Contains(apiErr.Message, "Token is too old") {
fmt.Printf("Error: old admin token, please re-authenticate using `ente account add` \n")
return nil
}
return err
}
fmt.Println("Successfully deleted user")
return nil
}
func (c *ClICtrl) Disable2FA(ctx context.Context, params model.AdminActionForUser) error {
accountCtx, err := c.buildAdminContext(ctx, params.AdminEmail)
if err != nil {