github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/password.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  var cmdPassword = &Command{
     8  	Run:   runPassword,
     9  	Usage: "password <command> [user name] [new password]",
    10  	Short: "See password status or reset password",
    11  	Long: `
    12  See password status or reset/change password
    13  
    14  Examples:
    15  
    16    force password status joe@org.com
    17  
    18    force password reset joe@org.com
    19  
    20    force password change joe@org.com $uP3r$3cure
    21    
    22  `,
    23  }
    24  
    25  func init() {
    26  }
    27  
    28  func runPassword(cmd *Command, args []string) {
    29  	if len(args) == 0 {
    30  		cmd.printUsage()
    31  	} else {
    32  		switch args[0] {
    33  		case "status":
    34  			runPasswordStatus(args[1:])
    35  		case "reset":
    36  			runPasswordReset(args[1:])
    37  		case "change":
    38  			runPasswordChange(args[1:])
    39  		default:
    40  			ErrorAndExit("no such command: %s", args[0])
    41  		}
    42  	}
    43  }
    44  
    45  func runPasswordStatus(args []string) {
    46  	if len(args) != 1 {
    47  		ErrorAndExit("must specify user name")
    48  	}
    49  	force, _ := ActiveForce()
    50  	records, err := force.Query(fmt.Sprintf("select Id From User Where UserName = '%s'", args[0]))
    51  	if err != nil {
    52  		ErrorAndExit(err.Error())
    53  	} else {
    54  		object, err := force.GetPasswordStatus(records.Records[0]["Id"].(string))
    55  		if err != nil {
    56  			ErrorAndExit(err.Error())
    57  		} else {
    58  			fmt.Printf("\nPassword is expired: %t\n\n", object.IsExpired)
    59  		}
    60  	}
    61  }
    62  
    63  func runPasswordReset(args []string) {
    64  	if len(args) != 1 {
    65  		ErrorAndExit("must specify user name")
    66  	}
    67  	force, _ := ActiveForce()
    68  	records, err := force.Query(fmt.Sprintf("select Id From User Where UserName = '%s'", args[0]))
    69  	object, err := force.ResetPassword(records.Records[0]["Id"].(string))
    70  	if err != nil {
    71  		ErrorAndExit(err.Error())
    72  	} else {
    73  		fmt.Printf("\nNew password is: %s\n\n", object.NewPassword)
    74  	}
    75  }
    76  
    77  func runPasswordChange(args []string) {
    78  	if len(args) != 2 {
    79  		ErrorAndExit("must specify user name")
    80  	}
    81  	force, _ := ActiveForce()
    82  	records, err := force.Query(fmt.Sprintf("select Id From User Where UserName = '%s'", args[0]))
    83  	if err != nil {
    84  		ErrorAndExit(err.Error())
    85  	} else {
    86  		fmt.Println(args[1:])
    87  		newPass := make(map[string]string)
    88  		newPass["NewPassword"] = args[1]
    89  		_, err := force.ChangePassword(records.Records[0]["Id"].(string), newPass)
    90  		if err != nil {
    91  			ErrorAndExit(err.Error())
    92  		} else {
    93  			fmt.Println("\nPassword changed\n")
    94  		}
    95  	}
    96  }