github.com/decred/politeia@v1.4.0/politeiawww/cmd/cmswww/edituser.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 main
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"os"
    11  	"strings"
    12  
    13  	cms "github.com/decred/politeia/politeiawww/api/cms/v1"
    14  	"github.com/decred/politeia/politeiawww/cmd/shared"
    15  )
    16  
    17  // EditUserCmd allows a user to edit their own contractor information, such as
    18  // GithubName, MatrixName, Contractor Name, Location and Contact.
    19  type EditUserCmd struct {
    20  	GitHubName         string `long:"githubname" optional:"true" description:"Github handle"`
    21  	MatrixName         string `long:"matrixname" optional:"true" description:"Matrix name"`
    22  	ContractorName     string `long:"contractorname" optional:"true" description:"Identifying IRL name"`
    23  	ContractorLocation string `long:"contractorlocation" optional:"true" description:"IRL location (country or continent)"`
    24  	ContractorContact  string `long:"contact" optional:"true" description:"Contact information"`
    25  }
    26  
    27  // Execute executes the cms edit user information command.
    28  func (cmd *EditUserCmd) Execute(args []string) error {
    29  	// Check for user identity
    30  	if cfg.Identity == nil {
    31  		return shared.ErrUserIdentityNotFound
    32  	}
    33  	lr, err := client.Me()
    34  	if err != nil {
    35  		return err
    36  	}
    37  	uir, err := client.CMSUserDetails(lr.UserID)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	userInfo := cms.User{}
    43  	if uir != nil {
    44  		userInfo = uir.User
    45  	}
    46  	reader := bufio.NewReader(os.Stdin)
    47  	if cmd.MatrixName != "" || cmd.GitHubName != "" ||
    48  		cmd.ContractorName != "" || cmd.ContractorLocation != "" ||
    49  		cmd.ContractorContact != "" {
    50  		if cmd.MatrixName == "" {
    51  			str := fmt.Sprintf(
    52  				"Your current MatrixName setting is: \"%v\" Update?",
    53  				userInfo.MatrixName)
    54  			update, err := promptListBool(reader, str, "no")
    55  			if err != nil {
    56  				return err
    57  			}
    58  			if update {
    59  				for {
    60  					fmt.Printf("Please enter your Matrix contact name:  ")
    61  					cmd.MatrixName, _ = reader.ReadString('\n')
    62  					cmd.MatrixName = strings.TrimSpace(cmd.MatrixName)
    63  					str := fmt.Sprintf(
    64  						"Your current Matrix name setting is: \"%v\" Keep this?",
    65  						cmd.MatrixName)
    66  					update, err := promptListBool(reader, str, "yes")
    67  					if err != nil {
    68  						return err
    69  					}
    70  					if update {
    71  						userInfo.MatrixName = cmd.MatrixName
    72  						break
    73  					}
    74  				}
    75  			}
    76  		}
    77  		if cmd.GitHubName == "" {
    78  			str := fmt.Sprintf(
    79  				"Your current GitHubName setting is: \"%v\" Update?",
    80  				userInfo.GitHubName)
    81  			update, err := promptListBool(reader, str, "no")
    82  			if err != nil {
    83  				return err
    84  			}
    85  			if update {
    86  				for {
    87  					fmt.Printf("Please enter your GitHubName contact name:  ")
    88  					cmd.GitHubName, _ = reader.ReadString('\n')
    89  					cmd.GitHubName = strings.TrimSpace(cmd.GitHubName)
    90  					str := fmt.Sprintf(
    91  						"Your current GitHubName name setting is: \"%v\" Keep this?",
    92  						cmd.GitHubName)
    93  					update, err := promptListBool(reader, str, "yes")
    94  					if err != nil {
    95  						return err
    96  					}
    97  					if update {
    98  						userInfo.GitHubName = cmd.GitHubName
    99  						break
   100  					}
   101  				}
   102  			}
   103  		}
   104  		if cmd.ContractorName == "" {
   105  			str := fmt.Sprintf(
   106  				"Your current Contractor Name setting is: \"%v\" Update?",
   107  				userInfo.ContractorName)
   108  			update, err := promptListBool(reader, str, "no")
   109  			if err != nil {
   110  				return err
   111  			}
   112  			if update {
   113  				for {
   114  					fmt.Printf("Please enter your IRL contractor name:  ")
   115  					cmd.ContractorName, _ = reader.ReadString('\n')
   116  					cmd.ContractorName = strings.TrimSpace(cmd.ContractorName)
   117  					str := fmt.Sprintf(
   118  						"Your current Contractor Name setting is: \"%v\" Keep this?",
   119  						cmd.ContractorName)
   120  					update, err := promptListBool(reader, str, "yes")
   121  					if err != nil {
   122  						return err
   123  					}
   124  					if update {
   125  						userInfo.ContractorName = cmd.ContractorName
   126  						break
   127  					}
   128  				}
   129  			}
   130  		}
   131  		if cmd.ContractorLocation == "" {
   132  			str := fmt.Sprintf("Your current Contractor Location setting is: \"%v\" Update?",
   133  				userInfo.ContractorLocation)
   134  			update, err := promptListBool(reader, str, "no")
   135  			if err != nil {
   136  				return err
   137  			}
   138  			if update {
   139  				for {
   140  					fmt.Printf("Please enter your IRL contractor location:  ")
   141  					cmd.ContractorLocation, _ = reader.ReadString('\n')
   142  					cmd.ContractorLocation = strings.TrimSpace(cmd.ContractorLocation)
   143  					str := fmt.Sprintf(
   144  						"Your current Contractor location setting is: \"%v\" Keep this?",
   145  						cmd.ContractorLocation)
   146  					update, err := promptListBool(reader, str, "yes")
   147  					if err != nil {
   148  						return err
   149  					}
   150  					if update {
   151  						userInfo.ContractorLocation = cmd.ContractorLocation
   152  						break
   153  					}
   154  				}
   155  			}
   156  		}
   157  		if cmd.ContractorContact == "" {
   158  			str := fmt.Sprintf("Your current Contractor Contact setting is: \"%v\" Update?",
   159  				userInfo.ContractorContact)
   160  			update, err := promptListBool(reader, str, "no")
   161  			if err != nil {
   162  				return err
   163  			}
   164  			if update {
   165  				for {
   166  					fmt.Printf("Please enter your Contractor contact information:  ")
   167  					cmd.ContractorContact, _ = reader.ReadString('\n')
   168  					cmd.ContractorContact = strings.TrimSpace(cmd.ContractorContact)
   169  					str := fmt.Sprintf("Your current Contractor contact setting is: \"%v\" Keep this?",
   170  						cmd.ContractorContact)
   171  					update, err := promptListBool(reader, str, "yes")
   172  					if err != nil {
   173  						return err
   174  					}
   175  					if update {
   176  						userInfo.ContractorContact = cmd.ContractorContact
   177  						break
   178  					}
   179  				}
   180  			}
   181  		}
   182  		fmt.Print("\nPlease carefully review your information and ensure it's " +
   183  			"correct. If not, press Ctrl + C to exit. Or, press Enter to continue " +
   184  			"your request.")
   185  		reader.ReadString('\n')
   186  	}
   187  	userInfo.MatrixName = cmd.MatrixName
   188  	userInfo.GitHubName = cmd.GitHubName
   189  	userInfo.ContractorName = cmd.ContractorName
   190  	userInfo.ContractorLocation = cmd.ContractorLocation
   191  	userInfo.ContractorContact = cmd.ContractorContact
   192  
   193  	updateInfo := cms.EditUser{
   194  		ContractorName:     userInfo.ContractorName,
   195  		ContractorLocation: userInfo.ContractorLocation,
   196  		ContractorContact:  userInfo.ContractorContact,
   197  		MatrixName:         userInfo.MatrixName,
   198  		GitHubName:         userInfo.GitHubName,
   199  	}
   200  
   201  	ecur, err := client.CMSEditUser(updateInfo)
   202  	if err != nil {
   203  		return err
   204  	}
   205  
   206  	// Print update user information reply. (should be empty)
   207  	return shared.PrintJSON(ecur)
   208  }