Ejemplo n.º 1
0
func CreateUser(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get parameter Domain
	udomain := req.FormValue("User-Domain")
	if udomain == "" {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing User-Domain parameter",
			Code:   http.StatusNotAcceptable,
		}, nil)
		return
	}

	uin, ok := GetUserInfo(req)
	if !ok {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing User parameters, check documentation.",
			Code:   http.StatusNotAcceptable,
		}, uin)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	// Check if user already exists.
	retErr := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: udomain,
		ID:          uin.UUID,
	}, &User{})
	if retErr == nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "User already registered.",
			Code:   http.StatusForbidden,
		}, nil)
		return
	}

	_, putErr := retrievable.PlaceEntity(ctx, StorageKey{
		LoginDomain: udomain,
		ID:          uin.UUID,
	}, &uin)
	if putErr != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Internal Services Error.",
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, nil)
}
Ejemplo n.º 2
0
func SelectStateFromRecord(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get Parameters
	sinfo, ok := GetStorageInfo(req)
	if !ok {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing Parameters, check documentation.",
			Code:   http.StatusNotAcceptable,
		}, sinfo)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	// Ensure user exists
	u := User{}
	getErr1 := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: sinfo.LoginDomain,
		ID:          sinfo.UUID,
	}, &u)
	if getErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "No such user exists.",
			Code:   http.StatusNotFound,
		}, nil)
		return
	}

	// Get Record
	r := Record{}
	getErr2 := retrievable.GetFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if getErr2 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Success",
		}, struct {
			Status string
			User
		}{"User Not Logged In", u})
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, struct {
		Status string
		User
	}{"User Logged In", u})
}
Ejemplo n.º 3
0
func QueryUser(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get parameter Domain
	udomain := req.FormValue("User-Domain")
	if udomain == "" {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing User-Domain parameter",
			Code:   http.StatusNotAcceptable,
		}, nil)
		return
	}

	uin, _ := GetUserInfo(req)
	if uin.UUID == "" {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing UUID parameter.",
			Code:   http.StatusNotAcceptable,
		}, nil)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	getErr := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: udomain,
		ID:          uin.UUID,
	}, &uin)

	if getErr != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: getErr.Error(),
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, uin)
}
Ejemplo n.º 4
0
func ToggleStateFromRecord(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get Parameters
	sinfo, ok := GetStorageInfo(req)
	if !ok {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing Parameters, check documentation.",
			Code:   http.StatusNotAcceptable,
		}, sinfo)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	// Ensure user exists
	u := User{}
	getErr1 := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: sinfo.LoginDomain,
		ID:          sinfo.UUID,
	}, &u)
	if getErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "No such user exists.",
			Code:   http.StatusNotFound,
		}, nil)
		return
	}

	// Get Record
	r := Record{}
	getErr2 := retrievable.GetFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if getErr2 != nil { // user is not logged in, lets do that.
		r = NewRecord(sinfo.UUID, sinfo.Domain)
		_, putErr1 := retrievable.PlaceInDatastore(ctx, StorageKey{
			Domain: sinfo.Domain,
			ID:     sinfo.UUID,
		}, &r)
		if putErr1 != nil {
			ServeJsonOfStruct(res, JsonOptions{
				Status: "Failure",
				Reason: "Internal Services Error (ts1)",
				Code:   http.StatusInternalServerError,
			}, nil)
			return
		}

		ServeJsonOfStruct(res, JsonOptions{
			Status: "Success",
		}, struct {
			Status string
			User
		}{
			"User is Logged In",
			u,
		})
		return
	}
	// User is logged in, lets move them to logout.
	r.Out = time.Now()

	delErr1 := retrievable.DeleteFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if delErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Internal Services Error (ts2)",
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	_, putErr2 := retrievable.PlaceInDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     fmt.Sprint(r.UUID, "-", r.In),
	}, &r)
	if putErr2 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Internal Services Error (ts3)",
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, struct {
		Status string
		User
	}{
		"User is Logged out",
		u,
	})
}