Пример #1
0
func upsertPermissionsHandler(mapper *pgmapper.Mapper, objectIdExtractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		objectId, err := objectIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		ids, ok := r.URL.Query()["sid"]
		entity := make(map[string]interface{})
		err = json.NewDecoder(r.Body).Decode(&entity)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if ok {
			err = mapper.Execute("SELECT insert_bulk_permissions(%v)", objectId, entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"], ids)
		} else {
			_, err = mapper.ExecuteRaw("insert into acl_entries(object_id,sid,create_permission,read_permission,update_permission,delete_permission) values($1,$2,$3,$4,$5,$6) ON CONFLICT (object_id,sid) DO UPDATE SET create_permission = $3, read_permission = $4, update_permission = $5, delete_permission = $6 where acl_entries.sid = $2 AND acl_entries.object_id = $1", objectId, entity["sid"], entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"])
		}
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
func TopicCreateHandler(mapper *pgmapper.Mapper) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		var topic = make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(&topic)
		if err != nil {
			log.Println("error while decoding new topic json: ", err)
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT add_topic(%v)", topic["id"], topic["name"], topic["description"], topic["officers"])
		if err != nil {
			log.Println("error while inserting new topic into database")
			return http.StatusBadRequest // TODO it could be an internal server error as well. need distinction
		}
		client := serviceclient.New("acl-service")
		aclEntity, _ := json.Marshal(topic)
		resp, err := client.Post("/objects", "application/json", bytes.NewReader(aclEntity), "Authorization", r.Header.Get("Authorization"))
		if err != nil {
			log.Println("error while creating acl-object: ", err)
			return http.StatusInternalServerError
		}
		if resp.StatusCode >= 300 {
			log.Println("got unexpected statuscode from acl-service while creating object: ", resp.StatusCode)
			return http.StatusInternalServerError
		}
		return http.StatusCreated
	}
	return jwtware.New(createHandler(handlerFunc))
}
Пример #3
0
func upsertMultiplePermissionsHandler(mapper *pgmapper.Mapper, sidIdExtractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		sid, err := sidIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		ids, ok := r.URL.Query()["oid"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		permissions := make(map[string]interface{})
		err = json.NewDecoder(r.Body).Decode(&permissions)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		err = mapper.Execute("SELECT insert_bulk_sid_permissions(%v)", sid, permissions["create_permission"], permissions["read_permission"], permissions["update_permission"], permissions["delete_permission"], ids)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
		return
	}
	return http.Handler(http.HandlerFunc(result))
}
Пример #4
0
func deleteMultipleObjectsHandler(mapper *pgmapper.Mapper) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		ids, ok := r.URL.Query()["oid"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
		}
		err := mapper.Execute("SELECT delete_objects(%v)", ids)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
	}
	return http.Handler(http.HandlerFunc(result))
}
func ExerciseStartHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		user := context.Get(r, "user")
		id := user.(*jwt.Token).Claims["id"]
		exerciseId, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		err = mapper.Execute("select start_exercise(%v)", exerciseId, id)
		if err != nil {
			return http.StatusNotFound
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
func TopicCreateHandler(mapper *pgmapper.Mapper) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		var topic = make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(topic)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT add_topic(%v)", topic["id"], topic["name"], topic["description"], topic["officers"])
		if err != nil {
			return http.StatusBadRequest // TODO it could be an internal server error as well. need distinction
		}
		w.WriteHeader(http.StatusCreated)
		return -1
	}
	return createHandler(handlerFunc)
}
Пример #7
0
func addObjectHandler(mapper *pgmapper.Mapper) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		entity := make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(&entity)
		if err != nil {
			log.Println("error while decoding json: ", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		err = mapper.Execute("insert into object_identities(id,parent_object) values(%v)", entity["id"], entity["parent"])
		if err != nil {
			log.Println("error while insertiing object into database: ", err)
			w.WriteHeader(http.StatusBadRequest)
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
func TopicRemoveOfficerHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		var officer string
		err = json.NewDecoder(r.Body).Decode(officer)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT remove_officer(%v)", id, officer)
		if err != nil {
			return http.StatusInternalServerError
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
func TopicAddOfficerHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		var officer string
		err = json.NewDecoder(r.Body).Decode(officer)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute(`SELECT add_officer($1,$2)`, id, officer)
		if err != nil {
			return http.StatusInternalServerError
		}
		return -1
	}
	return createHandler(handlerFunc)
}
func ExerciseStartHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		var exerciseId string
		err = json.NewDecoder(r.Body).Decode(exerciseId)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("insert into exercise_progress_histories(user_id,exercise_id,amount,time,state) values(%v)", id, exerciseId, 0, time.Now(), 1)
		if err != nil {
			return http.StatusNotFound
		}
		return -1
	}
	return createHandler(handlerFunc)
}