forked from jason/cpolis
Initial version of native session management
This commit is contained in:
@@ -43,11 +43,10 @@ func sortUsersByName(users []*b.User) {
|
||||
})
|
||||
}
|
||||
|
||||
func CreateUser(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
func CreateUser(c *b.Config, s map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := GetSession(w, r, c, s); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
if _, err := ManageSession(w, r, c, s); err != nil {
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -67,12 +66,11 @@ func CreateUser(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func AddUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -134,7 +132,7 @@ func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.Values["role"].(int)
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
@@ -146,16 +144,15 @@ 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 map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := db.GetUser(c, session.Values["id"].(int64))
|
||||
user, err := db.GetUser(c, session.User.ID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -179,17 +176,16 @@ 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 map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
user := &b.User{
|
||||
ID: session.Values["id"].(int64),
|
||||
ID: session.User.ID,
|
||||
UserName: r.PostFormValue("username"),
|
||||
FirstName: r.PostFormValue("first-name"),
|
||||
LastName: r.PostFormValue("last-name"),
|
||||
@@ -244,7 +240,7 @@ func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.Values["role"].(int)
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
@@ -256,7 +252,7 @@ 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 map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
user := &b.User{
|
||||
@@ -303,12 +299,6 @@ func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
if err := saveSession(w, r, s, user); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := db.AddIssue(); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
@@ -327,12 +317,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 map[string]*Session, action string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -348,7 +337,7 @@ func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.H
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
delete(data.Users, session.Values["id"].(int64))
|
||||
delete(data.Users, session.User.ID)
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
||||
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
@@ -359,11 +348,10 @@ func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.H
|
||||
}
|
||||
}
|
||||
|
||||
func EditUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func EditUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := GetSession(w, r, c, s); err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
if _, err := ManageSession(w, r, c, s); err != nil {
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -398,12 +386,11 @@ func EditUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
func UpdateUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -473,7 +460,7 @@ func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.Values["role"].(int)
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html"))
|
||||
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
||||
@@ -484,12 +471,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 map[string]*Session) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := GetSession(w, r, c, s)
|
||||
session, err := ManageSession(w, r, c, s)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -507,7 +493,7 @@ func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
||||
}
|
||||
|
||||
data := new(struct{ Role int })
|
||||
data.Role = session.Values["role"].(int)
|
||||
data.Role = session.User.Role
|
||||
|
||||
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
||||
tmpl = template.Must(tmpl, err)
|
||||
|
Reference in New Issue
Block a user