// Builds a user from his Id and information in http_header. func BuildUser(db *mgo.Database, ev ifaces.Event, user_id bson.ObjectId, http_header map[string][]string) (map[string]interface{}, error) { user, err := FindUser(db, user_id) if err != nil || user == nil { user = EmptyUser() } _, langs_are_set := user["languages"] if !langs_are_set { langs, has := http_header["Accept-Language"] if has { user["languages"] = ParseAcceptLanguage(langs[0]) } else { user["languages"] = []string{"en"} } } ev.Trigger("user.build", user) return user, nil }
// Registers a normal user with password and level 100. // See RegisterGuest for an other kind of registration. // See admin_model for registrations of admins. func RegisterUser(db *mgo.Database, ev ifaces.Event, rules map[string]interface{}, inp map[string][]string) (bson.ObjectId, error) { userDefaults(rules) user, err := extract.New(rules).Extract(inp) if err != nil { return "", err } if user["password"].(string) != user["password_again"].(string) { return "", fmt.Errorf("Password and password confirmation differs.") } delete(user, "password_again") user["password"] = EncodePass(user["password"].(string)) user["slug"] = slugify.S(user["slug"].(string)) user["level"] = 100 user_id := bson.NewObjectId() user["_id"] = user_id err = db.C("users").Insert(user) if err != nil { return "", fmt.Errorf("Name is not unique.") } delete(user, "password") ev.Trigger("user.register", user) return user_id, nil }
// At update uses $set, does not replace document. func InudOpt(db *mgo.Database, ev ifaces.Event, dat map[string]interface{}, coll, op, id string, version bool) error { var err error if (op == "update" || op == "delete") && len(id) != 24 { if len(id) == 39 { id = id[13:37] } else { return fmt.Errorf("Length of id is not 24 or 39 at updating or deleting.") } } switch op { case "insert": ins_id := bson.NewObjectId() version_id := bson.NewObjectId() dat["_id"] = ins_id // At an insert, the parent can be only a draft. dat["pointing_to"] = version_id draft_i, has_draft := dat["draft_id"] var parent, root bson.ObjectId if has_draft && len(draft_i.(string)) > 0 { draft_id := ToIdWithCare(draft_i.(string)) parent, root, _, err = GetDraftParent(db, coll, draft_id) if err != nil { return err } } if parent == "" { // Parent is "" if we are not coming from a draft. dat["root"] = version_id } else { dat["root"] = root } err = db.C(coll).Insert(dat) if err != nil { return err } if version { err = SaveVersion(db, coll, version_id, ins_id, parent, root) } case "update": live_id := bson.ObjectIdHex(id) version_id := bson.NewObjectId() var parent, root bson.ObjectId var err error draft_i, has_draft := dat["draft_id"] if has_draft && len(draft_i.(string)) > 0 { draft_id := ToIdWithCare(draft_i.(string)) parent, root, _, err = GetDraftParent(db, coll, draft_id) } else { parent, root, err = GetParentTroughContent(db, coll, live_id) } if err != nil { return err } q := bson.M{"_id": live_id} dat["pointing_to"] = version_id // Points to the new version now. if root != "" { dat["root"] = root } upd := bson.M{"$set": dat} err = db.C(coll).Update(q, upd) if err != nil { return err } if version { err = SaveVersion(db, coll, version_id, live_id, parent, root) } case "delete": live_id := bson.ObjectIdHex(id) err = Delete(db, coll, live_id) case "restore": // err = db.C(coll).Find // Not implemented yet. } if err != nil { return err } ev.Trigger(coll+"."+op, dat) return nil }