예제 #1
0
// Saves the information for a user.
func (auth *Authenticator) SaveUser(user *User) error {
	user.Channels = ch.SimplifyChannels(user.Channels, true)
	if user.Password != nil {
		user.SetPassword(*user.Password)
		user.Password = nil
	}
	if err := user.Validate(); err != nil {
		return err
	}
	if (user.Name == "") != (user.PasswordHash == nil) {
		// Real user must have a password; anon user must not have a password
		return &base.HTTPError{http.StatusBadRequest, "Invalid password"}
	}

	if err := auth.bucket.Set(docIDForUser(user.Name), 0, user); err != nil {
		return err
	}
	if user.Email != "" {
		info := userByEmailInfo{user.Name}
		if err := auth.bucket.Set(docIDForUserEmail(user.Email), 0, info); err != nil {
			return err
		}
		//FIX: Fail if email address is already registered to another user
		//FIX: Unregister old email address if any
	}
	return nil
}
예제 #2
0
func (h *handler) handleChanges() error {
	// http://wiki.apache.org/couchdb/HTTP_database_API#Changes
	var options db.ChangesOptions
	options.Since = h.getIntQuery("since", 0)
	options.Limit = int(h.getIntQuery("limit", 0))
	options.Conflicts = (h.getQuery("style") == "all_docs")
	options.IncludeDocs = (h.getBoolQuery("include_docs"))

	// Get the channels as parameters to an imaginary "bychannel" filter.
	// The default is all channels the user can access.
	userChannels := h.user.Channels
	filter := h.getQuery("filter")
	if filter != "" {
		if filter != "sync_gateway/bychannel" {
			return &base.HTTPError{http.StatusBadRequest, "Unknown filter; try sync_gateway/bychannel"}
		}
		channelsParam := h.getQuery("channels")
		if channelsParam == "" {
			return &base.HTTPError{http.StatusBadRequest, "Missing 'channels' filter parameter"}
		}
		userChannels = channels.SimplifyChannels(strings.Split(channelsParam, ","), true)
		userChannels = h.user.ExpandWildCardChannel(userChannels)
		if err := h.user.AuthorizeAllChannels(userChannels); err != nil {
			return err
		}
	}

	switch h.getQuery("feed") {
	case "continuous":
		return h.handleContinuousChanges(userChannels, options)
	case "longpoll":
		options.Wait = true
	}
	return h.handleSimpleChanges(userChannels, options)
}
예제 #3
0
파일: user.go 프로젝트: jchris/sync_gateway
// Creates a new User object.
func NewUser(username string, password string, channels []string) (*User, error) {
	user := &User{Name: username, Channels: ch.SimplifyChannels(channels, true)}
	user.SetPassword(password)
	if err := user.Validate(); err != nil {
		return nil, err
	}
	return user, nil
}
예제 #4
0
파일: auth.go 프로젝트: jchris/sync_gateway
// Saves the information for a user.
func (auth *Authenticator) SaveUser(user *User) error {
	user.Channels = ch.SimplifyChannels(user.Channels, true)
	if user.Password != nil {
		user.SetPassword(*user.Password)
		user.Password = nil
	}
	if err := user.Validate(); err != nil {
		return err
	}
	if err := auth.bucket.Set(docIDForUser(user.Name), 0, user); err != nil {
		return err
	}
	if user.Email != "" {
		info := userByEmailInfo{user.Name}
		if err := auth.bucket.Set(docIDForUserEmail(user.Email), 0, info); err != nil {
			return err
		}
		//FIX: Fail if email address is already registered to another user
		//FIX: Unregister old email address if any
	}
	return nil
}