// AddUser adds a user to the state. func (st *State) AddUser(name, password string) (*User, error) { if !names.IsUser(name) { return nil, fmt.Errorf("invalid user name %q", name) } salt, err := utils.RandomSalt() if err != nil { return nil, err } u := &User{ st: st, doc: userDoc{ Name: name, PasswordHash: utils.UserPasswordHash(password, salt), PasswordSalt: salt, }, } ops := []txn.Op{{ C: st.users.Name, Id: name, Assert: txn.DocMissing, Insert: &u.doc, }} err = st.runTransaction(ops) if err == txn.ErrAborted { err = fmt.Errorf("user already exists") } if err != nil { return nil, err } return u, nil }
// IsValidUser returns whether user is a valid username in charm URLs. func IsValidUser(user string) bool { return names.IsUser(user) }