mirror of
				https://github.com/ClusterCockpit/cc-backend
				synced 2025-10-31 07:55:06 +01:00 
			
		
		
		
	| @@ -517,48 +517,35 @@ func (r *JobRepository) WaitForArchiving() { | |||||||
| 	r.archivePending.Wait() | 	r.archivePending.Wait() | ||||||
| } | } | ||||||
|  |  | ||||||
| var ErrNotFound = errors.New("no such jobname, project or user") | func (r *JobRepository) FindUserOrProjectOrJobname(user *auth.User, searchterm string) (jobid string, username string, project string, jobname string) { | ||||||
| var ErrForbidden = errors.New("not authorized") |  | ||||||
|  |  | ||||||
| // FindJobnameOrUserOrProject returns a jobName or a username or a projectId if a jobName or user or project matches the search term. |  | ||||||
| // 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. |  | ||||||
|  |  | ||||||
| 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 searchterm, "", "", "" | ||||||
| 	} else { // Has to have letters and logged-in user for other guesses | 	} else { // Has to have letters and logged-in user for other guesses | ||||||
| 		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) | ||||||
| 			if uresult != "" { | 			if uresult != "" { | ||||||
| 				return uresult, "", "", nil | 				return "", uresult, "", "" | ||||||
| 			} | 			} | ||||||
| 			// Find username by name (like) | 			// Find username by name (like) | ||||||
| 			nresult, _ := r.FindColumnValue(user, searchterm, "user", "username", "name", true) | 			nresult, _ := r.FindColumnValue(user, searchterm, "user", "username", "name", true) | ||||||
| 			if nresult != "" { | 			if nresult != "" { | ||||||
| 				return nresult, "", "", nil | 				return "", nresult, "", "" | ||||||
| 			} | 			} | ||||||
| 			// Find projectId in jobs (match) | 			// Find projectId in jobs (match) | ||||||
| 			presult, _ := r.FindColumnValue(user, searchterm, "job", "project", "project", false) | 			presult, _ := r.FindColumnValue(user, searchterm, "job", "project", "project", false) | ||||||
| 			if presult != "" { | 			if presult != "" { | ||||||
| 				return "", presult, "", nil | 				return "", "", presult, "" | ||||||
| 			} |  | ||||||
| 			// Still no return (or not authorized for above): Try JobName |  | ||||||
| 			// Match Metadata, on hit, parent method redirects to jobName GQL query |  | ||||||
| 			err := sq.Select("job.cluster").Distinct().From("job"). |  | ||||||
| 				Where("job.meta_data LIKE ?", "%"+searchterm+"%"). |  | ||||||
| 				RunWith(r.stmtCache).QueryRow().Scan(&metasnip) |  | ||||||
| 			if err != nil && err != sql.ErrNoRows { |  | ||||||
| 				return "", "", "", err |  | ||||||
| 			} else if err == nil { |  | ||||||
| 				return "", "", metasnip[0:1], nil |  | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 		return "", "", "", ErrNotFound | 		// Return searchterm if no match before: Forward as jobname query to GQL in handleSearchbar function | ||||||
|  | 		return "", "", "", searchterm | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | var ErrNotFound = errors.New("no such jobname, project or user") | ||||||
|  | var ErrForbidden = errors.New("not authorized") | ||||||
|  |  | ||||||
| func (r *JobRepository) FindColumnValue(user *auth.User, searchterm string, table string, selectColumn string, whereColumn string, isLike bool) (result string, err error) { | func (r *JobRepository) FindColumnValue(user *auth.User, searchterm string, table string, selectColumn string, whereColumn string, isLike bool) (result string, err error) { | ||||||
| 	compareStr := " = ?" | 	compareStr := " = ?" | ||||||
| 	query := searchterm | 	query := searchterm | ||||||
|   | |||||||
| @@ -308,21 +308,20 @@ func HandleSearchBar(rw http.ResponseWriter, r *http.Request, buildInfo web.Buil | |||||||
| 			} | 			} | ||||||
| 		} else if len(splitSearch) == 1 { | 		} else if len(splitSearch) == 1 { | ||||||
|  |  | ||||||
| 			username, project, jobname, err := repo.FindUserOrProjectOrJobname(user, strings.Trim(search, " ")) | 			jobid, username, project, jobname := repo.FindUserOrProjectOrJobname(user, strings.Trim(search, " ")) | ||||||
| 			if err != nil { |  | ||||||
|  | 			if jobid != "" { | ||||||
|  | 				http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(jobid), http.StatusFound) // JobId (Match) | ||||||
|  | 			} else if username != "" { | ||||||
|  | 				http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page of first match | ||||||
|  | 			} else if project != "" { | ||||||
|  | 				http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(project), http.StatusFound) // projectId (equal) | ||||||
|  | 			} else if jobname != "" { | ||||||
|  | 				http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(jobname), http.StatusFound) // JobName (contains) | ||||||
|  | 			} else { | ||||||
| 				web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Info", MsgType: "alert-info", Message: "Search without result", User: *user, Roles: availableRoles, Build: buildInfo}) | 				web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Info", MsgType: "alert-info", Message: "Search without result", User: *user, Roles: availableRoles, Build: buildInfo}) | ||||||
| 				return |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if username != "" { |  | ||||||
| 				http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusFound) // User: Redirect to user page |  | ||||||
| 			} else if project != "" { |  | ||||||
| 				http.Redirect(rw, r, "/monitoring/jobs/?projectMatch=eq&project="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // projectId (equal) |  | ||||||
| 			} else if jobname != "" { |  | ||||||
| 				http.Redirect(rw, r, "/monitoring/jobs/?jobName="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // JobName (contains) |  | ||||||
| 			} else { |  | ||||||
| 				http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(strings.Trim(search, " ")), http.StatusFound) // No Result: Probably jobId |  | ||||||
| 			} |  | ||||||
| 		} else { | 		} else { | ||||||
| 			web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Searchbar query parameters malformed", User: *user, Roles: availableRoles, Build: buildInfo}) | 			web.RenderTemplate(rw, r, "message.tmpl", &web.Page{Title: "Error", MsgType: "alert-danger", Message: "Searchbar query parameters malformed", User: *user, Roles: availableRoles, Build: buildInfo}) | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user