示例#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
}
示例#2
0
func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]error)
	var obj map[string]interface{}
	var accessKey *dm.AccessKey
	var err error
	ds := p.ds
	authDS := p.authDS
	if user := cac.User(); user != nil {
		var userPassword *dm.UserPassword
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, ""))
			}
		}
		if cac.Password() != "" && user != nil && user.Id != "" {
			userPassword = dm.NewUserPassword(user.Id, cac.Password())
			userPassword.Validate(true, errors)
			if len(errors) == 0 && err == nil {
				userPassword, err = authDS.StoreUserPassword(userPassword)
			}
		}
		obj = make(map[string]interface{})
		obj["user"] = user
		obj["type"] = "user"
		obj["key"] = accessKey
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey("", user.Id))
			}
		}
		obj = make(map[string]interface{})
		obj["consumer"] = user
		obj["type"] = "consumer"
		obj["key"] = accessKey
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
			if err == nil && user != nil {
				accessKey, err = authDS.StoreAccessKey(dm.NewAccessKey(user.Id, user.ConsumerId))
			}
		}
		obj = make(map[string]interface{})
		obj["external_user"] = user
		obj["type"] = "external_user"
		obj["key"] = accessKey
	} else {
		return apiutil.OutputErrorMessage("\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}