Example #1
0
func (p *SetPasswordRequestHandler) HandleInputHandlerAfterSetup(cxt SetPasswordContext) (int, http.Header, io.WriterTo) {
	errors := make(map[string][]error)
	var obj jsonhelper.JSONObject
	var err error
	authDS := p.authDS
	if user := cxt.User(); user != nil {
		var userPassword *dm.UserPassword
		if user != nil {
			userPassword = dm.NewUserPassword(user.Id, cxt.Password())
		} else {
			userPassword = dm.NewUserPassword("", cxt.Password())
		}
		userPassword.Validate(true, errors)
		if len(errors) == 0 {
			userPassword, err = authDS.StoreUserPassword(userPassword)
		}
		obj = jsonhelper.NewJSONObject()
		userObj, _ := jsonhelper.Marshal(user)
		obj.Set("user", userObj)
		obj.Set("type", "user")
		obj.Set("message", "password changed")
	} else {
		return apiutil.OutputErrorMessage(ERR_MUST_SPECIFY_USERNAME.Error(), time.Time{}, http.StatusBadRequest, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), time.Time{}, http.StatusInternalServerError, nil)
	}
	cxt.SetResult(obj)
	return 0, nil, nil
}