Ejemplo n.º 1
0
func (u *User) Insert() (code int, err error) {
	mConn := mymongo.Conn()
	defer mConn.Close()

	c := mConn.DB("").C("users")
	err = c.Insert(u)

	if err != nil {
		if mgo.IsDup(err) {
			code = ErrDupRows
		} else {
			code = ErrDatabase
		}
	} else {
		code = 0
	}
	return
}
Ejemplo n.º 2
0
func (u *User) FindById(id string) (code int, err error) {
	mConn := mymongo.Conn()
	defer mConn.Close()

	c := mConn.DB("").C("users")
	err = c.FindId(id).One(u)

	if err != nil {
		if err == mgo.ErrNotFound {
			code = ErrNotFound
		} else {
			code = ErrDatabase
		}
	} else {
		code = 0
	}
	return
}
Ejemplo n.º 3
0
func ChangePass(id, oldPass, newPass string) (code int, err error) {
	mConn := mymongo.Conn()
	defer mConn.Close()

	c := mConn.DB("").C("users")
	u := User{}
	err = c.FindId(id).One(&u)
	if err != nil {
		if err == mgo.ErrNotFound {
			return ErrNotFound, err
		} else {
			return ErrDatabase, err
		}
	}

	oldHash, err := generatePassHash(oldPass, u.Salt)
	if err != nil {
		return ErrSystem, err
	}
	newSalt, err := generateSalt()
	if err != nil {
		return ErrSystem, err
	}
	newHash, err := generatePassHash(newPass, newSalt)
	if err != nil {
		return ErrSystem, err
	}

	err = c.Update(bson.M{"_id": id, "password": oldHash},
		bson.M{"$set": bson.M{"password": newHash, "salt": newSalt}})
	if err != nil {
		if err == mgo.ErrNotFound {
			return ErrNotFound, err
		} else {
			return ErrDatabase, err
		}
	}

	return 0, nil
}