Example #1
0
// Returns an error if user_id is not the author of the parent of the chosen document.
func IsAuthor(db *mgo.Database, coll, parent_fieldname string, user_id, chosen_doc_id bson.ObjectId) (bson.ObjectId, error) {
	chosen_doc, err := patterns.FindEq(db, coll, "_id", chosen_doc_id)
	if err != nil {
		return "", err
	}
	parent_id := chosen_doc[parent_fieldname]
	// Check if author of equals to user_id, because only the parent author can chose a child.
	parent_doc, err := patterns.FindEq(db, coll, "_id", parent_id)
	if err != nil {
		return "", err
	}
	owner := parent_doc["_users_created_by"].(bson.ObjectId) == user_id
	if !owner {
		return "", fmt.Errorf("You can only choose a child of your own document.")
	}
	return parent_id.(bson.ObjectId), nil
}
Example #2
0
func FindTag(db *mgo.Database, field string, value interface{}) (map[string]interface{}, error) {
	return patterns.FindEq(db, "tags", field, value)
}