func (u User) sendConfirmEmail(request prago.Request, a *Admin) error { if u.emailConfirmed() { return errors.New("email already confirmed") } if a.noReplyEmail == "" { return errors.New("no reply email empty") } locale := GetLocale(request) urlValues := make(url.Values) urlValues.Add("email", u.Email) urlValues.Add("token", u.emailToken(a.App)) subject := messages.Messages.Get(locale, "admin_confirm_email_subject", a.AppName) link := request.App().Config.GetString("baseUrl") + a.Prefix + "/user/confirm_email?" + urlValues.Encode() body := messages.Messages.Get(locale, "admin_confirm_email_body", link, link, a.AppName) message := sendgrid.NewMail() message.SetFrom(a.noReplyEmail) message.AddTo(u.Email) message.AddToName(u.Name) message.SetSubject(subject) message.SetHTML(body) return a.sendgridClient.Send(message) }
func localeFromRequest(request prago.Request) string { acceptHeader := request.Request().Header.Get("Accept-Language") t, _, _ := language.ParseAcceptLanguage(acceptHeader) tag, _, _ := languageMatcher.Match(t...) return tag.String() }
//GetLocale from request func GetLocale(request prago.Request) string { user, hasUser := request.GetData("currentuser").(*User) if hasUser { if validLocale(user.Locale) { return user.Locale } } return localeFromRequest(request) }
//ValidateCSRF validates csrf token for request func ValidateCSRF(request prago.Request) { token := CSRFToken(request) if len(token) == 0 { panic("token not set") } paramsToken := request.Params().Get("_csrfToken") if paramsToken != token { panic("Wrong CSRF token") } }
func stats(request prago.Request) { if !AuthenticateSysadmin(GetUser(request)) { render403(request) return } stats := [][2]string{} stats = append(stats, [2]string{"App name", request.App().Data()["appName"].(string)}) stats = append(stats, [2]string{"App version", request.App().Data()["version"].(string)}) port := request.App().Data()["port"].(int) stats = append(stats, [2]string{"Port", fmt.Sprintf("%d", port)}) developmentModeStr := "false" if request.App().DevelopmentMode { developmentModeStr = "true" } stats = append(stats, [2]string{"Development mode", developmentModeStr}) stats = append(stats, [2]string{"Started at", request.App().StartedAt.Format(time.RFC3339)}) stats = append(stats, [2]string{"Go version", runtime.Version()}) stats = append(stats, [2]string{"Compiler", runtime.Compiler}) stats = append(stats, [2]string{"GOARCH", runtime.GOARCH}) stats = append(stats, [2]string{"GOOS", runtime.GOOS}) stats = append(stats, [2]string{"GOMAXPROCS", fmt.Sprintf("%d", runtime.GOMAXPROCS(-1))}) configStats := request.App().Config.Export() osStats := [][2]string{} osStats = append(osStats, [2]string{"EGID", fmt.Sprintf("%d", os.Getegid())}) osStats = append(osStats, [2]string{"EUID", fmt.Sprintf("%d", os.Geteuid())}) osStats = append(osStats, [2]string{"GID", fmt.Sprintf("%d", os.Getgid())}) osStats = append(osStats, [2]string{"Page size", fmt.Sprintf("%d", os.Getpagesize())}) osStats = append(osStats, [2]string{"PID", fmt.Sprintf("%d", os.Getpid())}) osStats = append(osStats, [2]string{"PPID", fmt.Sprintf("%d", os.Getppid())}) wd, _ := os.Getwd() osStats = append(osStats, [2]string{"Working directory", wd}) hostname, _ := os.Hostname() osStats = append(osStats, [2]string{"Hostname", hostname}) var mStats runtime.MemStats runtime.ReadMemStats(&mStats) memStats := [][2]string{} memStats = append(memStats, [2]string{"Alloc", fmt.Sprintf("%d", mStats.Alloc)}) memStats = append(memStats, [2]string{"TotalAlloc", fmt.Sprintf("%d", mStats.TotalAlloc)}) memStats = append(memStats, [2]string{"Sys", fmt.Sprintf("%d", mStats.Sys)}) memStats = append(memStats, [2]string{"Lookups", fmt.Sprintf("%d", mStats.Lookups)}) memStats = append(memStats, [2]string{"Mallocs", fmt.Sprintf("%d", mStats.Mallocs)}) memStats = append(memStats, [2]string{"Frees", fmt.Sprintf("%d", mStats.Frees)}) memStats = append(memStats, [2]string{"HeapAlloc", fmt.Sprintf("%d", mStats.HeapAlloc)}) memStats = append(memStats, [2]string{"HeapSys", fmt.Sprintf("%d", mStats.HeapSys)}) memStats = append(memStats, [2]string{"HeapIdle", fmt.Sprintf("%d", mStats.HeapIdle)}) memStats = append(memStats, [2]string{"HeapInuse", fmt.Sprintf("%d", mStats.HeapInuse)}) memStats = append(memStats, [2]string{"HeapReleased", fmt.Sprintf("%d", mStats.HeapReleased)}) memStats = append(memStats, [2]string{"HeapObjects", fmt.Sprintf("%d", mStats.HeapObjects)}) memStats = append(memStats, [2]string{"StackInuse", fmt.Sprintf("%d", mStats.StackInuse)}) memStats = append(memStats, [2]string{"StackSys", fmt.Sprintf("%d", mStats.StackSys)}) memStats = append(memStats, [2]string{"MSpanInuse", fmt.Sprintf("%d", mStats.MSpanInuse)}) memStats = append(memStats, [2]string{"MSpanSys", fmt.Sprintf("%d", mStats.MSpanSys)}) memStats = append(memStats, [2]string{"MCacheInuse", fmt.Sprintf("%d", mStats.MCacheInuse)}) memStats = append(memStats, [2]string{"MCacheSys", fmt.Sprintf("%d", mStats.MCacheSys)}) memStats = append(memStats, [2]string{"BuckHashSys", fmt.Sprintf("%d", mStats.BuckHashSys)}) memStats = append(memStats, [2]string{"GCSys", fmt.Sprintf("%d", mStats.GCSys)}) memStats = append(memStats, [2]string{"OtherSys", fmt.Sprintf("%d", mStats.OtherSys)}) memStats = append(memStats, [2]string{"NextGC", fmt.Sprintf("%d", mStats.NextGC)}) memStats = append(memStats, [2]string{"LastGC", fmt.Sprintf("%d", mStats.LastGC)}) memStats = append(memStats, [2]string{"PauseTotalNs", fmt.Sprintf("%d", mStats.PauseTotalNs)}) memStats = append(memStats, [2]string{"NumGC", fmt.Sprintf("%d", mStats.NumGC)}) environmentStats := [][2]string{} for _, e := range os.Environ() { pair := strings.Split(e, "=") environmentStats = append(environmentStats, [2]string{pair[0], pair[1]}) } request.SetData("stats", stats) request.SetData("configStats", configStats) request.SetData("osStats", osStats) request.SetData("memStats", memStats) request.SetData("environmentStats", environmentStats) request.SetData("admin_yield", "admin_stats") prago.Render(request, 200, "admin_layout") }
func (s *Sessions) around(request prago.Request, next func()) { if request.IsProcessed() { next() return } sessionName := s.app.Data()["appName"].(string) session, err := s.cookieStore.Get(request.Request(), sessionName) if err != nil { request.Log().Errorln("Session not valid") request.Response().Header().Set("Set-Cookie", sessionName+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;") panic(err) } flashes := session.Flashes() if len(flashes) > 0 { request.SetData("flash_messages", flashes) prago.Must(session.Save(request.Request(), request.Response())) } request.SetData("session", session) next() }
//CSRFToken returns csrf token from request func CSRFToken(request prago.Request) string { return request.GetData("_csrfToken").(string) }
func (u User) getRenewURL(request prago.Request, a *Admin) string { urlValues := make(url.Values) urlValues.Add("email", u.Email) urlValues.Add("token", u.emailToken(a.App)) return request.App().Config.GetString("baseUrl") + a.Prefix + "/user/renew_password?" + urlValues.Encode() }
func DevelopmentRecovery(p *prago.Request, recoveryData interface{}) { if p.App().DevelopmentMode { temp, err := template.New("development_error").Parse(developmentErrorTmpl) if err != nil { panic(err) } byteData := fmt.Sprintf("%s", recoveryData) buf := new(bytes.Buffer) err = temp.ExecuteTemplate(buf, "development_error", map[string]interface{}{ "name": byteData, "subname": "500 Internal Server Error", "stack": string(debug.Stack()), }) if err != nil { panic(err) } p.Response().Header().Add("Content-type", "text/html") p.Response().WriteHeader(500) p.Response().Write(buf.Bytes()) } else { p.Response().WriteHeader(500) p.Response().Write([]byte("We are sorry, some error occured. (500)")) } p.Log().Errorln(fmt.Sprintf("500 - error\n%s\nstack:\n", recoveryData)) p.Log().Errorln(string(debug.Stack())) }
func render404(request prago.Request) { request.SetData("message", messages.Messages.Get(GetLocale(request), "admin_404")) request.SetData("admin_yield", "admin_message") prago.Render(request, 404, "admin_layout") }
//GetUser returns currently logged in user func GetUser(request prago.Request) *User { return request.GetData("currentuser").(*User) }
//AddFlashMessage adds flash message to request func AddFlashMessage(request prago.Request, message string) { session := request.GetData("session").(*sessions.Session) session.AddFlash(message) prago.Must(session.Save(request.Request(), request.Response())) }
//Sitemap renders sites sitemap xml file func Sitemap(request prago.Request, urls []string) { request.Response().Header().Set("Content-Type", "text/xml") request.Response().WriteHeader(200) prev := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">` request.Response().Write([]byte(prev)) for _, v := range urls { url := fmt.Sprintf("<url><loc>%s</loc></url>", v) request.Response().Write([]byte(url)) } after := `</urlset>` request.Response().Write([]byte(after)) request.SetProcessed() }
func Newsletter(request prago.Request) { email := request.Params().Get("email") println(email) prago.Redirect(request, "/index.html?submitted") }