github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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 if verifiedPassword != newPassword { 56 return errors.New(T("Password verification does not match")) 57 } 58 59 cmd.ui.Say(T("Changing password...")) 60 err := cmd.pwdRepo.UpdatePassword(oldPassword, newPassword) 61 62 switch typedErr := err.(type) { 63 case nil: 64 case errors.HTTPError: 65 if typedErr.StatusCode() == 401 { 66 return errors.New(T("Current password did not match")) 67 } 68 return err 69 default: 70 return err 71 } 72 73 cmd.ui.Ok() 74 cmd.config.ClearSession() 75 cmd.ui.Say(T("Please log in again")) 76 return nil 77 }