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