Created package "calls" and verification for frontend and calls
This commit is contained in:
parent
5b41892dff
commit
cd27349d04
@ -1,7 +1,8 @@
|
|||||||
package frontend
|
package calls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -9,31 +10,6 @@ import (
|
|||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
|
|
||||||
idToken := r.Header.Get("Authorization")
|
|
||||||
if idToken == "" {
|
|
||||||
log.Println("Authorization header missing")
|
|
||||||
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := b.NewClient()
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = client.Verify(idToken)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func ServePDFList(c *b.Config) http.HandlerFunc {
|
func ServePDFList(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if tokenIsVerified(w, r) {
|
if tokenIsVerified(w, r) {
|
||||||
@ -62,7 +38,7 @@ func ServePDFList(c *b.Config) http.HandlerFunc {
|
|||||||
func ServePDF(c *b.Config) http.HandlerFunc {
|
func ServePDF(c *b.Config) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if tokenIsVerified(w, r) {
|
if tokenIsVerified(w, r) {
|
||||||
http.ServeFile(w, r, r.PathValue("id"))
|
http.ServeFile(w, r, fmt.Sprint(c.PDFDir, "/", r.PathValue("id")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
cmd/calls/rss.go
Normal file
15
cmd/calls/rss.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package calls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ServeRSS(c *b.Config) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if tokenIsVerified(w, r) {
|
||||||
|
http.ServeFile(w, r, c.RSSFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
cmd/calls/verification.go
Normal file
34
cmd/calls/verification.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package calls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
// tokenIsVerified verifies that a request is authorized. It returns a bool.
|
||||||
|
func tokenIsVerified(w http.ResponseWriter, r *http.Request) bool {
|
||||||
|
idToken := r.Header.Get("Authorization")
|
||||||
|
if idToken == "" {
|
||||||
|
log.Println("Authorization header missing")
|
||||||
|
http.Error(w, "Authorization header missing", http.StatusUnauthorized)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := b.NewClient()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.Verify(idToken)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
@ -25,11 +25,9 @@ const (
|
|||||||
|
|
||||||
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := getSession(w, r, c, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Values["article"] = nil
|
session.Values["article"] = nil
|
||||||
@ -46,6 +44,11 @@ func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type editorHTMLData struct {
|
type editorHTMLData struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
@ -55,13 +58,6 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
Mode int
|
Mode int
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var data editorHTMLData
|
var data editorHTMLData
|
||||||
if session.Values["article"] == nil {
|
if session.Values["article"] == nil {
|
||||||
data = editorHTMLData{}
|
data = editorHTMLData{}
|
||||||
@ -84,11 +80,9 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := getSession(w, r, c, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Values["article"] = nil
|
session.Values["article"] = nil
|
||||||
@ -139,6 +133,11 @@ func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -178,21 +177,18 @@ func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowUnpublishedArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
func ShowUnpublishedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
unpublishedArticles, err := db.GetCertainArticles(false, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -208,19 +204,17 @@ func ShowUnpublishedArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
MyIDs map[int64]bool
|
MyIDs map[int64]bool
|
||||||
RejectedArticles []*b.Article
|
RejectedArticles []*b.Article
|
||||||
}
|
}
|
||||||
data := new(htmlData)
|
data := new(htmlData)
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data.RejectedArticles, err = db.GetCertainArticles(false, true)
|
data.RejectedArticles, err = db.GetCertainArticles(false, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -243,6 +237,10 @@ func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerF
|
|||||||
|
|
||||||
func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
@ -305,6 +303,10 @@ func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Hand
|
|||||||
|
|
||||||
func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Selected map[int64]bool
|
Selected map[int64]bool
|
||||||
Article *b.Article
|
Article *b.Article
|
||||||
@ -352,6 +354,11 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.Handler
|
|||||||
|
|
||||||
func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -359,13 +366,6 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = db.AddArticleToCurrentIssue(id); err != nil {
|
if err = db.AddArticleToCurrentIssue(id); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -402,6 +402,11 @@ func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -409,13 +414,6 @@ func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = db.UpdateAttributes(
|
if err = db.UpdateAttributes(
|
||||||
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@ -430,8 +428,12 @@ func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowCurrentArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
func ShowCurrentArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
articles, err := db.GetCurrentIssueArticles()
|
articles, err := db.GetCurrentIssueArticles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -444,8 +446,12 @@ func ShowCurrentArticles(c *b.Config, db *b.DB) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadImage(c *b.Config) http.HandlerFunc {
|
func UploadImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
file, header, err := r.FormFile("article-image")
|
file, header, err := r.FormFile("article-image")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -7,8 +7,12 @@ import (
|
|||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateTag(c *b.Config) http.HandlerFunc {
|
func CreateTag(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
@ -16,15 +20,13 @@ func CreateTag(c *b.Config) http.HandlerFunc {
|
|||||||
|
|
||||||
func AddTag(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func AddTag(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
db.AddTag(r.PostFormValue("tag"))
|
session, err := getSession(w, r, c, s)
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.AddTag(r.PostFormValue("tag"))
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
|
@ -10,6 +10,10 @@ import (
|
|||||||
|
|
||||||
func ServeImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
func ServeImage(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
absFilepath, err := filepath.Abs(c.PicsDir)
|
absFilepath, err := filepath.Abs(c.PicsDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -10,19 +10,17 @@ import (
|
|||||||
|
|
||||||
func PublishLatestIssue(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func PublishLatestIssue(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := db.PublishLatestIssue(); err != nil {
|
if err := db.PublishLatestIssue(); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
|
||||||
|
@ -90,11 +90,9 @@ func Login(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func Logout(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
func Logout(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := getSession(w, r, c, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Options.MaxAge = -1
|
session.Options.MaxAge = -1
|
||||||
|
@ -30,8 +30,12 @@ func checkUserStrings(user *b.User) (string, int, bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateUser(c *b.Config) http.HandlerFunc {
|
func CreateUser(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
|
||||||
}
|
}
|
||||||
@ -39,6 +43,11 @@ func CreateUser(c *b.Config) http.HandlerFunc {
|
|||||||
|
|
||||||
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
role, err := strconv.Atoi(r.PostFormValue("role"))
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -94,13 +103,6 @@ func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||||
@ -109,11 +111,9 @@ func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := getSession(w, r, c, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := db.GetUser(session.Values["id"].(int64))
|
user, err := db.GetUser(session.Values["id"].(int64))
|
||||||
@ -130,11 +130,9 @@ func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
session, err := s.Get(r, "cookie")
|
session, err := getSession(w, r, c, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
return
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
userData := UserData{
|
userData := UserData{
|
||||||
@ -200,8 +198,11 @@ func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
htmlData := UserData{
|
htmlData := UserData{
|
||||||
User: &b.User{
|
User: &b.User{
|
||||||
UserName: r.PostFormValue("username"),
|
UserName: r.PostFormValue("username"),
|
||||||
@ -275,7 +276,11 @@ func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type htmlData struct {
|
type htmlData struct {
|
||||||
Users map[int64]*b.User
|
Users map[int64]*b.User
|
||||||
Action string
|
Action string
|
||||||
@ -289,21 +294,18 @@ func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.H
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(data.Users, session.Values["id"].(int64))
|
delete(data.Users, session.Values["id"].(int64))
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EditUser(c *b.Config, db *b.DB) http.HandlerFunc {
|
func EditUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if _, err := getSession(w, r, c, s); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -325,6 +327,11 @@ func EditUser(c *b.Config, db *b.DB) http.HandlerFunc {
|
|||||||
|
|
||||||
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -394,13 +401,6 @@ func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData)
|
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData)
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||||
@ -409,6 +409,11 @@ func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
|
|
||||||
func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
session, err := getSession(w, r, c, s)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@ -422,13 +427,6 @@ func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := s.Get(r, "cookie")
|
|
||||||
if err != nil {
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html")
|
|
||||||
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
|
||||||
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||||
tmpl = template.Must(tmpl, err)
|
tmpl = template.Must(tmpl, err)
|
||||||
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
|
||||||
|
22
cmd/frontend/verification.go
Normal file
22
cmd/frontend/verification.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package frontend
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/sessions"
|
||||||
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getSession is used for verifying that the user is logged in and returns their session and an error.
|
||||||
|
func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) {
|
||||||
|
session, err := s.Get(r, "cookie")
|
||||||
|
if err != nil {
|
||||||
|
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
|
||||||
|
tmpl, tmplErr := template.ParseFiles(c.WebDir + "/templates/login.html")
|
||||||
|
template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return session, nil
|
||||||
|
}
|
19
cmd/main.go
19
cmd/main.go
@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
b "streifling.com/jason/cpolis/cmd/backend"
|
b "streifling.com/jason/cpolis/cmd/backend"
|
||||||
|
c "streifling.com/jason/cpolis/cmd/calls"
|
||||||
f "streifling.com/jason/cpolis/cmd/frontend"
|
f "streifling.com/jason/cpolis/cmd/frontend"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -48,15 +49,15 @@ func main() {
|
|||||||
http.FileServer(http.Dir(config.WebDir+"/static/"))))
|
http.FileServer(http.Dir(config.WebDir+"/static/"))))
|
||||||
mux.HandleFunc("/", f.HomePage(config, db, store))
|
mux.HandleFunc("/", f.HomePage(config, db, store))
|
||||||
|
|
||||||
mux.HandleFunc("GET /create-tag", f.CreateTag(config))
|
mux.HandleFunc("GET /create-tag", f.CreateTag(config, store))
|
||||||
mux.HandleFunc("GET /create-user", f.CreateUser(config))
|
mux.HandleFunc("GET /create-user", f.CreateUser(config, store))
|
||||||
mux.HandleFunc("GET /edit-self", f.EditSelf(config, db, store))
|
mux.HandleFunc("GET /edit-self", f.EditSelf(config, db, store))
|
||||||
mux.HandleFunc("GET /edit-user/{id}", f.EditUser(config, db))
|
mux.HandleFunc("GET /edit-user/{id}", f.EditUser(config, db, store))
|
||||||
mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store))
|
mux.HandleFunc("GET /delete-user/{id}", f.DeleteUser(config, db, store))
|
||||||
mux.HandleFunc("GET /hub", f.ShowHub(config, db, store))
|
mux.HandleFunc("GET /hub", f.ShowHub(config, db, store))
|
||||||
mux.HandleFunc("GET /logout", f.Logout(config, store))
|
mux.HandleFunc("GET /logout", f.Logout(config, store))
|
||||||
mux.HandleFunc("GET /pdf/get-list", f.ServePDFList(config))
|
mux.HandleFunc("GET /pdf/get-list", c.ServePDFList(config))
|
||||||
mux.HandleFunc("GET /pdf/{id}", f.ServePDF(config))
|
mux.HandleFunc("GET /pdf/{id}", c.ServePDF(config))
|
||||||
mux.HandleFunc("GET /pics/{pic}", f.ServeImage(config, store))
|
mux.HandleFunc("GET /pics/{pic}", f.ServeImage(config, store))
|
||||||
mux.HandleFunc("GET /publish-article/{id}", f.PublishArticle(config, db, store))
|
mux.HandleFunc("GET /publish-article/{id}", f.PublishArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /publish-issue", f.PublishLatestIssue(config, db, store))
|
mux.HandleFunc("GET /publish-issue", f.PublishLatestIssue(config, db, store))
|
||||||
@ -64,11 +65,11 @@ func main() {
|
|||||||
mux.HandleFunc("GET /rejected-articles", f.ShowRejectedArticles(config, db, store))
|
mux.HandleFunc("GET /rejected-articles", f.ShowRejectedArticles(config, db, store))
|
||||||
mux.HandleFunc("GET /review-rejected-article/{id}", f.ReviewRejectedArticle(config, db, store))
|
mux.HandleFunc("GET /review-rejected-article/{id}", f.ReviewRejectedArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /review-unpublished-article/{id}", f.ReviewUnpublishedArticle(config, db, store))
|
mux.HandleFunc("GET /review-unpublished-article/{id}", f.ReviewUnpublishedArticle(config, db, store))
|
||||||
mux.HandleFunc("GET /rss", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, config.RSSFile) })
|
mux.HandleFunc("GET /rss", c.ServeRSS(config))
|
||||||
mux.HandleFunc("GET /show-all-users-edit", f.ShowAllUsers(config, db, store, "edit-user"))
|
mux.HandleFunc("GET /show-all-users-edit", f.ShowAllUsers(config, db, store, "edit-user"))
|
||||||
mux.HandleFunc("GET /show-all-users-delete", f.ShowAllUsers(config, db, store, "delete-user"))
|
mux.HandleFunc("GET /show-all-users-delete", f.ShowAllUsers(config, db, store, "delete-user"))
|
||||||
mux.HandleFunc("GET /this-issue", f.ShowCurrentArticles(config, db))
|
mux.HandleFunc("GET /this-issue", f.ShowCurrentArticles(config, db, store))
|
||||||
mux.HandleFunc("GET /unpublished-articles", f.ShowUnpublishedArticles(config, db))
|
mux.HandleFunc("GET /unpublished-articles", f.ShowUnpublishedArticles(config, db, store))
|
||||||
mux.HandleFunc("GET /write-article", f.WriteArticle(config, db, store))
|
mux.HandleFunc("GET /write-article", f.WriteArticle(config, db, store))
|
||||||
|
|
||||||
mux.HandleFunc("POST /add-first-user", f.AddFirstUser(config, db, store))
|
mux.HandleFunc("POST /add-first-user", f.AddFirstUser(config, db, store))
|
||||||
@ -79,7 +80,7 @@ func main() {
|
|||||||
mux.HandleFunc("POST /submit-article", f.SubmitArticle(config, db, store))
|
mux.HandleFunc("POST /submit-article", f.SubmitArticle(config, db, store))
|
||||||
mux.HandleFunc("POST /update-self", f.UpdateSelf(config, db, store))
|
mux.HandleFunc("POST /update-self", f.UpdateSelf(config, db, store))
|
||||||
mux.HandleFunc("POST /update-user/{id}", f.UpdateUser(config, db, store))
|
mux.HandleFunc("POST /update-user/{id}", f.UpdateUser(config, db, store))
|
||||||
mux.HandleFunc("POST /upload-image", f.UploadImage(config))
|
mux.HandleFunc("POST /upload-image", f.UploadImage(config, store))
|
||||||
|
|
||||||
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
log.Fatalln(http.ListenAndServe(config.Port, mux))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user