Esempio n. 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
}
Esempio n. 2
0
func (p *UpdateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	uac := cxt.(UpdateAccountContext)
	uac.SetFromJSON(inputObj)
	uac.CleanInput(uac.RequestingUser(), uac.OriginalValue())
	//log.Print("[UARH]: HandleJSONObjectInputHandler()")
	errors := make(map[string][]error)
	var obj interface{}
	var err error
	ds := p.ds
	switch uac.Type() {
	case "user":
		if user := uac.User(); user != nil {
			//log.Printf("[UARH]: user is not nil1: %v\n", user)
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateUserAccount(user)
				//log.Printf("[UARH]: user after errors is %v\n", user)
			}
			obj = user
			uac.SetUser(user)
			//log.Printf("[UARH]: setUser to %v\n", user)
		}
	case "consumer":
		if user := uac.Consumer(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateConsumerAccount(user)
			}
			obj = user
			uac.SetConsumer(user)
		}
	case "external_user":
		if user := uac.ExternalUser(); user != nil {
			user.Validate(false, errors)
			if len(errors) == 0 {
				user, err = ds.UpdateExternalUserAccount(user)
			}
			obj = user
			uac.SetExternalUser(user)
		}
	default:
		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)
	//log.Printf("[UARH]: obj was: \n%v\n", obj)
	//log.Printf("[UARH]: Going to output:\n%s\n", jsonObj)
	return apiutil.OutputJSONObject(jsonObj, uac.LastModified(), uac.ETag(), http.StatusOK, nil)
}
Esempio n. 3
0
func (p *UpdateContactRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	ucc := cxt.(UpdateContactContext)
	ucc.SetFromJSON(inputObj)
	ucc.CleanInput(ucc.AuthUser(), ucc.OriginalContact())
	//log.Print("[UARH]: HandleJSONObjectInputHandler()")
	errors := make(map[string][]error)
	var obj interface{}
	var err error
	contactsDS := p.contactsDS
	contact := ucc.Contact()
	origContact := ucc.OriginalContact()
	if contact != nil && origContact != nil && ucc.User() != nil {
		dsocialUserId := ucc.User().Id
		contact.Id = ucc.ContactId()
		contact.UserId = dsocialUserId
		contact.Validate(false, errors)
		if len(errors) == 0 {
			l := new(list.List)
			origContact.GenerateChanges(origContact, contact, nil, l)
			allowAdd, allowDelete, allowUpdate := true, true, true
			pipeline := bc.NewPipeline()
			l = pipeline.RemoveUnacceptedChanges(l, allowAdd, allowDelete, allowUpdate)
			changes := make([]*dm.Change, l.Len())
			for i, iter := 0, l.Front(); iter != nil; i, iter = i+1, iter.Next() {
				changes[i] = iter.Value.(*dm.Change)
			}
			changeset := &dm.ChangeSet{
				CreatedAt:      time.Now().UTC().Format(dm.UTC_DATETIME_FORMAT),
				ChangedBy:      ucc.AuthUser().Id,
				ChangeImportId: ucc.ContactId(),
				RecordId:       ucc.ContactId(),
				Changes:        changes,
			}
			_, err = contactsDS.StoreContactChangeSet(dsocialUserId, changeset)
			if err == nil {
				contact, err = contactsDS.StoreDsocialContact(contact.UserId, contact.Id, contact)
			}
		}
	}
	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, ucc.LastModified(), ucc.ETag(), http.StatusOK, nil)
}
Esempio n. 4
0
func (p *ViewAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	vac := cxt.(ViewAccountContext)

	obj := vac.ToObject()
	var err error
	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, vac.LastModified(), vac.ETag(), 0, nil)
}
Esempio n. 5
0
func (p *LoginAccountRequestHandler) HandleInputHandlerAfterSetup(lac LoginAccountContext) (int, http.Header, io.WriterTo) {
	errors := make(map[string][]error)
	user, err := lac.ValidateLogin(p.ds, p.authDS, errors)
	if len(errors) > 0 {
		if err != nil {
			return apiutil.OutputErrorMessage(err.Error(), errors, http.StatusBadRequest, nil)
		}
		return apiutil.OutputErrorMessage(ERR_VALUE_ERRORS.Error(), errors, http.StatusUnauthorized, nil)
	}
	if err == ERR_INVALID_USERNAME_PASSWORD_COMBO {
		return apiutil.OutputErrorMessage(err.Error(), nil, http.StatusUnauthorized, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage("Unable to process login request: ", nil, http.StatusInternalServerError, nil)
	}
	if user == nil {
		return apiutil.OutputErrorMessage("Unable to process login request: no such username", nil, http.StatusUnauthorized, nil)
	}
	accessKey, err := p.authDS.StoreAccessKey(dm.NewAccessKey(user.Id, ""))
	if err != nil {
		return apiutil.OutputErrorMessage("Unable to process login request: "+err.Error(), nil, http.StatusInternalServerError, nil)
	}
	obj := jsonhelper.NewJSONObject()
	obj.Set("user_id", user.Id)
	obj.Set("username", user.Username)
	obj.Set("name", user.Name)
	obj.Set("access_key_id", accessKey.Id)
	obj.Set("private_key", accessKey.PrivateKey)
	lac.SetResult(obj)
	return 0, nil, nil
}
Esempio n. 6
0
func (p *CreateAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, writer io.Writer, inputObj jsonhelper.JSONObject) (int, http.Header, os.Error) {
	cac := cxt.(CreateAccountContext)
	cac.SetFromJSON(inputObj)
	cac.CleanInput(cac.RequestingUser())

	errors := make(map[string][]os.Error)
	var obj interface{}
	var err os.Error
	ds := p.ds
	if user := cac.User(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateUserAccount(user)
		}
		obj = user
	} else if user := cac.Consumer(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateConsumerAccount(user)
		}
		obj = user
	} else if user := cac.ExternalUser(); user != nil {
		user.Validate(true, errors)
		if len(errors) == 0 {
			user, err = ds.CreateExternalUserAccount(user)
		}
		obj = user
	} else {
		return apiutil.OutputErrorMessage(writer, "\"type\" must be \"user\", \"consumer\", or \"external_user\"", nil, 400, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage(writer, "Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(writer, err.String(), nil, http.StatusInternalServerError, nil)
	}
	theobj, _ := jsonhelper.MarshalWithOptions(obj, dm.UTC_DATETIME_FORMAT)
	jsonObj, _ := theobj.(jsonhelper.JSONObject)
	return apiutil.OutputJSONObject(writer, jsonObj, cac.LastModified(), cac.ETag(), 0, nil)
}
Esempio n. 7
0
func (p *DeleteAccountRequestHandler) HandleJSONObjectInputHandler(req wm.Request, cxt wm.Context, inputObj jsonhelper.JSONObject) (int, http.Header, io.WriterTo) {
	dac := cxt.(DeleteAccountContext)

	obj := dac.ToObject()
	var err error
	if !dac.Deleted() {
		_, req, cxt, _, err = p.DeleteResource(req, cxt)
	}
	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, time.Time{}, "", 0, nil)
}
Esempio n. 8
0
func (p *LogoutAccountRequestHandler) ProcessPost(req wm.Request, cxt wm.Context) (wm.Request, wm.Context, int, http.Header, io.WriterTo, error) {
	var err error
	var code int
	var headers http.Header
	var writerTo io.WriterTo
	lac := cxt.(LogoutAccountContext)
	if lac.AccessKey() != nil {
		_, err = p.authDS.DeleteAccessKey(lac.AccessKey().Id)
	}
	httpHeaders := apiutil.AddNoCacheHeaders(nil)
	if err != nil {
		code, headers, writerTo = apiutil.OutputErrorMessage("Unable to process logout request", nil, http.StatusInternalServerError, httpHeaders)
	} else {
		code, headers, writerTo = apiutil.OutputJSONObject(nil, time.Time{}, "", http.StatusOK, httpHeaders)
	}
	return req, cxt, code, headers, writerTo, nil
}
Esempio n. 9
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)
}