github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/user/disenable.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package user 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 10 "github.com/juju/juju/cmd/juju/block" 11 "github.com/juju/juju/cmd/modelcmd" 12 ) 13 14 var usageDisableUserSummary = ` 15 Disables a Juju user.`[1:] 16 17 var usageDisableUserDetails = ` 18 A disabled Juju user is one that cannot log in to any controller. 19 This command has no affect on models that the disabled user may have 20 created and/or shared nor any services associated with that user. 21 22 Examples: 23 juju disable-user bob 24 25 See also: 26 enable-user 27 list-users 28 login`[1:] 29 30 var usageEnableUserSummary = ` 31 Re-enables a previously disabled Juju user.`[1:] 32 33 var usageEnableUserDetails = ` 34 An enabled Juju user is one that can log in to a controller. 35 36 Examples: 37 juju enable-user bob 38 39 See also: 40 disable-user 41 list-users 42 login`[1:] 43 44 // disenableUserBase common code for enable/disable user commands 45 type disenableUserBase struct { 46 modelcmd.ControllerCommandBase 47 api disenableUserAPI 48 User string 49 } 50 51 func NewDisableCommand() cmd.Command { 52 return modelcmd.WrapController(&disableCommand{}) 53 } 54 55 // disableCommand disables users. 56 type disableCommand struct { 57 disenableUserBase 58 } 59 60 func NewEnableCommand() cmd.Command { 61 return modelcmd.WrapController(&enableCommand{}) 62 } 63 64 // enableCommand enables users. 65 type enableCommand struct { 66 disenableUserBase 67 } 68 69 // Info implements Command.Info. 70 func (c *disableCommand) Info() *cmd.Info { 71 return &cmd.Info{ 72 Name: "disable-user", 73 Args: "<user name>", 74 Purpose: usageDisableUserSummary, 75 Doc: usageDisableUserDetails, 76 } 77 } 78 79 // Info implements Command.Info. 80 func (c *enableCommand) Info() *cmd.Info { 81 return &cmd.Info{ 82 Name: "enable-user", 83 Args: "<user name>", 84 Purpose: usageEnableUserSummary, 85 Doc: usageEnableUserDetails, 86 } 87 } 88 89 // Init implements Command.Init. 90 func (c *disenableUserBase) Init(args []string) error { 91 if len(args) == 0 { 92 return errors.New("no username supplied") 93 } 94 // TODO(thumper): support multiple users in one command, 95 // and also verify that the values are valid user names. 96 c.User = args[0] 97 return cmd.CheckEmpty(args[1:]) 98 } 99 100 // Username is here entirely for testing purposes to allow both the 101 // disableCommand and enableCommand to support a common interface that is able 102 // to ask for the command line supplied username. 103 func (c *disenableUserBase) Username() string { 104 return c.User 105 } 106 107 // disenableUserAPI defines the API methods that the disable and enable 108 // commands use. 109 type disenableUserAPI interface { 110 EnableUser(username string) error 111 DisableUser(username string) error 112 Close() error 113 } 114 115 // Run implements Command.Run. 116 func (c *disableCommand) Run(ctx *cmd.Context) error { 117 if c.api == nil { 118 api, err := c.NewUserManagerAPIClient() 119 if err != nil { 120 return errors.Trace(err) 121 } 122 c.api = api 123 defer c.api.Close() 124 } 125 126 if err := c.api.DisableUser(c.User); err != nil { 127 return block.ProcessBlockedError(err, block.BlockChange) 128 } 129 ctx.Infof("User %q disabled", c.User) 130 return nil 131 } 132 133 // Run implements Command.Run. 134 func (c *enableCommand) Run(ctx *cmd.Context) error { 135 if c.api == nil { 136 api, err := c.NewUserManagerAPIClient() 137 if err != nil { 138 return errors.Trace(err) 139 } 140 c.api = api 141 defer c.api.Close() 142 } 143 144 if err := c.api.EnableUser(c.User); err != nil { 145 return block.ProcessBlockedError(err, block.BlockChange) 146 } 147 ctx.Infof("User %q enabled", c.User) 148 return nil 149 }