Completed template system, added login/logout
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.aiterp.net/gisle/wrouter/response"
|
||||
|
||||
"git.aiterp.net/AiteRP/aitestory/model"
|
||||
"git.aiterp.net/AiteRP/aitestory/view"
|
||||
"git.aiterp.net/AiteRP/aitestory/viewmodel"
|
||||
|
||||
@@ -15,18 +19,71 @@ import (
|
||||
var ListController = wrouter.Router{}
|
||||
|
||||
func listIndex(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
||||
if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
|
||||
var err error
|
||||
if req.Method != "GET" || len(req.URL.Path) > len(path) {
|
||||
return false
|
||||
}
|
||||
|
||||
vm := viewmodel.PageList{}
|
||||
vm.Headers, err = model.ListHeaders()
|
||||
vm.Categories = model.PageCategories
|
||||
vm.Setup(user)
|
||||
|
||||
if err != nil {
|
||||
response.Text(w, 500, err.Error())
|
||||
return true
|
||||
}
|
||||
|
||||
view.Render(w, "index", 200, vm)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func listFiltered(category model.PageCategory) wrouter.FunctionHandlerFunc {
|
||||
return func(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
||||
var err error
|
||||
|
||||
if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
|
||||
return false
|
||||
}
|
||||
|
||||
tagName := strings.Replace(req.URL.Path[len(path):], "_", " ", -1)
|
||||
|
||||
vm := viewmodel.PageList{}
|
||||
|
||||
if tagName != "" {
|
||||
tag, err := model.FindTag("name", tagName)
|
||||
if err != nil {
|
||||
response.Text(w, 404, err.Error())
|
||||
return true
|
||||
}
|
||||
|
||||
vm.Headers, err = model.ListHeadersByTag(category.Key, tag)
|
||||
} else {
|
||||
vm.Headers, err = model.ListHeadersByCategory(category.Key)
|
||||
}
|
||||
|
||||
vm.Categories = model.PageCategories
|
||||
vm.ActiveCategory = category
|
||||
vm.Setup(user)
|
||||
|
||||
if err != nil {
|
||||
response.Text(w, 500, err.Error())
|
||||
return true
|
||||
}
|
||||
|
||||
view.Render(w, "index", 200, vm)
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// story.aiterp.net/Ruins_of_Rakhana
|
||||
func init() {
|
||||
ListController.Function("/", listIndex)
|
||||
|
||||
ListController.Function("/", listFiltered(model.PageCategory{Key: ""}))
|
||||
for _, category := range model.PageCategories {
|
||||
ListController.Function(fmt.Sprintf("/%s/", strings.ToLower(category.Plural)), listFiltered(category))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.aiterp.net/AiteRP/aitestory/view"
|
||||
"git.aiterp.net/AiteRP/aitestory/viewmodel"
|
||||
"git.aiterp.net/gisle/wrouter"
|
||||
"git.aiterp.net/gisle/wrouter/auth"
|
||||
)
|
||||
|
||||
// UserController serves and handles the login form
|
||||
var UserController = wrouter.Router{}
|
||||
|
||||
func userLogin(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
||||
//var err error
|
||||
if (req.Method != "GET" && req.Method != "POST") || len(req.URL.Path) > len(path) {
|
||||
return false
|
||||
}
|
||||
|
||||
if req.Method == "GET" && user != nil {
|
||||
http.Redirect(w, req, "/", 302)
|
||||
return true
|
||||
}
|
||||
|
||||
ul := viewmodel.UserLogin{}
|
||||
|
||||
if req.Method == "POST" {
|
||||
req.ParseForm()
|
||||
|
||||
wa := WikiAthenticator{}
|
||||
newUser, err := wa.Login(req.Form.Get("username"), req.Form.Get("password"))
|
||||
if err == nil {
|
||||
sess := auth.OpenSession(newUser)
|
||||
|
||||
http.SetCookie(w, &http.Cookie{Name: auth.SessionCookieName, Value: sess.ID, Expires: sess.Time.Add(auth.SessionMaxTime), Path: "/", HttpOnly: true})
|
||||
http.Redirect(w, req, "/", 302)
|
||||
return true
|
||||
}
|
||||
ul.UserName = req.Form.Get("username")
|
||||
ul.Error = err.Error()
|
||||
}
|
||||
|
||||
ul.Setup(user)
|
||||
view.Render(w, "login", 200, ul)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func userLogout(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
||||
//var err error
|
||||
|
||||
if user != nil {
|
||||
auth.CloseSession(user.Session.ID)
|
||||
}
|
||||
|
||||
http.Redirect(w, req, "/", 302)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserController.Function("/login", userLogin)
|
||||
UserController.Function("/logout", userLogout)
|
||||
}
|
||||
@@ -15,14 +15,17 @@ import (
|
||||
// to log in
|
||||
type WikiAthenticator struct{}
|
||||
|
||||
// ID is for a potential multi-login future
|
||||
func (wikiAuth *WikiAthenticator) ID() string {
|
||||
return "wiki"
|
||||
}
|
||||
|
||||
// Name is for a potential multi-login future
|
||||
func (wikiAuth *WikiAthenticator) Name() string {
|
||||
return "Wiki"
|
||||
}
|
||||
|
||||
// Find finds a user that has logged in at least once
|
||||
func (wikiAuth *WikiAthenticator) Find(username string) *auth.User {
|
||||
db := server.Main.DB
|
||||
|
||||
@@ -38,7 +41,7 @@ func (wikiAuth *WikiAthenticator) Find(username string) *auth.User {
|
||||
return nil
|
||||
}
|
||||
|
||||
user := auth.NewUser(wikiAuth, "", "member", nil)
|
||||
user := auth.NewUser(wikiAuth, "", "member", make(map[string]string, 4))
|
||||
role := "member"
|
||||
rows.Scan(&user.ID, &role)
|
||||
user.Data["role"] = role
|
||||
@@ -63,20 +66,19 @@ func (wikiAuth *WikiAthenticator) Login(username, password string) (*auth.User,
|
||||
}
|
||||
|
||||
// Look up the user
|
||||
rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", client.BasicAuthUser)
|
||||
rows, err := db.Query("SELECT id,role FROM `user` WHERE id=?", username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Login failed %v", err)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// If none was found, just create a new record with the role of member
|
||||
if !rows.Next() {
|
||||
_, err = db.Exec("INSERT INTO `user` (id, role) VALUES (?, 'member')", client.BasicAuthUser)
|
||||
_, err = db.Exec("INSERT INTO `user` (id, role) VALUES (?, 'member')", username)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Login failed %v", err)
|
||||
}
|
||||
|
||||
return auth.NewUser(wikiAuth, client.BasicAuthUser, "member", nil), nil
|
||||
return auth.NewUser(wikiAuth, username, "member", nil), nil
|
||||
}
|
||||
|
||||
// If the user was found, read it in
|
||||
@@ -91,6 +93,7 @@ func (wikiAuth *WikiAthenticator) Login(username, password string) (*auth.User,
|
||||
return auth.NewUser(wikiAuth, userid, "member", nil), nil
|
||||
}
|
||||
|
||||
// Register just tells the user that they can't.
|
||||
func (wikiAuth *WikiAthenticator) Register(username, password string, data map[string]string) (*auth.User, error) {
|
||||
return nil, errors.New("Registration not allowed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user