func GetAccount(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) bnd := binding.GetDefault(r) id, _ := strconv.Atoi(c.URLParams["uid"]) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) u, err := user.Get(db, id) if err != nil { panic(err) } if r.FormValue("edit") == getresponse.ComparableValue { bnd["Success"] = strEditSuccess } bnd["User"] = u bnd["ID"] = claims["id"] bnd["ADMIN"] = claims["guuid"] template.HTML(w, http.StatusOK, "panel/show", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func InjectAuthenticate(c *web.C, h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { cookie, err := r.Cookie("injuv_auth") if err != nil { http.Redirect(w, r, formatURLlogin, http.StatusFound) return } claims, ok := security.Decode(cookie.Value) if !ok { http.Redirect(w, r, formatURLlogin, http.StatusFound) return } for key, value := range claims { c.Env[key] = value } h.ServeHTTP(w, r) return } return http.HandlerFunc(fn) }
func GetNewAccount(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) bnd := binding.GetDefault(r) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) bnd["ID"] = claims["id"] bnd["ADMIN"] = claims["guuid"] template.HTML(w, http.StatusOK, "panel/create", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func GetAccounts(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) bnd := binding.Binding{} numberPage, _ := strconv.Atoi(c.URLParams["page"]) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) var ( init int page []int Status string ) if numberPage == 0 || numberPage == 1 { init = 1 } else { init = numberPage } users, _ := user.Range(db, init, config.NumberResultPage) total := user.CountAll(db) totalPage := (total / config.NumberResultPage) for i := 1; i <= totalPage; i++ { page = append(page, i) } if r.FormValue("delete") == getresponse.ComparableValue { Status = strDeleteSuccess } bnd = binding.Binding{ "PageTitle": "Back office INJUV", "CurrentURL": r.URL.Path, "ID": claims["id"], "ADMIN": claims["guuid"], "Page": page, "Users": users, "TotalPage": totalPage, "Success": Status, } template.HTML(w, http.StatusOK, "panel/accounts", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) bnd := binding.GetDefault(r) cookie, _ := r.Cookie("injuv_auth") if cookie != nil { claims, _ := security.Decode(cookie.Value) id := int(claims["id"].(float64)) http.Redirect(w, r, fmt.Sprintf("/panel/%d.html", id), http.StatusFound) return } template.HTML(w, http.StatusOK, "home/login", bnd) }
func GetActivities(c web.C, w http.ResponseWriter, r *http.Request) { var ( pendientes []*activity.Activity impedidas []*activity.Activity proceso []*activity.Activity terminados []*activity.Activity ) template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) id, _ := strconv.Atoi(c.URLParams["id"]) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) users, _ := user.GetAll(db) bnd := binding.GetDefault(r) fmt.Println("\n\n\n\n\n\n %d", id) if claims["guuid"].(bool) { impedidas, _ = activity.GetImpedidas(db, 0) pendientes, _ = activity.GetPendintes(db, 0) proceso, _ = activity.GetEnProceso(db, 0) terminados, _ = activity.GetTerminados(db, 0) fmt.Println("\n\n\n\n\n\n ", impedidas) } else { impedidas, _ = activity.GetImpedidas(db, id) pendientes, _ = activity.GetPendintes(db, id) proceso, _ = activity.GetEnProceso(db, id) terminados, _ = activity.GetTerminados(db, id) } bnd["Users"] = users bnd["ID"] = claims["id"] bnd["ADMIN"] = claims["guuid"] bnd["Impedidas"] = impedidas bnd["Pendientes"] = pendientes bnd["Proceso"] = proceso bnd["Terminados"] = terminados template.HTML(w, http.StatusOK, "panel/activities", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func GetProfile(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) bnd := binding.Binding{} cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) bnd = binding.Binding{ "PageTitle": "Back office INJUV", "CurrentURL": r.URL.Path, "Name": claims["name"], } template.HTML(w, http.StatusOK, "panel/profile", bnd, render.HTMLOptions{ Layout: "panel/layout", }) }
func GetDeleteProfile(c web.C, w http.ResponseWriter, r *http.Request) { template := c.Env["render"].(*render.Render) db := c.Env["mysql"].(*sqlx.DB) bnd := binding.GetDefault(r) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) ids := claims["id"].(float64) id, _ := strconv.Atoi(c.URLParams["uid"]) err := user.Delete(db, id) bnd["ID"] = ids bnd["ADMIN"] = claims["guuid"] if err == user.ErrUserNotExist { bnd["Error"] = user.ErrUserNotExist template.HTML(w, http.StatusOK, "/panel/profile", bnd) } http.Redirect(w, r, fmt.Sprintf(URLPanelDelete, int(ids), getresponse.ComparableValue), http.StatusFound) return }
func PostEditAccount(c web.C, w http.ResponseWriter, r *http.Request) { bnd := binding.GetDefault(r) db := c.Env["mysql"].(*sqlx.DB) template := c.Env["render"].(*render.Render) id, _ := strconv.Atoi(c.URLParams["uid"]) cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) firstName := utils.GetAndTrim(r, "firstname") lastName := utils.GetAndTrim(r, "lastname") userName := utils.GetAndTrim(r, "userName") email := utils.GetAndTrim(r, "email") password := utils.GetAndTrim(r, "password") rePassword := utils.GetAndTrim(r, "repassword") userAdmin, _ := strconv.ParseBool(utils.GetAndTrim(r, "admin")) fmt.Println("%t\n\n", userAdmin) if firstName == "" { bnd["Error"] = strFirstNameEmpty } if lastName == "" { bnd["Error"] = strLastNameEmpty } if userName == "" { bnd["Error"] = strUserNameEmpty } if password == "" { bnd["Error"] = strPasswordEmpty } if rePassword == "" { bnd["Error"] = strPasswordEmpty } if password != rePassword { bnd["Error"] = strErrorPassword } if bnd["Error"] != nil { template.HTML(w, http.StatusOK, "panel/edit", bnd) return } newUser := &user.User{ ID: id, FirstName: firstName, LastName: lastName, UserName: userName, Email: email, Password: password, Admin: userAdmin, SignupDate: time.Now(), } ids := claims["id"].(float64) bnd["ADMIN"] = claims["guuid"] bnd["User"] = newUser newUser.Save(db) if claims["guuid"].(bool) == true { http.Redirect(w, r, fmt.Sprintf(URLPanelUserByID, int(ids), id, getresponse.ComparableValue), http.StatusFound) return } http.Redirect(w, r, fmt.Sprintf(URLProfileUserByID, int(ids), id, getresponse.ComparableValue), http.StatusFound) return }
func main() { goji.Use(func(c *web.C, h http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") h.ServeHTTP(w, r) } return http.HandlerFunc(fn) }) db := helperdb.GetDatabase() defer db.Close() goji.Use(renderer.InjectRender) goji.Use(static.Static("assets")) goji.Get("/", http.RedirectHandler("/login.html", http.StatusFound)) goji.Get("/logout", handlers.Logout) goji.Get("/login.html", handlers.GetLogin) goji.Post("/login.html", cji.Use(database.InjectDatabase(db)).On(handlers.PostLogin)) //perfil goji.Get("/panel/:id/perfil/:uid/mostrar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetAccount)) goji.Get("/panel/:id/perfil/:uid/editar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetEditAccount)) goji.Post("/panel/:id/perfil/:uid/editar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.PostEditAccount)) goji.Get("/panel/:id.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.ShowPanel)) //Cuentas goji.Get("/panel/:id/cuentas.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetAccounts)) goji.Get("/panel/:id/cuentas/pagina/:page.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetAccounts)) goji.Get("/panel/:id/crear.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetNewAccount)) goji.Post("/panel/:id/crear.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.PostNewAccount)) goji.Get("/panel/:id/cuenta/:uid/mostrar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetAccount)) goji.Get("/panel/:id/cuenta/:uid/editar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetEditAccount)) goji.Post("/panel/:id/cuenta/:uid/editar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.PostEditAccount)) goji.Get("/panel/:id/cuenta/:uid/eliminar.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetDeleteProfile)) //Actividades goji.Get("/panel/:id/actividades.html", cji.Use(database.InjectDatabase(db), authenticate.InjectAuthenticate, loaduser.LoadUser).On(handlers.GetActivities)) goji.Post("/panel/nueva/actividad", cji.Use(database.InjectDatabase(db)).On(handlers.NewActivitie)) goji.Post("/panel/actualiza/actividad", cji.Use(database.InjectDatabase(db)).On(handlers.UpdateActivity)) goji.Get("/test", cji.Use(authenticate.InjectAuthenticate).On(func(c web.C, w http.ResponseWriter, r *http.Request) { renderer := c.Env["render"].(*render.Render) /*render.JSON(w, http.StatusOK, map[string]interface{}{ })*/ bnd := binding.Binding{} cookie, _ := r.Cookie("injuv_auth") claims, _ := security.Decode(cookie.Value) bnd = binding.Binding{ "PageTitle": "Back office INJUV", "CurrentURL": r.URL.Path, "Name": claims["name"], } renderer.HTML(w, http.StatusOK, "panel/profile", bnd, render.HTMLOptions{ Layout: "panel/layout", }) })) goji.Serve() }