Esempio n. 1
0
func setUserAttribute(user content.User, secret []byte, attr string, data []byte) (resp responseError) {
	resp = newResponse()
	in := user.Data()
	resp.val["Login"] = in.Login

	switch attr {
	case "FirstName":
		resp.err = json.Unmarshal(data, &in.FirstName)
	case "LastName":
		resp.err = json.Unmarshal(data, &in.LastName)
	case "Email":
		resp.err = json.Unmarshal(data, &in.Email)
	case "ProfileData":
		if resp.err = json.Unmarshal(data, &in.ProfileData); resp.err == nil {
			in.ProfileJSON = []byte{}
		}
	case "Active":
		in.Active = string(data) == "true"
	case "Password":
		passwd := struct {
			Current string
			New     string
		}{}
		if resp.err = json.Unmarshal(data, &passwd); resp.err != nil {
			/* TODO: non-fatal error */
			return
		}
		if user.Authenticate(passwd.Current, secret) {
			user.Password(passwd.New, secret)
			resp.err = user.Err()
		} else {
			resp.err = errors.New("Error change user password: current password is invalid")
		}
	default:
		resp.err = errors.New("Error getting user attribute: unknown attribute " + attr)
	}

	if resp.err != nil {
		return
	}

	user.Data(in)
	user.Update()
	if resp.err = user.Err(); resp.err != nil {
		return
	}

	resp.val["Success"] = true
	resp.val["Attribute"] = attr

	return
}