github.com/loafoe/cli@v7.1.0+incompatible/cf/commands/passwd.go (about) 1 package commands 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/password" 5 "code.cloudfoundry.org/cli/cf/commandregistry" 6 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/flags" 9 . "code.cloudfoundry.org/cli/cf/i18n" 10 "code.cloudfoundry.org/cli/cf/requirements" 11 "code.cloudfoundry.org/cli/cf/terminal" 12 ) 13 14 type Password struct { 15 ui terminal.UI 16 pwdRepo password.Repository 17 config coreconfig.ReadWriter 18 } 19 20 func init() { 21 commandregistry.Register(&Password{}) 22 } 23 24 func (cmd *Password) MetaData() commandregistry.CommandMetadata { 25 return commandregistry.CommandMetadata{ 26 Name: "passwd", 27 ShortName: "pw", 28 Description: T("Change user password"), 29 Usage: []string{ 30 T("CF_NAME passwd"), 31 }, 32 } 33 } 34 35 func (cmd *Password) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 36 reqs := []requirements.Requirement{ 37 requirementsFactory.NewLoginRequirement(), 38 } 39 40 return reqs, nil 41 } 42 43 func (cmd *Password) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 44 cmd.ui = deps.UI 45 cmd.config = deps.Config 46 cmd.pwdRepo = deps.RepoLocator.GetPasswordRepository() 47 return cmd 48 } 49 50 func (cmd *Password) Execute(c flags.FlagContext) error { 51 oldPassword := cmd.ui.AskForPassword(T("Current Password")) 52 newPassword := cmd.ui.AskForPassword(T("New Password")) 53 verifiedPassword := cmd.ui.AskForPassword(T("Verify Password")) 54 55 cmd.ui.Say(T("Changing password for user {{.Username}}...", map[string]interface{}{ 56 "Username": cmd.config.Username(), 57 })) 58 59 if verifiedPassword != newPassword { 60 return errors.New(T("Password verification does not match")) 61 } 62 63 err := cmd.pwdRepo.UpdatePassword(oldPassword, newPassword) 64 65 switch typedErr := err.(type) { 66 case nil: 67 case errors.HTTPError: 68 if typedErr.StatusCode() == 401 { 69 return errors.New(T("Current password did not match")) 70 } 71 return err 72 default: 73 return err 74 } 75 76 cmd.ui.Ok() 77 cmd.config.ClearSession() 78 cmd.ui.Say(T("Please log in again")) 79 return nil 80 }