From a2a59f8ad1192d3504abd50b2daf2ebfd97c86ca Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 7 Mar 2014 17:08:21 -0500 Subject: [PATCH] Change /user/profile URL to /user/:username --- gogs.go | 2 +- models/repo.go | 4 +-- models/user.go | 17 ++++++++++++ modules/auth/user.go | 2 +- routers/repo/repo.go | 54 ++++++++++++++++---------------------- routers/user/ssh.go | 39 +++++++++++++-------------- routers/user/user.go | 34 ++++++++++++++---------- templates/base/error.tmpl | 3 ++- templates/base/navbar.tmpl | 4 +-- templates/repo/create.tmpl | 6 ++--- web.go | 14 ++++++---- 11 files changed, 96 insertions(+), 83 deletions(-) diff --git a/gogs.go b/gogs.go index e239737e2..7bbcd803e 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.1.0307" +const APP_VER = "0.0.2.0307" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index 6fc590566..04e8c75c1 100644 --- a/models/repo.go +++ b/models/repo.go @@ -48,9 +48,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) { return s.IsDir(), nil } -// -// create a repository for a user or orgnaziation -// +// CreateRepository creates a repository for given user or orgnaziation. func CreateRepository(user *User, reposName string) (*Repo, error) { f := RepoPath(user.Name, reposName) _, err := git.InitRepository(f, true) diff --git a/models/user.go b/models/user.go index a1ec293a7..36cdfc8a4 100644 --- a/models/user.go +++ b/models/user.go @@ -190,6 +190,23 @@ func GetUserById(id int64) (*User, error) { return user, nil } +func GetUserByName(name string) (*User, error) { + if len(name) == 0 { + return nil, ErrUserNotExist + } + user := &User{ + LowerName: strings.ToLower(name), + } + has, err := orm.Get(user) + if err != nil { + return nil, err + } + if !has { + return nil, ErrUserNotExist + } + return user, nil +} + // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { user := User{LowerName: strings.ToLower(name), Passwd: passwd} diff --git a/modules/auth/user.go b/modules/auth/user.go index 3a793f453..e25593b73 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -69,7 +69,7 @@ func SignInRequire(redirect bool) martini.Handler { data["IsSigned"] = true data["SignedUserId"] = SignedInId(session) data["SignedUserName"] = SignedInName(session) - data["Avatar"] = SignedInUser(session).Avatar + data["SignedAvatar"] = SignedInUser(session).Avatar } } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index c34c33d74..6ff949b90 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -15,17 +15,14 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/utils/log" ) func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { data["Title"] = "Create repository" if req.Method == "GET" { - r.HTML(200, "repo/create", map[string]interface{}{ - "UserName": auth.SignedInName(session), - "UserId": auth.SignedInId(session), - "IsSigned": auth.IsSignedIn(session), - }) + r.HTML(200, "repo/create", data) return } @@ -42,56 +39,49 @@ func Create(req *http.Request, r render.Render, data base.TmplData, session sess _, err = models.CreateRepository(u, req.FormValue("name")) } if err == nil { - r.HTML(200, "repo/created", map[string]interface{}{ - "RepoName": u.Name + "/" + req.FormValue("name"), - "IsSigned": auth.IsSignedIn(session), - }) + data["RepoName"] = u.Name + "/" + req.FormValue("name") + r.HTML(200, "repo/created", data) return } } if err != nil { - r.HTML(200, "base/error", map[string]interface{}{ - "Error": fmt.Sprintf("%v", err), - "IsSigned": auth.IsSignedIn(session), - }) + data["ErrorMsg"] = err + log.Error("repo.Create: %v", err) + r.HTML(200, "base/error", data) } } -func Delete(req *http.Request, r render.Render, session sessions.Session) { +func Delete(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { + data["Title"] = "Delete repository" + if req.Method == "GET" { - r.HTML(200, "repo/delete", map[string]interface{}{ - "Title": "Delete repository", - "IsSigned": auth.IsSignedIn(session), - }) + r.HTML(200, "repo/delete", data) return } u := &models.User{} err := models.DeleteRepository(u, "") if err != nil { - r.HTML(200, "base/error", map[string]interface{}{ - "Error": fmt.Sprintf("%v", err), - "IsSigned": auth.IsSignedIn(session), - }) + data["ErrorMsg"] = err + log.Error("repo.Delete: %v", err) + r.HTML(200, "base/error", data) } } -func List(req *http.Request, r render.Render, session sessions.Session) { +func List(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) { + data["Title"] = "Repositories" + u := auth.SignedInUser(session) repos, err := models.GetRepositories(u) fmt.Println("repos", repos) if err != nil { - r.HTML(200, "base/error", map[string]interface{}{ - "Error": fmt.Sprintf("%v", err), - "IsSigned": auth.IsSignedIn(session), - }) + data["ErrorMsg"] = err + log.Error("repo.List: %v", err) + r.HTML(200, "base/error", data) return } - r.HTML(200, "repo/list", map[string]interface{}{ - "Title": "repositories", - "Repos": repos, - "IsSigned": auth.IsSignedIn(session), - }) + data["Repos"] = repos + r.HTML(200, "repo/list", data) } diff --git a/routers/user/ssh.go b/routers/user/ssh.go index c928407d4..658975352 100644 --- a/routers/user/ssh.go +++ b/routers/user/ssh.go @@ -5,7 +5,6 @@ package user import ( - "fmt" "net/http" "github.com/martini-contrib/render" @@ -13,14 +12,15 @@ import ( "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" + "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/utils/log" ) -func AddPublicKey(req *http.Request, r render.Render, session sessions.Session) { +func AddPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) { + data["Title"] = "Add Public Key" + if req.Method == "GET" { - r.HTML(200, "user/publickey_add", map[string]interface{}{ - "Title": "Add Public Key", - "IsSigned": auth.IsSignedIn(session), - }) + r.HTML(200, "user/publickey_add", data) return } @@ -30,28 +30,25 @@ func AddPublicKey(req *http.Request, r render.Render, session sessions.Session) } err := models.AddPublicKey(k) if err != nil { - r.HTML(403, "status/403", map[string]interface{}{ - "Title": fmt.Sprintf("%v", err), - "IsSigned": auth.IsSignedIn(session), - }) + data["ErrorMsg"] = err + log.Error("ssh.AddPublicKey: %v", err) + r.HTML(200, "base/error", data) } else { - r.HTML(200, "user/publickey_added", map[string]interface{}{}) + r.HTML(200, "user/publickey_added", data) } } -func ListPublicKey(req *http.Request, r render.Render, session sessions.Session) { +func ListPublicKey(req *http.Request, data base.TmplData, r render.Render, session sessions.Session) { + data["Title"] = "Public Keys" + keys, err := models.ListPublicKey(auth.SignedInId(session)) if err != nil { - r.HTML(200, "base/error", map[string]interface{}{ - "Error": fmt.Sprintf("%v", err), - "IsSigned": auth.IsSignedIn(session), - }) + data["ErrorMsg"] = err + log.Error("ssh.ListPublicKey: %v", err) + r.HTML(200, "base/error", data) return } - r.HTML(200, "user/publickey_list", map[string]interface{}{ - "Title": "repositories", - "Keys": keys, - "IsSigned": auth.IsSignedIn(session), - }) + data["Keys"] = keys + r.HTML(200, "user/publickey_list", data) } diff --git a/routers/user/user.go b/routers/user/user.go index fae376dd2..7eac9b742 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -5,9 +5,9 @@ package user import ( - "fmt" "net/http" + "github.com/codegangsta/martini" "github.com/martini-contrib/render" "github.com/martini-contrib/sessions" @@ -23,12 +23,18 @@ func Dashboard(r render.Render, data base.TmplData, session sessions.Session) { r.HTML(200, "user/dashboard", data) } -func Profile(r render.Render, data base.TmplData, session sessions.Session) { +func Profile(params martini.Params, r render.Render, data base.TmplData, session sessions.Session) { data["Title"] = "Profile" - data["IsSigned"] = auth.IsSignedIn(session) // TODO: Need to check view self or others. - user := auth.SignedInUser(session) + user, err := models.GetUserByName(params["username"]) + if err != nil { + data["ErrorMsg"] = err + log.Error("user.Profile: %v", err) + r.HTML(200, "base/error", data) + return + } + data["Avatar"] = user.Avatar data["Username"] = user.Name r.HTML(200, "user/profile", data) @@ -59,11 +65,10 @@ func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render data["ErrorMsg"] = err log.Error("user.SignIn: %v", data) - r.HTML(500, "base/error", nil) + r.HTML(200, "base/error", nil) return } - // login success session.Set("userId", user.Id) session.Set("userName", user.Name) r.Redirect("/") @@ -119,7 +124,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren default: data["ErrorMsg"] = err log.Error("user.SignUp: %v", data) - r.HTML(500, "base/error", nil) + r.HTML(200, "base/error", nil) } return } @@ -127,17 +132,18 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren r.Redirect("/user/login") } -func Delete(req *http.Request, r render.Render) { +// TODO: unfinished +func Delete(data base.TmplData, req *http.Request, r render.Render) { + data["Title"] = "Delete user" + if req.Method == "GET" { - r.HTML(200, "user/delete", map[string]interface{}{ - "Title": "Delete user", - }) + r.HTML(200, "user/delete", data) return } u := &models.User{} err := models.DeleteUser(u) - r.HTML(403, "status/403", map[string]interface{}{ - "Title": fmt.Sprintf("%v", err), - }) + data["ErrorMsg"] = err + log.Error("user.Delete: %v", data) + r.HTML(200, "base/error", nil) } diff --git a/templates/base/error.tmpl b/templates/base/error.tmpl index fbd424bff..8f6449420 100644 --- a/templates/base/error.tmpl +++ b/templates/base/error.tmpl @@ -1,6 +1,7 @@ {{template "base/head" .}} {{template "base/navbar" .}}
- An error is occurred : {{.ErrorMsg}} +

An error is occurred : {{.ErrorMsg}}

+

Application Version: {{AppVer}}

{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index 565fb30c8..12434dad2 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -6,8 +6,8 @@ Explore Help{{if .IsSigned}} - - user-avatar + + user-avatar diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index ffcffb09e..4c7f4dd27 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -6,15 +6,15 @@
-

{{.UserName}}

- +

{{.SignedUserName}}

+
- + Great repository names are short and memorable.
diff --git a/web.go b/web.go index b04746699..fa889ba1a 100644 --- a/web.go +++ b/web.go @@ -41,6 +41,9 @@ var AppHelpers template.FuncMap = map[string]interface{}{ "AppName": func() string { return utils.Cfg.MustValue("", "APP_NAME") }, + "AppVer": func() string { + return APP_VER + }, } func runWeb(*cli.Context) { @@ -48,7 +51,7 @@ func runWeb(*cli.Context) { m := martini.Classic() - // Middleware. + // Middlewares. m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}})) m.Use(base.InitContext()) @@ -61,14 +64,15 @@ func runWeb(*cli.Context) { m.Any("/user/login", auth.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn) m.Any("/user/logout", auth.SignInRequire(true), user.SignOut) m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) - m.Get("/user/profile", user.Profile) // should be /username m.Any("/user/delete", auth.SignInRequire(true), user.Delete) - m.Any("/user/publickey/add", user.AddPublicKey) - m.Any("/user/publickey/list", user.ListPublicKey) + m.Get("/user/:username", auth.SignInRequire(false), user.Profile) + + m.Any("/user/publickey/add", auth.SignInRequire(true), user.AddPublicKey) + m.Any("/user/publickey/list", auth.SignInRequire(true), user.ListPublicKey) m.Any("/repo/create", auth.SignInRequire(true), repo.Create) m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) - m.Any("/repo/list", repo.List) + m.Any("/repo/list", auth.SignInRequire(false), repo.List) listenAddr := fmt.Sprintf("%s:%s", utils.Cfg.MustValue("server", "HTTP_ADDR"),