Exemplo n.º 1
0
func handleInspectStagingRecomm(resp http.ResponseWriter, req *http.Request) {
	user_perm, _ := public.GetSessionUserPermission(req)

	if !user_perm.ContainsPermission(public.USER_PERMISSION_RECOMM) {
		public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
			Message: "Not Recommender",
		})
		return
	}

	switch strings.ToLower(req.Method) {
	case "get":
		viewStagingRecomm(resp, req)
		break

	case "post":
		editStagingRecomm(resp, req)
		break
	case "put":
		editStagingRecomm(resp, req)
		break

	case "delete":
		removeStagingRecomm(resp, req)
		break

	default:
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message: "No such http method",
		})
	}
}
Exemplo n.º 2
0
func handleRecommFileUpload(resp http.ResponseWriter, req *http.Request) {
	user_perm, _ := public.GetSessionUserPermission(req)

	if user_perm.ContainsPermission(public.USER_PERMISSION_RECOMM) {

		handleFormFileUpload(resp, req)

	} else {
		public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
			Message: "Not Recommender",
		})
	}
}
Exemplo n.º 3
0
func handleSubmitRecomm(resp http.ResponseWriter, req *http.Request) {

	user_id, _ := public.GetSessionUserId(req)
	user_perm, _ := public.GetSessionUserPermission(req)

	vars := mux.Vars(req)
	hash_str := vars["hash"]

	stagingDb := public.GetNewStagingDatabase()
	defer stagingDb.Session.Close()

	staging_recomm := stagingDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)

	q := staging_recomm.Find(bson.M{
		"hash": hash_str,
	})

	recomm_result := db.RecommEntity{}
	if e := q.One(&recomm_result); e != nil {
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message: "No such recomm entity",
		})
		return
	}

	// Permission check
	if recomm_result.Recommender != user_id && user_perm != public.USER_PERMISSION_GM {
		public.ResponseStatusAsJson(resp, 403, nil)
		return
	}

	// Migrate from staging db to application db
	staging_recomm.RemoveId(recomm_result.Id)

	appDb := public.GetNewApplicationDatabase()
	defer appDb.Session.Close()

	recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
	new_recomm := recomm_result
	new_recomm.Id = bson.NewObjectId()
	new_recomm.LastModified = time.Now()

	if e := recomm.Insert(new_recomm); e != nil {
		public.LogE.Printf("Error migrating recomm from staging db to application db: %s\n", e.Error())
		public.ResponseStatusAsJson(resp, 500, &public.SimpleResult{
			Message: "Submit failed",
		})
	}
}
Exemplo n.º 4
0
func handleViewRecomm(resp http.ResponseWriter, req *http.Request) {
	user_id, _ := public.GetSessionUserId(req)
	user_perm, _ := public.GetSessionUserPermission(req)

	vars := mux.Vars(req)
	hash_str := vars["hash"]

	appDb := public.GetNewApplicationDatabase()
	defer appDb.Session.Close()

	recomm := appDb.C(public.APPLICATION_DB_RECOMM_COLLECTION)
	q := recomm.Find(bson.M{
		"hash": hash_str,
	})

	recomm_result := db.RecommEntity{}
	if e := q.One(&recomm_result); e != nil {
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message: "No such recomm entity",
		})
		return
	}

	// Permission check
	if recomm_result.Recommender != user_id && user_perm != public.USER_PERMISSION_GM {
		public.ResponseStatusAsJson(resp, 403, nil)
		return
	}

	signed_url := ""
	if client, err := storage.GetNewStorageClient(); err == nil {
		expireTime := time.Now().Add(time.Duration(1) * time.Hour) //an hour
		signed_url, _ = client.GetNewSignedURL(string(recomm_result.Attachment), expireTime)
	}

	public.ResponseOkAsJson(resp, &public.RecommView{
		Hash:         recomm_result.Hash,
		ApplyUser:    recomm_result.ApplyUser,
		Topic:        recomm_result.Topic,
		LastModified: recomm_result.LastModified,
		Content:      recomm_result.Content,
		Attachment:   signed_url,
	})
}
Exemplo n.º 5
0
func handleRecommDoorBell(resp http.ResponseWriter, req *http.Request) {
	_, user_err := public.GetSessionUserId(req)

	if user_err != nil {
		public.ResponseStatusAsJson(resp, 403, nil)
		return
	}

	user_perm, _ := public.GetSessionUserPermission(req)

	if !user_perm.ContainsPermission(public.USER_PERMISSION_RECOMM) {
		public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
			Message: "Not Recommender",
		})
		return
	}

	public.ResponseOkAsJson(resp, &public.SimpleResult{
		Message: "Ok",
	})
}
Exemplo n.º 6
0
func handleInspectRecomm(resp http.ResponseWriter, req *http.Request) {
	user_perm, _ := public.GetSessionUserPermission(req)

	if !user_perm.ContainsPermission(public.USER_PERMISSION_RECOMM) {
		public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
			Message: "Not Recommender",
		})
		return
	}

	switch strings.ToLower(req.Method) {

	case "get":
		{
			// View formal recommendation
			handleViewRecomm(resp, req)
			break
		}

	case "put":
		{
			// Submit as formal recommendation
			handleSubmitRecomm(resp, req)
			break
		}
	case "post":
		{
			// Submit as formal recommendation
			handleSubmitRecomm(resp, req)
			break
		}

	default:
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message: "No such http method",
		})

	}
}