github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/user/add.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package user
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"gopkg.in/juju/names.v2"
    12  
    13  	"github.com/juju/juju/apiserver/params"
    14  	jujucmd "github.com/juju/juju/cmd"
    15  	"github.com/juju/juju/cmd/juju/block"
    16  	"github.com/juju/juju/cmd/juju/common"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  )
    19  
    20  var usageSummary = `
    21  Adds a Juju user to a controller.`[1:]
    22  
    23  const usageDetails = `The user's details are stored within the controller and
    24  will be removed when the controller is destroyed.
    25  
    26  A user unique registration string will be printed. This registration string 
    27  must be used by the newly added user as supplied to 
    28  complete the registration process. 
    29  
    30  Some machine providers will require the user to be in possession of certain
    31  credentials in order to create a model.
    32  
    33  Examples:
    34      juju add-user bob
    35      juju add-user --controller mycontroller bob
    36  
    37  See also:
    38      register
    39      grant
    40      users
    41      show-user
    42      disable-user
    43      enable-user
    44      change-user-password
    45      remove-user`
    46  
    47  // AddUserAPI defines the usermanager API methods that the add command uses.
    48  type AddUserAPI interface {
    49  	AddUser(username, displayName, password string) (names.UserTag, []byte, error)
    50  	Close() error
    51  }
    52  
    53  func NewAddCommand() cmd.Command {
    54  	return modelcmd.WrapController(&addCommand{})
    55  }
    56  
    57  // addCommand adds new users into a Juju Server.
    58  type addCommand struct {
    59  	modelcmd.ControllerCommandBase
    60  	api         AddUserAPI
    61  	User        string
    62  	DisplayName string
    63  }
    64  
    65  // Info implements Command.Info.
    66  func (c *addCommand) Info() *cmd.Info {
    67  	return jujucmd.Info(&cmd.Info{
    68  		Name:    "add-user",
    69  		Args:    "<user name> [<display name>]",
    70  		Purpose: usageSummary,
    71  		Doc:     usageDetails,
    72  	})
    73  }
    74  
    75  // Init implements Command.Init.
    76  func (c *addCommand) Init(args []string) error {
    77  	if len(args) == 0 {
    78  		return errors.Errorf("no username supplied")
    79  	}
    80  
    81  	c.User, args = args[0], args[1:]
    82  	if len(args) > 0 {
    83  		c.DisplayName, args = args[0], args[1:]
    84  	}
    85  	return cmd.CheckEmpty(args)
    86  }
    87  
    88  // Run implements Command.Run.
    89  func (c *addCommand) Run(ctx *cmd.Context) error {
    90  	api := c.api
    91  	if api == nil {
    92  		var err error
    93  		api, err = c.NewUserManagerAPIClient()
    94  		if err != nil {
    95  			return errors.Trace(err)
    96  		}
    97  		defer api.Close()
    98  	}
    99  
   100  	// Add a user without a password. This will generate a temporary
   101  	// secret key, which we'll print out for the user to supply to
   102  	// "juju register".
   103  	_, secretKey, err := api.AddUser(c.User, c.DisplayName, "")
   104  	if err != nil {
   105  		if params.IsCodeUnauthorized(err) {
   106  			common.PermissionsMessage(ctx.Stderr, "add a user")
   107  		}
   108  		return block.ProcessBlockedError(err, block.BlockChange)
   109  	}
   110  
   111  	displayName := c.User
   112  	if c.DisplayName != "" {
   113  		displayName = fmt.Sprintf("%s (%s)", c.DisplayName, c.User)
   114  	}
   115  	base64RegistrationData, err := generateUserControllerAccessToken(
   116  		c.ControllerCommandBase,
   117  		c.User,
   118  		secretKey,
   119  	)
   120  	if err != nil {
   121  		return errors.Annotate(err, "generating controller user access token")
   122  	}
   123  	fmt.Fprintf(ctx.Stdout, "User %q added\n", displayName)
   124  	fmt.Fprintf(ctx.Stdout, "Please send this command to %v:\n", c.User)
   125  	fmt.Fprintf(ctx.Stdout, "    juju register %s\n",
   126  		base64RegistrationData,
   127  	)
   128  	fmt.Fprintf(ctx.Stdout, `
   129  %q has not been granted access to any models. You can use "juju grant" to grant access.
   130  `, displayName)
   131  
   132  	return nil
   133  }