Пример #1
0
func ListUsersHandler(w http.ResponseWriter, r *http.Request) {
	fields := ListUsersForm{}

	cef, err := form.Parse(&fields, w, r)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if cef.HasError() {
		cef.Error.Send(http.StatusBadRequest)
		return
	}

	users := []model.User{}

	s := conf.M.Copy()
	defer s.Close()

	m := bson.M{}

	if fields.Name != "" {
		//todo: do full text search instead
		m["name"] = bson.RegEx{fields.Name, "i"}
	}

	q := s.DB("").C("users").Find(m)

	totalCount, err := q.Count()
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	p := paging.Paging{
		Page:  fields.Page,
		Limit: fields.Limit,
		Count: totalCount,
	}.Calc()

	if err = q.
		Limit(p.Limit).
		Skip(p.Offset).
		Sort("-created_at").
		All(&users); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	rp := UsersResponse{
		Users:           users,
		CurrentPage:     p.Page,
		TotalPagesCount: p.TotalPages,
	}

	httpres.Json(w, http.StatusOK, rp)
}
Пример #2
0
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
	fields := CreateUserForm{}

	cef, err := form.Parse(&fields, w, r)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if cef.HasError() {
		cef.Error.Send(http.StatusBadRequest)
		return
	}

	hash, err := cryptoutils.Hash(fields.Password, conf.PasswordLevel)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	user := model.User{
		ID:        bson.NewObjectId(),
		Name:      fields.Name,
		Email:     strings.TrimSpace(fields.Email),
		Hash:      hash,
		CreatedAt: time.Now(),
	}

	s := conf.M.Copy()
	defer s.Close()

	if err := s.DB("").C("users").Insert(&user); err != nil {
		if mgo.IsDup(err) {
			cef.Error.SendMessage("this email address already exists", http.StatusBadRequest)
			return
		}
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	token := model.NewToken(user.ID)

	if err := s.DB("").C("tokens").Insert(&token); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	user.AccessToken = token.Token

	httpres.Json(w, http.StatusCreated, UserResponse{user})
}
Пример #3
0
func CreateTokenHandler(w http.ResponseWriter, r *http.Request) {
	fields := CreateTokenForm{}

	cef, err := form.Parse(&fields, w, r)
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if cef.HasError() {
		cef.Error.Send(http.StatusBadRequest)
		return
	}

	var user model.User

	s := conf.M.Copy()
	defer s.Close()

	if err := s.DB("").C("users").Find(bson.M{
		"email": strings.TrimSpace(fields.Email),
	}).One(&user); err != nil {
		if err == mgo.ErrNotFound {
			cef.Error.SendMessage("bad credentials", http.StatusBadRequest)
			return
		}
		log.Println(err)
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	if err := bcrypt.CompareHashAndPassword([]byte(user.Hash), []byte(fields.Password)); err != nil {
		cef.Error.SendMessage("bad credentials", http.StatusBadRequest)
		return
	}

	token := model.NewToken(user.ID)
	if err := s.DB("").C("tokens").Insert(&token); err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	httpres.Json(w, http.StatusCreated, TokenResponse{token.Token})
}