github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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 ) 12 13 const disableUserDoc = ` 14 Disabling a user stops that user from being able to log in. The user still 15 exists and can be reenabled using the "juju enable" command. If the user is 16 already disabled, this command succeeds silently. 17 18 Examples: 19 juju user disable foobar 20 21 See Also: 22 juju help user enable 23 ` 24 25 const enableUserDoc = ` 26 Enabling a user that is disabled allows that user to log in again. The user 27 still exists and can be reenabled using the "juju enable" command. If the 28 user is already enabled, this command succeeds silently. 29 30 Examples: 31 juju user enable foobar 32 33 See Also: 34 juju help user disable 35 ` 36 37 // DisenableUserBase common code for enable/disable user commands 38 type DisenableUserBase struct { 39 UserCommandBase 40 api DisenableUserAPI 41 User string 42 } 43 44 // DisableCommand disables users. 45 type DisableCommand struct { 46 DisenableUserBase 47 } 48 49 // EnableCommand enables users. 50 type EnableCommand struct { 51 DisenableUserBase 52 } 53 54 // Info implements Command.Info. 55 func (c *DisableCommand) Info() *cmd.Info { 56 return &cmd.Info{ 57 Name: "disable", 58 Args: "<username>", 59 Purpose: "disable a user to stop the user logging in", 60 Doc: disableUserDoc, 61 } 62 } 63 64 // Info implements Command.Info. 65 func (c *EnableCommand) Info() *cmd.Info { 66 return &cmd.Info{ 67 Name: "enable", 68 Args: "<username>", 69 Purpose: "reenables a disabled user to allow the user to log in", 70 Doc: enableUserDoc, 71 } 72 } 73 74 // Init implements Command.Init. 75 func (c *DisenableUserBase) Init(args []string) error { 76 if len(args) == 0 { 77 return errors.New("no username supplied") 78 } 79 // TODO(thumper): support multiple users in one command, 80 // and also verify that the values are valid user names. 81 c.User = args[0] 82 return cmd.CheckEmpty(args[1:]) 83 } 84 85 // Username is here entirely for testing purposes to allow both the 86 // DisableCommand and EnableCommand to support a common interface that is able 87 // to ask for the command line supplied username. 88 func (c *DisenableUserBase) Username() string { 89 return c.User 90 } 91 92 // DisenableUserAPI defines the API methods that the disable and enable 93 // commands use. 94 type DisenableUserAPI interface { 95 EnableUser(username string) error 96 DisableUser(username string) error 97 Close() error 98 } 99 100 func (c *DisenableUserBase) getDisableUserAPI() (DisenableUserAPI, error) { 101 return c.NewUserManagerAPIClient() 102 } 103 104 var getDisableUserAPI = (*DisenableUserBase).getDisableUserAPI 105 106 // Run implements Command.Run. 107 func (c *DisableCommand) Run(ctx *cmd.Context) error { 108 if c.api == nil { 109 api, err := c.NewUserManagerAPIClient() 110 if err != nil { 111 return errors.Trace(err) 112 } 113 c.api = api 114 defer c.api.Close() 115 } 116 117 if err := c.api.DisableUser(c.User); err != nil { 118 return block.ProcessBlockedError(err, block.BlockChange) 119 } 120 ctx.Infof("User %q disabled", c.User) 121 return nil 122 } 123 124 // Run implements Command.Run. 125 func (c *EnableCommand) Run(ctx *cmd.Context) error { 126 if c.api == nil { 127 api, err := c.NewUserManagerAPIClient() 128 if err != nil { 129 return errors.Trace(err) 130 } 131 c.api = api 132 defer c.api.Close() 133 } 134 135 if err := c.api.EnableUser(c.User); err != nil { 136 return block.ProcessBlockedError(err, block.BlockChange) 137 } 138 ctx.Infof("User %q enabled", c.User) 139 return nil 140 }