This commit is contained in:
Lunny Xiao 2014-03-03 22:44:51 +08:00
commit b73cf0ee77
3 changed files with 12 additions and 4 deletions

2
.gitignore vendored
View File

@ -3,4 +3,4 @@ gogs
*.exe~ *.exe~
.DS_Store .DS_Store
*.db *.db
*.log *.log

View File

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/gogits/validation" "github.com/gogits/validation"
@ -23,7 +24,7 @@ func Profile(r render.Render) {
return return
} }
func SignIn(req *http.Request, r render.Render) { func SignIn(req *http.Request, r render.Render, session sessions.Session) {
if req.Method == "GET" { if req.Method == "GET" {
r.HTML(200, "user/signin", map[string]interface{}{ r.HTML(200, "user/signin", map[string]interface{}{
"Title": "Log In", "Title": "Log In",
@ -31,14 +32,16 @@ func SignIn(req *http.Request, r render.Render) {
return return
} }
// todo sign in // TODO: LDAP sign in
_, err := models.LoginUserPlain(req.FormValue("account"), req.FormValue("passwd")) user, err := models.LoginUserPlain(req.FormValue("account"), req.FormValue("passwd"))
if err != nil { if err != nil {
r.HTML(200, "base/error", map[string]interface{}{ r.HTML(200, "base/error", map[string]interface{}{
"Error": fmt.Sprintf("%v", err), "Error": fmt.Sprintf("%v", err),
}) })
return return
} }
session.Set("userId", user.Id)
session.Set("userName", user.Name)
r.Redirect("/") r.Redirect("/")
} }

5
web.go
View File

@ -12,6 +12,7 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/codegangsta/martini" "github.com/codegangsta/martini"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
"github.com/martini-contrib/sessions"
"github.com/gogits/gogs/routers" "github.com/gogits/gogs/routers"
"github.com/gogits/gogs/routers/repo" "github.com/gogits/gogs/routers/repo"
@ -46,6 +47,10 @@ func runWeb(*cli.Context) {
// Middleware. // Middleware.
m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}})) m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
// TODO: should use other store because cookie store is not secure.
store := sessions.NewCookieStore([]byte("secret123"))
m.Use(sessions.Sessions("my_session", store))
// Routers. // Routers.
m.Get("/", routers.Dashboard) m.Get("/", routers.Dashboard)
m.Any("/login", user.SignIn) m.Any("/login", user.SignIn)