[cli] Add admin disable-2fa cmd

This commit is contained in:
Neeraj Gupta 2024-03-14 16:13:11 +05:30
parent da3c6a78d4
commit 6af62d8727
3 changed files with 56 additions and 3 deletions

View file

@ -29,6 +29,9 @@ var _userDetailsCmd = &cobra.Command{
flags.UserEmail = f.Value.String() flags.UserEmail = f.Value.String()
} }
}) })
if flags.UserEmail == "" {
return fmt.Errorf("user email is required")
}
return ctrl.GetUserId(context.Background(), *flags) return ctrl.GetUserId(context.Background(), *flags)
}, },
} }
@ -47,8 +50,11 @@ var _disable2faCmd = &cobra.Command{
flags.UserEmail = f.Value.String() flags.UserEmail = f.Value.String()
} }
}) })
fmt.Println("Not supported yet") if flags.UserEmail == "" {
return nil return fmt.Errorf("user email is required")
}
return ctrl.Disable2FA(context.Background(), *flags)
}, },
} }
@ -86,6 +92,9 @@ var _updateFreeUserStorage = &cobra.Command{
noLimit = strings.ToLower(f.Value.String()) == "true" noLimit = strings.ToLower(f.Value.String()) == "true"
} }
}) })
if flags.UserEmail == "" {
return fmt.Errorf("user email is required")
}
return ctrl.UpdateFreeStorage(context.Background(), *flags, noLimit) return ctrl.UpdateFreeStorage(context.Background(), *flags, noLimit)
}, },
} }

View file

@ -47,6 +47,29 @@ func (c *Client) ListUsers(ctx context.Context) ([]models.User, error) {
return res.Users, nil return res.Users, nil
} }
func (c *Client) Disable2Fa(ctx context.Context, userID int64) error {
var res interface{}
payload := map[string]interface{}{
"userID": userID,
}
r, err := c.restClient.R().
SetContext(ctx).
SetResult(&res).
SetBody(payload).
Post("/admin/user/disable-2fa")
if err != nil {
return err
}
if r.IsError() {
return &ApiError{
StatusCode: r.StatusCode(),
Message: r.String(),
}
}
return nil
}
func (c *Client) UpdateFreePlanSub(ctx context.Context, userDetails *models.UserDetails, storageInBytes int64, expiryTimeInMicro int64) error { func (c *Client) UpdateFreePlanSub(ctx context.Context, userDetails *models.UserDetails, storageInBytes int64, expiryTimeInMicro int64) error {
var res interface{} var res interface{}
if userDetails.Subscription.ProductID != "free" { if userDetails.Subscription.ProductID != "free" {

View file

@ -33,7 +33,7 @@ func (c *ClICtrl) ListUsers(ctx context.Context, params model.AdminActionForUser
users, err := c.Client.ListUsers(accountCtx) users, err := c.Client.ListUsers(accountCtx)
if err != nil { if err != nil {
if apiErr, ok := err.(*api.ApiError); ok && apiErr.StatusCode == 400 && strings.Contains(apiErr.Message, "Token is too old") { if apiErr, ok := err.(*api.ApiError); ok && apiErr.StatusCode == 400 && strings.Contains(apiErr.Message, "Token is too old") {
fmt.Printf("Old admin token, please re-authenticate using `ente account add` \n") fmt.Printf("Error: old admin token, please re-authenticate using `ente account add` \n")
return nil return nil
} }
return err return err
@ -44,6 +44,27 @@ func (c *ClICtrl) ListUsers(ctx context.Context, params model.AdminActionForUser
return nil return nil
} }
func (c *ClICtrl) Disable2FA(ctx context.Context, params model.AdminActionForUser) error {
accountCtx, err := c.buildAdminContext(ctx, params.AdminEmail)
if err != nil {
return err
}
userDetails, err := c.Client.GetUserIdFromEmail(accountCtx, params.UserEmail)
if err != nil {
return err
}
err = c.Client.Disable2Fa(accountCtx, userDetails.User.ID)
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 disabled 2FA for user")
return nil
}
func (c *ClICtrl) UpdateFreeStorage(ctx context.Context, params model.AdminActionForUser, noLimit bool) error { func (c *ClICtrl) UpdateFreeStorage(ctx context.Context, params model.AdminActionForUser, noLimit bool) error {
accountCtx, err := c.buildAdminContext(ctx, params.AdminEmail) accountCtx, err := c.buildAdminContext(ctx, params.AdminEmail)
if err != nil { if err != nil {