func GetUserByToken(token string) Domain.User { collection := DAO.Collection("User") user := Domain.User{} collection.Find(bson.M{ "token": token, }).One(&user) return user }
func AddUser(user Domain.User) { user.Token = "" user.Forms = []Domain.Form{} user.Password = Hash(user.Password) collection := DAO.Collection("User") collection.Insert(user) }
func GetPrivateKey() string { collection := DAO.Collection("Keys") private := bson.M{} collection.Find(bson.M{ "name": "private", }).One(&private) result, ok := private["value"].(string) if ok { return result } else { return "" } }
func GetPublicKey() string { collection := DAO.Collection("Keys") public := bson.M{} collection.Find(bson.M{ "name": "public", }).One(&public) result, ok := public["value"].(string) if ok { return result } else { return "" } }
func Login(email string, password string) string { password = Hash(password) collection := DAO.Collection("User") count, _ := collection.Find(bson.M{ "email": email, "password": password, }).Count() if count != 1 { return "" } token := Hash(time.Now().String() + password) collection.Update(bson.M{ "email": email, "password": password, }, bson.M{"$set": bson.M{ "token": token, }}) return token }
func AddForm(user Domain.User, form Domain.Form) { collection := DAO.Collection("User") collection.Update(user, bson.M{"$set": bson.M{ "forms": append(user.Forms, form), }}) }
func RemoveUser(user Domain.User) { collection := DAO.Collection("User") collection.Remove(user) }