github.com/decred/politeia@v1.4.0/politeiawww/cmd/shared/usermanage.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package shared
     6  
     7  import (
     8  	"fmt"
     9  	"strconv"
    10  
    11  	v1 "github.com/decred/politeia/politeiawww/api/www/v1"
    12  )
    13  
    14  // UserManageCmd allows an admin to edit certain properties of the specified
    15  // user.
    16  type UserManageCmd struct {
    17  	Args struct {
    18  		UserID string `positional-arg-name:"userid"` // User ID
    19  		Action string `positional-arg-name:"action"` // Edit user action
    20  		Reason string `positional-arg-name:"reason"` // Reason for editing user
    21  	} `positional-args:"true" required:"true"`
    22  }
    23  
    24  // Execute executes the manage user command.
    25  func (cmd *UserManageCmd) Execute(args []string) error {
    26  	ManageActions := map[string]v1.UserManageActionT{
    27  		"expirenewuser":       v1.UserManageExpireNewUserVerification,
    28  		"expireupdatekey":     v1.UserManageExpireUpdateKeyVerification,
    29  		"expireresetpassword": v1.UserManageExpireResetPasswordVerification,
    30  		"clearpaywall":        v1.UserManageClearUserPaywall,
    31  		"unlock":              v1.UserManageUnlock,
    32  		"deactivate":          v1.UserManageDeactivate,
    33  		"reactivate":          v1.UserManageReactivate,
    34  	}
    35  
    36  	// Parse edit user action.  This can be either the numeric
    37  	// action code or the human readable equivalent.
    38  	var action v1.UserManageActionT
    39  	a, err := strconv.ParseUint(cmd.Args.Action, 10, 32)
    40  	if err == nil {
    41  		// Numeric action code found
    42  		action = v1.UserManageActionT(a)
    43  	} else if a, ok := ManageActions[cmd.Args.Action]; ok {
    44  		// Human readable action code found
    45  		action = a
    46  	} else {
    47  		return fmt.Errorf("Invalid useredit action.  Valid actions are:\n  " +
    48  			"expirenewuser         expires new user verification\n  " +
    49  			"expireupdatekey       expires update user key verification\n  " +
    50  			"expireresetpassword   expires reset password verification\n  " +
    51  			"clearpaywall          clears user registration paywall\n  " +
    52  			"unlock                unlocks user account from failed logins\n  " +
    53  			"deactivate            deactivates user account\n  " +
    54  			"reactivate            reactivates user account")
    55  	}
    56  
    57  	// Setup request
    58  	mu := &v1.ManageUser{
    59  		UserID: cmd.Args.UserID,
    60  		Action: action,
    61  		Reason: cmd.Args.Reason,
    62  	}
    63  
    64  	// Print request details
    65  	err = PrintJSON(mu)
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	// Send request
    71  	mur, err := client.ManageUser(mu)
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	// Print response details
    77  	return PrintJSON(mur)
    78  }
    79  
    80  // UserManageHelpMsg is the output of the help command when 'edituser' is
    81  // specified.
    82  const UserManageHelpMsg = `usermanage "userid" "action" "reason"
    83  
    84  Edit the details for the given user id. Requires admin privileges.
    85  
    86  Arguments:
    87  1. userid       (string, required)   User id
    88  2. action       (string, required)   Edit user action
    89  3. reason       (string, required)   Reason for editing the user
    90  
    91  Valid actions are:
    92  1. expirenewuser           Expires new user verification
    93  2. expireupdatekey         Expires update user key verification
    94  3. expireresetpassword     Expires reset password verification
    95  4. clearpaywall            Clears user registration paywall
    96  5. unlocks                 Unlocks user account from failed logins
    97  6. deactivates             Deactivates user account
    98  7. reactivate              Reactivates user account`