Gisle Aune
7 years ago
19 changed files with 399 additions and 51 deletions
-
2aitestory.json
-
59controllers/listcontroller.go
-
65controllers/usercontroller.go
-
13controllers/wikiauth.go
-
BINdebug
-
1main.go
-
39model/category.go
-
94model/header.go
-
16model/page.go
-
4model/page_test.go
-
7model/tag.go
-
24model/tag_test.go
-
20view/renderer.go
-
9view/templates/base/default.tmpl
-
26view/templates/index.tmpl
-
21view/templates/login.tmpl
-
22viewmodel/base.go
-
9viewmodel/pagelist.go
-
19viewmodel/userlogin.go
@ -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) |
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
package model |
||||
|
|
||||
|
import ( |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
// PageCategory represents a page category
|
||||
|
type PageCategory struct { |
||||
|
Key string |
||||
|
Plural string |
||||
|
Icon string |
||||
|
} |
||||
|
|
||||
|
// URLRoot is the "folder" used for searching within the category
|
||||
|
func (category *PageCategory) URLRoot() string { |
||||
|
return strings.ToLower(category.Plural) |
||||
|
} |
||||
|
|
||||
|
// PageCategories are used by the view model and page to enforce
|
||||
|
// a limited selection of categories. I may move it to a configuration
|
||||
|
// or the database, but for now I think this list is pretty fixed
|
||||
|
var PageCategories = []PageCategory{ |
||||
|
{"OoC", "OoC", "ooc"}, |
||||
|
{"Info", "Info", "info"}, |
||||
|
{"News", "News", "news"}, |
||||
|
{"Item", "Items", "item"}, |
||||
|
{"Document", "Documents", "document"}, |
||||
|
{"Background", "Background", "background"}, |
||||
|
{"Story", "Stories", "story"}, |
||||
|
} |
||||
|
var pageCategories []string |
||||
|
|
||||
|
// init setups pageCategories
|
||||
|
func init() { |
||||
|
pageCategories := make([]string, len(PageCategories)) |
||||
|
for i, category := range PageCategories { |
||||
|
pageCategories[i] = category.Key |
||||
|
} |
||||
|
} |
@ -1,11 +1,33 @@ |
|||||
{{ define "content" }} |
{{ define "content" }} |
||||
<article> |
<article> |
||||
<h1>Hello, World</h1> |
|
||||
|
|
||||
</article> |
</article> |
||||
{{ end }} |
{{ end }} |
||||
|
|
||||
{{ define "menu" }} |
{{ define "menu" }} |
||||
<h1>Aite RP</h1> |
|
||||
|
<a href="/"><h1>Aite RP</h1></a> |
||||
|
|
||||
|
<ul> |
||||
|
{{ range .Categories }} |
||||
|
<li class="{{ if eq .Key $.ActiveCategory.Key }}selected{{end}}"><a href="/{{.URLRoot}}/{{$.ActiveTag.Hook}}"><div class="mg-icon">Π</div><div class="mg-label">{{.Plural}}</div></a></li> |
||||
|
{{ end }} |
||||
|
</ul> |
||||
|
|
||||
|
{{ if $.User.LoggedIn }} |
||||
|
<ul> |
||||
|
<li><a href="/page/create"><div class="mg-icon">+</div><div class="mg-label">Create</div></a></li> |
||||
|
</ul> |
||||
|
|
||||
|
<ul> |
||||
|
<li><a href="/user/logout"><div class="mg-icon">A</div><div class="mg-label">Logout</div></a></li> |
||||
|
</ul> |
||||
|
{{ else }} |
||||
|
<ul> |
||||
|
<li><a href="/user/login"><div class="mg-icon">A</div><div class="mg-label">Login</div></a></li> |
||||
|
</ul> |
||||
|
{{ end }} |
||||
|
|
||||
|
|
||||
{{ end }} |
{{ end }} |
||||
|
|
||||
{{ define "head" }} |
{{ define "head" }} |
||||
|
@ -0,0 +1,21 @@ |
|||||
|
{{ define "content" }} |
||||
|
<article> |
||||
|
<h1>Login</h1> |
||||
|
<form action="/user/login", method="POST"> |
||||
|
<p class="red">{{$.Error}}</p> |
||||
|
<input placeholder="Username" name="username" type="text" /> |
||||
|
<input placeholder="Password" name="password" type="password" /> |
||||
|
<button type="submit">Submit</button> |
||||
|
</form> |
||||
|
</article> |
||||
|
{{ end }} |
||||
|
|
||||
|
{{ define "menu" }} |
||||
|
<a href="/"><h1>Aite RP</h1></a> |
||||
|
|
||||
|
<li><a href="/"><div class="mg-icon"><</div><div class="mg-label">Back</div></a></li> |
||||
|
{{ end }} |
||||
|
|
||||
|
{{ define "head" }} |
||||
|
|
||||
|
{{ end }} |
@ -0,0 +1,19 @@ |
|||||
|
package viewmodel |
||||
|
|
||||
|
import ( |
||||
|
"git.aiterp.net/gisle/wrouter/auth" |
||||
|
) |
||||
|
|
||||
|
// UserLogin is a view model for rendering the user login form
|
||||
|
type UserLogin struct { |
||||
|
Base |
||||
|
Error string |
||||
|
UserName string |
||||
|
Password string |
||||
|
} |
||||
|
|
||||
|
// Setup sets up the page model and the base, and should
|
||||
|
// be run after the details have been filled in.
|
||||
|
func (ul *UserLogin) Setup(user *auth.User) { |
||||
|
ul.setupBase(user, "Login") |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue