Godeps/_workspace/src/gopkg.in/mgo.v2/session.go
func (db *Database) AddUser(username, password string, readOnly bool) error {
	// Try to emulate the old behavior on 2.6+
	user := &User{Username: username, Password: password}
	if db.Name == "admin" {
		if readOnly {
			user.Roles = []Role{RoleReadAny}
		} else {
			user.Roles = []Role{RoleReadWriteAny}
		}
	} else {
		if readOnly {
			user.Roles = []Role{RoleRead}
		} else {
			user.Roles = []Role{RoleReadWrite}
		}
	}
	err := db.runUserCmd("updateUser", user)
	if isNotFound(err) {
		return db.runUserCmd("createUser", user)
	}
	if !isNoCmd(err) {
		return err
	}

	// Command doesn't exist. Fallback to pre-2.6 behavior.
	psum := md5.New()
	psum.Write([]byte(username + ":mongo:" + password))
	digest := hex.EncodeToString(psum.Sum(nil))
	c := db.C("system.users")
	_, err = c.Upsert(bson.M{"user": username}, bson.M{"$set": bson.M{"user": username, "pwd": digest, "readOnly": readOnly}})
	return err
}
func (db *Database) AddUser(username, password string, readOnly bool) error {
	// Try to emulate the old behavior on 2.6+
	user := &User{Username: username, Password: password}
	if db.Name == "admin" {
		if readOnly {
			user.Roles = []Role{RoleReadAny}
		} else {
			user.Roles = []Role{RoleReadWriteAny}
		}
	} else {
		if readOnly {
			user.Roles = []Role{RoleRead}
		} else {
			user.Roles = []Role{RoleReadWrite}
		}
	}
	err := db.runUserCmd("updateUser", user)
	if isNotFound(err) {
		return db.runUserCmd("createUser", user)
	}
	if !isNoCmd(err) {
		return err
	}

	// Command doesn't exist. Fallback to pre-2.6 behavior.
	psum := md5.New()
	psum.Write([]byte(username + ":mongo:" + password))
	digest := hex.EncodeToString(psum.Sum(nil))
	c := db.C("system.users")
	_, err = c.Upsert(bson.M{"user": username}, bson.M{"$set": bson.M{"user": username, "pwd": digest, "readOnly": readOnly}})
	return err
}
func ( db * Database ) AddUser ( username , password string , readOnly bool ) error { // Try to emulate the old behavior on 2.6+ user := & User { Username : username , Password : password } if db . Name == " " { if readOnly { user . Roles = [ ] Role { RoleReadAny } } else { user . Roles = [ ] Role { RoleReadWriteAny } } } else { if readOnly { user . Roles = [ ] Role { RoleRead } } else { user . Roles = [ ] Role { RoleReadWrite } } } err := db . runUserCmd ( " " , user ) if isNotFound ( err ) { return db . runUserCmd ( " " , user ) } if ! isNoCmd ( err ) { return err } // Command doesn't exist. Fallback to pre-2.6 behavior. psum := md5 . New ( ) psum . Write ( [ ] byte ( username + " " + password ) ) digest := hex . EncodeToString ( psum . Sum ( nil ) ) c := db . C ( " " ) _ , err = c . Upsert ( bson . M { " " : username } , bson . M { " " : bson . M { " " : username , " " : digest , " " : readOnly } } ) return err }
// AddUser creates or updates the authentication credentials of user within
// the db database.
//
// WARNING: This method is obsolete and should only be used with MongoDB 2.2
// or earlier. For MongoDB 2.4 and on, use UpsertUser instead.
AddUser creates or updates the authentication credentials of user within the db database . WARNING : This method is obsolete and should only be used with MongoDB 2 . 2 or earlier . For MongoDB 2 . 4 and on use UpsertUser instead .