// AddUser adds a user to the state. func (st *State) AddUser(username, displayName, password, creator string) (*User, error) { if !names.IsUser(username) { return nil, errors.Errorf("invalid user name %q", username) } salt, err := utils.RandomSalt() if err != nil { return nil, err } timestamp := time.Now().Round(time.Second).UTC() u := &User{ st: st, doc: userDoc{ Name: username, DisplayName: displayName, PasswordHash: utils.UserPasswordHash(password, salt), PasswordSalt: salt, CreatedBy: creator, DateCreated: timestamp, }, } ops := []txn.Op{{ C: st.users.Name, Id: username, Assert: txn.DocMissing, Insert: &u.doc, }} err = st.runTransaction(ops) if err == txn.ErrAborted { err = errors.New("user already exists") } if err != nil { return nil, errors.Trace(err) } return u, nil }
func (c *Client) AddUser(username, displayName, password string) error { if !names.IsUser(username) { return fmt.Errorf("invalid user name %q", username) } userArgs := params.ModifyUsers{ Changes: []params.ModifyUser{{Username: username, DisplayName: displayName, Password: password}}, } results := new(params.ErrorResults) err := c.call("AddUser", userArgs, results) if err != nil { return err } return results.OneError() }
// IsValidUser returns whether user is a valid username in charm URLs. func IsValidUser(user string) bool { return names.IsUser(user) }
func (s *userSuite) TestIsUser(c *gc.C) { for i, t := range validTests { c.Logf("test %d: %s", i, t.string) c.Assert(names.IsUser(t.string), gc.Equals, t.expect, gc.Commentf("%s", t.string)) } }