func PostSavedQueries(w http.ResponseWriter, r *http.Request) { db := context.Get(r, "db").(*sqlx.DB) cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) session, _ := cookieStore.Get(r, "resourcedmaster-session") currentUser := session.Values["user"].(*rm_dal.UserRow) accessTokenRow, err := rm_dal.NewAccessToken(db).GetByUserID(nil, currentUser.ID) if err != nil { libhttp.HandleErrorJson(w, err) return } savedQuery := r.FormValue("SavedQuery") _, err = rm_dal.NewSavedQuery(db).CreateOrUpdate(nil, accessTokenRow, savedQuery) if err != nil { libhttp.HandleErrorJson(w, err) return } http.Redirect(w, r, "/?q="+savedQuery, 301) }
func DeleteSavedQueriesID(w http.ResponseWriter, r *http.Request) { savedQueryID, err := getIdFromPath(w, r) if err != nil { libhttp.HandleErrorJson(w, err) return } db := context.Get(r, "db").(*sqlx.DB) cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) session, _ := cookieStore.Get(r, "resourcedmaster-session") currentUser := session.Values["user"].(*rm_dal.UserRow) sq := rm_dal.NewSavedQuery(db) savedQueryRow, err := sq.GetByID(nil, savedQueryID) if currentUser.ID != savedQueryRow.UserID { err := errors.New("Modifying other user's saved query is not allowed.") libhttp.HandleErrorJson(w, err) return } err = sq.DeleteByID(nil, savedQueryID) if err != nil { libhttp.HandleErrorJson(w, err) return } http.Redirect(w, r, "/", 301) }
func GetHosts(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") cookieStore := context.Get(r, "cookieStore").(*sessions.CookieStore) session, _ := cookieStore.Get(r, "resourcedmaster-session") currentUserRow, ok := session.Values["user"].(*dal.UserRow) if !ok { http.Redirect(w, r, "/logout", 301) return } currentClusterInterface := session.Values["currentCluster"] if currentClusterInterface == nil { http.Redirect(w, r, "/", 301) return } currentCluster := currentClusterInterface.(*dal.ClusterRow) db := context.Get(r, "db").(*sqlx.DB) query := r.URL.Query().Get("q") hosts, err := dal.NewHost(db).AllByClusterIDAndQuery(nil, currentCluster.ID, query) if err != nil { libhttp.HandleErrorJson(w, err) return } savedQueries, err := dal.NewSavedQuery(db).AllByClusterID(nil, currentCluster.ID) if err != nil { libhttp.HandleErrorJson(w, err) return } accessTokenRow, err := dal.NewAccessToken(db).GetByUserID(nil, currentUserRow.ID) if err != nil { libhttp.HandleErrorJson(w, err) return } data := struct { Addr string CurrentUser *dal.UserRow AccessToken *dal.AccessTokenRow Clusters []*dal.ClusterRow CurrentClusterJson string Hosts []*dal.HostRow SavedQueries []*dal.SavedQueryRow }{ context.Get(r, "addr").(string), currentUserRow, accessTokenRow, context.Get(r, "clusters").([]*dal.ClusterRow), string(context.Get(r, "currentClusterJson").([]byte)), hosts, savedQueries, } tmpl, err := template.ParseFiles("templates/dashboard.html.tmpl", "templates/hosts/list.html.tmpl") if err != nil { libhttp.HandleErrorJson(w, err) return } tmpl.Execute(w, data) }