Exemple #1
0
// GET: /node
// To do:
// - Iterate node queries
func (cr *NodeController) ReadMany(cx *goweb.Context) {
	// Log Request and check for Auth
	LogRequest(cx.Request)
	u, err := AuthenticateRequest(cx.Request)
	if err != nil && err.Error() != e.NoAuth {
		handleAuthError(err, cx)
		return
	}

	// Gather query params
	query := &Query{list: cx.Request.URL.Query()}

	// Setup query and nodes objects
	q := bson.M{}
	nodes := store.Nodes{}

	if u != nil {
		// Admin sees all
		if !u.Admin {
			q["$or"] = []bson.M{bson.M{"acl.read": []string{}}, bson.M{"acl.read": u.Uuid}, bson.M{"acl.owner": u.Uuid}}
		}
	} else {
		if conf.ANON_READ {
			// select on only nodes with no read rights set
			q["acl.read"] = []string{}
		} else {
			cx.RespondWithErrorMessage(e.NoAuth, http.StatusUnauthorized)
			return
		}
	}

	// Gather params to make db query. Do not include the
	// following list.
	paramlist := map[string]int{"limit": 1, "offset": 1, "query": 1, "querynode": 1}
	if query.Has("query") {
		for key, val := range query.All() {
			_, s := paramlist[key]
			if !s {
				q[fmt.Sprintf("attributes.%s", key)] = val[0]
			}
		}
	} else if query.Has("querynode") {
		for key, val := range query.All() {
			if key == "type" {
				querytypes := strings.Split(query.Value("type"), ",")
				q["type"] = bson.M{"$all": querytypes}
			} else {
				_, s := paramlist[key]
				if !s {
					q[key] = val[0]
				}
			}
		}
	}

	// defaults
	limit := 25
	offset := 0
	if query.Has("limit") {
		limit = ToInt(query.Value("limit"))
	}
	if query.Has("offset") {
		offset = ToInt(query.Value("offset"))
	}

	// Get nodes from db
	count, err := nodes.GetPaginated(q, limit, offset)
	if err != nil {
		log.Error("err " + err.Error())
		cx.RespondWithError(http.StatusBadRequest)
		return
	}
	cx.RespondWithPaginatedData(nodes, limit, offset, count)
	return
}