Пример #1
0
func handleReviewLogin(resp http.ResponseWriter, req *http.Request) {
	email := public.EmailFilter(req.FormValue("email"))
	password := req.FormValue("password")

	if len(email) <= 0 || len(password) <= 0 {
		r := public.SimpleResult{
			Message:     "Error",
			Description: "Incorrect email or password",
		}
		public.ResponseStatusAsJson(resp, 403, &r)
		return
	}

	//Check login status
	if _, err := public.GetSessionReviewerId(req); err == nil {
		r := public.SimpleResult{
			Message:     "Already Login",
			Description: email,
		}
		public.ResponseOkAsJson(resp, &r)
		return
	}

	reviewerDb := public.GetNewReviewerDatabase()
	defer reviewerDb.Session.Close()

	profiles := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)
	q := profiles.Find(bson.M{"baseprofile.email": email})
	reviewer := db.Reviewer{}
	if q.One(&reviewer) == nil {
		//Check password
		if bcrypt.CompareHashAndPassword([]byte(reviewer.BaseProfile.AuthInfo.BcyptHash), []byte(password)) != nil {
			r := public.SimpleResult{
				Message:     "Error",
				Description: "Incorrect email or password",
			}
			public.ResponseStatusAsJson(resp, 403, &r)
			return
		}

		if err := public.SetReviewerSessionValue(req, resp, public.REVIEWER_ID_SESSION_KEY, reviewer.Id.Hex()); err != nil {
			public.LogE.Printf("Error setting session user id: %s\n", err.Error())
		}
		r := public.SimpleResult{
			Message:     "Login Successed",
			Description: email,
		}
		public.ResponseOkAsJson(resp, &r)
	} else {
		r := public.SimpleResult{
			Message:     "Error",
			Description: "Incorrect email or password",
		}
		public.ResponseStatusAsJson(resp, 403, &r)
		return
	}
}
Пример #2
0
func handleReviewerProfile(resp http.ResponseWriter, req *http.Request) {
	userId, _ := public.GetSessionReviewerId(req)

	reviewerDb := public.GetNewReviewerDatabase()
	defer reviewerDb.Session.Close()

	profile := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)

	q := profile.FindId(userId)
	if c, err := q.Count(); c == 0 || err != nil {
		r := public.SimpleResult{
			Message:     "Error",
			Description: "User Not Found",
		}
		public.ResponseStatusAsJson(resp, 500, &r)
	} else {
		reviewer := db.Reviewer{}
		q.One(&reviewer)

		r := public.ReviewerProfile{
			Email:    reviewer.BaseProfile.Email,
			Username: reviewer.BaseProfile.Username,
			FormalId: reviewer.BaseProfile.FormalId,

			Topics:      reviewer.Topics,
			Permissions: reviewer.Permissions,
		}

		if client, err := storage.GetNewStorageClient(); err == nil && len(reviewer.BaseProfile.Thumbnail) > 0 {
			defer client.Close()
			expire := time.Now().Add(time.Duration(12) * time.Hour)
			if r.Thumbnail, err = client.GetNewSignedURL(reviewer.BaseProfile.Thumbnail, expire); err != nil {
				r.Thumbnail = ""
			}
		}

		public.ResponseOkAsJson(resp, &r)
	}
}
Пример #3
0
func handleSubmitReview(resp http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	appHash := vars["appHash"]

	appId, ok := exportAppHashMap[appHash]
	if !ok {
		public.ResponseStatusAsJson(resp, 404, &public.SimpleResult{
			Message:     "Error",
			Description: "Hash not found",
		})
		return
	}
	delete(exportAppHashMap, appHash)

	userId, _ := public.GetSessionReviewerId(req)

	reviewDb := public.GetNewReviewerDatabase()
	defer reviewDb.Session.Close()
	userDb := public.GetNewUserDatabase()
	defer userDb.Session.Close()

	results := reviewDb.C(REVIEWER_DB_RESULT_COLLECTION)
	profiles := reviewDb.C(REVIEWER_DB_PROFILE_COLLECTION)

	//See if exist
	//Re-submit is not allowed
	q := results.Find(bson.M{
		"applicationid": appId,
		"reviewerid":    userId,
	})
	if n, _ := q.Count(); n > 0 {
		public.ResponseStatusAsJson(resp, 403, &public.SimpleResult{
			Message:     "Error",
			Description: "Data exist",
		})
		return
	}

	//Get user profile info
	q = profiles.FindId(userId)
	user := db.User{}
	if err := q.One(&user); err != nil {
		public.ResponseStatusAsJson(resp, 404, nil)
		return
	}

	//Get review json data
	reviewData := public.ReviewResponse{}
	body, _ := ioutil.ReadAll(req.Body)

	if err := json.Unmarshal(body, &reviewData); err != nil {
		public.ResponseStatusAsJson(resp, 400, &public.SimpleResult{
			Message:     "Error",
			Description: "Wrong review response",
		})
		return
	}

	reviewResult := db.ReviewResult{
		ApplicationId: appId,
		ReviewerId:    userId,
	}
	(&reviewResult).CopyFromReviewResponse(reviewData)

	if err := results.Insert(&reviewResult); err != nil {
		public.LogE.Printf("Error inserting new review result: %s\n", err)
	}

	public.ResponseOkAsJson(resp, nil)
}
Пример #4
0
func handleGetReviewApplications(resp http.ResponseWriter, req *http.Request) {
	userId, _ := public.GetSessionReviewerId(req)

	reviewerDb := public.GetNewReviewerDatabase()
	defer reviewerDb.Session.Close()

	profile := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)

	q := profile.FindId(userId)
	if c, err := q.Count(); c == 0 || err != nil {
		r := public.SimpleResult{
			Message:     "Error",
			Description: "User Not Found",
		}
		public.ResponseStatusAsJson(resp, 500, &r)
	} else {
		reviewer := db.Reviewer{}
		q.One(&reviewer)

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

		forms := appDb.C(public.APPLICATION_DB_FORM_COLLECTION)
		results := reviewerDb.C(REVIEWER_DB_RESULT_COLLECTION)

		var exportApps []exportApplication
		for _, t := range reviewer.Topics {
			q := forms.Find(bson.M{
				"topic": t,
			}).Sort("-timestamp") //From new to old

			//Remove the duplicate application
			//Keep the latest one
			ownerSet := make(map[bson.ObjectId]bool)

			it := q.Iter()
			appData := db.ApplicationForm{}
			for it.Next(&appData) {

				if _, ok := ownerSet[appData.OwnerId]; ok {
					//Exist duplicate application
					continue
				}

				//Check if reviewed
				q_r := results.Find(bson.M{
					"applicationid": appData.Id,
				})
				if n, _ := q_r.Count(); n > 0 {
					//Has reviewed
					continue
				}

				exportApp := exportApplication{}
				(&exportApp).fromDbApplication(&appData, true)
				exportApps = append(exportApps, exportApp)

				exportAppHashMap[exportApp.Hash] = appData.Id

				ownerSet[appData.OwnerId] = true
			}
		}

		//Output reviewed topics
		public.ResponseOkAsJson(resp, &exportApps)
	}
}