Remove errorcase from single searchterm logic

This commit is contained in:
Christoph Kluge 2023-06-22 10:58:36 +02:00
parent fc33bfb47b
commit bcadb1adda
2 changed files with 22 additions and 21 deletions

View File

@ -523,11 +523,10 @@ var ErrForbidden = errors.New("not authorized")
// If query is found to be an integer (= conversion to INT datatype succeeds), skip back to parent call // If query is found to be an integer (= conversion to INT datatype succeeds), skip back to parent call
// If nothing matches the search, `ErrNotFound` is returned. // If nothing matches the search, `ErrNotFound` is returned.
func (r *JobRepository) FindUserOrProjectOrJobname(ctx context.Context, searchterm string) (username string, project string, metasnip string, err error) { func (r *JobRepository) FindUserOrProjectOrJobname(user *auth.User, searchterm string) (username string, project string, metasnip string, err error) {
if _, err := strconv.Atoi(searchterm); err == nil { // Return empty on successful conversion: parent method will redirect for integer jobId if _, err := strconv.Atoi(searchterm); err == nil { // Return empty on successful conversion: parent method will redirect for integer jobId
return "", "", "", nil return "", "", "", nil
} else { // Has to have letters and logged-in user for other guesses } else { // Has to have letters and logged-in user for other guesses
user := auth.GetUser(ctx)
if user != nil { if user != nil {
// Find username in jobs (match) // Find username in jobs (match)
uresult, _ := r.FindColumnValue(user, searchterm, "job", "user", "user", false) uresult, _ := r.FindColumnValue(user, searchterm, "job", "user", "user", false)

View File

@ -278,55 +278,57 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, api *api.RestApi)
if len(splitSearch) == 2 { if len(splitSearch) == 2 {
switch strings.Trim(splitSearch[0], " ") { switch strings.Trim(splitSearch[0], " ") {
case "jobId": case "jobId":
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusTemporaryRedirect) // All Users: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound) // All Users: Redirect to Tablequery
case "jobName": case "jobName":
http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusTemporaryRedirect) // All Users: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound) // All Users: Redirect to Tablequery
case "projectId": case "projectId":
http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusTemporaryRedirect) // All Users: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound) // All Users: Redirect to Tablequery
case "username": case "username":
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusTemporaryRedirect) http.Redirect(rw, r, "/monitoring/users/?user="+url.QueryEscape(strings.Trim(splitSearch[1], " ")), http.StatusFound)
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) // Users: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery
} }
case "name": case "name":
usernames, _ := api.JobRepository.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name") usernames, _ := api.JobRepository.FindColumnValues(user, strings.Trim(splitSearch[1], " "), "user", "username", "name")
if len(usernames) != 0 { if len(usernames) != 0 {
joinedNames := strings.Join(usernames, "&user=") joinedNames := strings.Join(usernames, "&user=")
http.Redirect(rw, r, "/monitoring/users/?user="+joinedNames, http.StatusTemporaryRedirect) http.Redirect(rw, r, "/monitoring/users/?user="+joinedNames, http.StatusFound)
} else { } else {
if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) { if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleManager}) {
http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusTemporaryRedirect) http.Redirect(rw, r, "/monitoring/users/?user=NoUserNameFound", http.StatusPermanentRedirect)
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) // Users: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Users: Redirect to Tablequery
} }
} }
default: default:
log.Warnf("Searchbar type parameter '%s' unknown", strings.Trim(splitSearch[0], " ")) log.Warnf("Searchbar type parameter '%s' unknown", strings.Trim(splitSearch[0], " "))
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) // Unknown: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery
} }
} else if len(splitSearch) == 1 { } else if len(splitSearch) == 1 {
username, project, jobname, err := api.JobRepository.FindUserOrProjectOrJobname(r.Context(), strings.Trim(search, " "))
if err != nil { username, project, jobname, _ := api.JobRepository.FindUserOrProjectOrJobname(user, strings.Trim(search, " "))
log.Errorf("Error while searchbar best guess: %v", err.Error())
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) // Unknown: Redirect to Tablequery /* Causes 'http: superfluous response.WriteHeader call' causing SSL error and frontend crash: Cause unknown*/
} // if err != nil {
// log.Errorf("Error while searchbar best guess: %v", err.Error())
// http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery
//}
if username != "" { if username != "" {
http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusTemporaryRedirect) // User: Redirect to user page http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page
} else if project != "" { } else if project != "" {
http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(strings.Trim(search, " ")), http.StatusTemporaryRedirect) // projectId (equal) http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // projectId (equal)
} else if jobname != "" { } else if jobname != "" {
http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(strings.Trim(search, " ")), http.StatusTemporaryRedirect) // JobName (contains) http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // JobName (contains)
} else { } else {
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusTemporaryRedirect) // No Result: Probably jobId http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // No Result: Probably jobId
} }
} else { } else {
log.Warnf("Searchbar query parameters malformed: %v", search) log.Warnf("Searchbar query parameters malformed: %v", search)
http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusTemporaryRedirect) // Unknown: Redirect to Tablequery http.Redirect(rw, r, "/monitoring/jobs/?", http.StatusPermanentRedirect) // Unknown: Redirect to Tablequery
} }
} else { } else {