예제 #1
0
// If get all, just use &User{}
func GetUser(cond *Users, limit, index int) ([]*Users, error) {
	r := make([]*Users, 0)
	o := orm.NewOrm()
	q := o.QueryTable("users")
	if cond.Id != "" {
		q = q.Filter("id", cond.Id)
	}
	if cond.Name != "" {
		q = q.Filter("name", cond.Name)
	}
	if cond.ShowName != "" {
		q = q.Filter("show_name", cond.ShowName)
	}
	if cond.Password != "" {
		q = q.Filter("password", common.EncryptPassword(cond.Password))
	}
	if limit > 0 {
		q = q.Limit(limit)
	}
	if index > 0 {
		q = q.Offset(index)
	}
	_, err := q.All(&r)

	if err != nil {
		return nil, err
	}
	for _, v := range r {
		o.LoadRelated(v, "Roles", common.RelDepth)
	}
	return r, nil
}
예제 #2
0
func AddUser(a *Users) (string, error) {
	beego.Debug("[M] Got data:", a)
	o := orm.NewOrm()
	err := o.Begin()
	if err != nil {
		return "", err
	}

	a.Id = uuid.New()
	beego.Debug("[M] Got new id:", a.Id)
	a.Password = common.EncryptPassword(a.Password)
	validator := new(validation.Validation)
	valid, err := validator.Valid(a)
	if err != nil {
		o.Rollback()
		return "", err
	}
	if !valid {
		o.Rollback()
		var errS string
		for _, err := range validator.Errors {
			errS = fmt.Sprintf("%s, %s:%s", errS, err.Key, err.Message)
		}
		return "", fmt.Errorf("Bad info: %s", errS)
	}
	beego.Debug("[M] Got new data:", a)
	_, err = o.Insert(a)
	if err != nil {
		o.Rollback()
		return "", err
	}
	_, err = o.QueryM2M(a, "Roles").Add(a.Roles)
	if err != nil {
		o.Rollback()
		return "", err
	}
	beego.Debug("[M] User info saved")
	o.Commit()
	return a.Id, nil
}
예제 #3
0
// @Title updateUser
// @router /:name [put]
func (h *UserController) Put() {
	name := h.GetString(":name")
	defer h.ServeJSON()
	beego.Debug("[C] Got user name:", name)
	if name != "" {
		user := &models.Users{
			Name: name,
		}
		users, err := models.GetUser(user, 0, 0)
		if err != nil {
			h.Data["json"] = map[string]string{
				"message": fmt.Sprint("Failed to get with name:", name),
				"error":   err.Error(),
			}
			beego.Warn("[C] Got error:", err)
			h.Ctx.Output.SetStatus(http.StatusInternalServerError)
			return
		}
		if len(users) == 0 {
			beego.Debug("[C] Got nothing with name:", name)
			h.Ctx.Output.SetStatus(http.StatusNotFound)
			return
		}

		sessionId := h.GetSession("id")
		if sessionId != nil {
			userNow := &models.Users{
				Id: sessionId.(string),
			}
			userNows, err := models.GetUser(userNow, 0, 0)
			if err != nil {
				h.Data["json"] = map[string]string{
					"message": fmt.Sprint("Failed to get with name:", name),
					"error":   err.Error(),
				}
				beego.Warn("[C] Got error:", err)
				h.Ctx.Output.SetStatus(http.StatusInternalServerError)
				return
			}
			if len(userNows) == 0 {
				beego.Debug("[C] Invalid user id:", sessionId)
				h.Ctx.Output.SetStatus(http.StatusNotFound)
				return
			}
		}

		err = json.Unmarshal(h.Ctx.Input.RequestBody, user)
		if err != nil {
			beego.Warn("[C] Got error:", err)
			h.Data["json"] = map[string]string{
				"message": "Bad request",
				"error":   err.Error(),
			}
			h.Ctx.Output.SetStatus(http.StatusBadRequest)
			return
		}
		user.Id = users[0].Id
		user.Removable = users[0].Removable // Removable should not be changed.
		if user.Password != users[0].Password {
			user.Password = common.EncryptPassword(user.Password)
		}
		beego.Debug("[C] Got user data:", user)
		err = models.UpdateUser(user)
		if err != nil {
			h.Data["json"] = map[string]string{
				"message": fmt.Sprint("Failed to update with name:", name),
				"error":   err.Error(),
			}
			beego.Warn("[C] Got error:", err)
			h.Ctx.Output.SetStatus(http.StatusInternalServerError)
			return
		}
		h.Ctx.Output.SetStatus(http.StatusAccepted)
	}
}