Browse Source

Change /user/profile URL to /user/:username

for-closed-social
Unknown 10 years ago
parent
commit
a2a59f8ad1
11 changed files with 96 additions and 83 deletions
  1. +1
    -1
      gogs.go
  2. +1
    -3
      models/repo.go
  3. +17
    -0
      models/user.go
  4. +1
    -1
      modules/auth/user.go
  5. +22
    -32
      routers/repo/repo.go
  6. +18
    -21
      routers/user/ssh.go
  7. +20
    -14
      routers/user/user.go
  8. +2
    -1
      templates/base/error.tmpl
  9. +2
    -2
      templates/base/navbar.tmpl
  10. +3
    -3
      templates/repo/create.tmpl
  11. +9
    -5
      web.go

+ 1
- 1
gogs.go View File

@ -19,7 +19,7 @@ import (
// Test that go1.1 tag above is included in builds. main.go refers to this definition. // Test that go1.1 tag above is included in builds. main.go refers to this definition.
const go11tag = true const go11tag = true
const APP_VER = "0.0.1.0307"
const APP_VER = "0.0.2.0307"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

+ 1
- 3
models/repo.go View File

@ -48,9 +48,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
return s.IsDir(), nil 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) { func CreateRepository(user *User, reposName string) (*Repo, error) {
f := RepoPath(user.Name, reposName) f := RepoPath(user.Name, reposName)
_, err := git.InitRepository(f, true) _, err := git.InitRepository(f, true)

+ 17
- 0
models/user.go View File

@ -190,6 +190,23 @@ func GetUserById(id int64) (*User, error) {
return user, nil 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. // LoginUserPlain validates user by raw user name and password.
func LoginUserPlain(name, passwd string) (*User, error) { func LoginUserPlain(name, passwd string) (*User, error) {
user := User{LowerName: strings.ToLower(name), Passwd: passwd} user := User{LowerName: strings.ToLower(name), Passwd: passwd}

+ 1
- 1
modules/auth/user.go View File

@ -69,7 +69,7 @@ func SignInRequire(redirect bool) martini.Handler {
data["IsSigned"] = true data["IsSigned"] = true
data["SignedUserId"] = SignedInId(session) data["SignedUserId"] = SignedInId(session)
data["SignedUserName"] = SignedInName(session) data["SignedUserName"] = SignedInName(session)
data["Avatar"] = SignedInUser(session).Avatar
data["SignedAvatar"] = SignedInUser(session).Avatar
} }
} }

+ 22
- 32
routers/repo/repo.go View File

@ -15,17 +15,14 @@ import (
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base" "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) { func Create(req *http.Request, r render.Render, data base.TmplData, session sessions.Session) {
data["Title"] = "Create repository" data["Title"] = "Create repository"
if req.Method == "GET" { 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 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")) _, err = models.CreateRepository(u, req.FormValue("name"))
} }
if err == nil { 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 return
} }
} }
if err != nil { 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" { 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 return
} }
u := &models.User{} u := &models.User{}
err := models.DeleteRepository(u, "") err := models.DeleteRepository(u, "")
if err != nil { 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) u := auth.SignedInUser(session)
repos, err := models.GetRepositories(u) repos, err := models.GetRepositories(u)
fmt.Println("repos", repos) fmt.Println("repos", repos)
if err != nil { 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 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)
} }

+ 18
- 21
routers/user/ssh.go View File

@ -5,7 +5,6 @@
package user package user
import ( import (
"fmt"
"net/http" "net/http"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
@ -13,14 +12,15 @@ import (
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth" "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" { 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 return
} }
@ -30,28 +30,25 @@ func AddPublicKey(req *http.Request, r render.Render, session sessions.Session)
} }
err := models.AddPublicKey(k) err := models.AddPublicKey(k)
if err != nil { 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 { } 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)) keys, err := models.ListPublicKey(auth.SignedInId(session))
if err != nil { 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 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)
} }

+ 20
- 14
routers/user/user.go View File

@ -5,9 +5,9 @@
package user package user
import ( import (
"fmt"
"net/http" "net/http"
"github.com/codegangsta/martini"
"github.com/martini-contrib/render" "github.com/martini-contrib/render"
"github.com/martini-contrib/sessions" "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) 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["Title"] = "Profile"
data["IsSigned"] = auth.IsSignedIn(session)
// TODO: Need to check view self or others. // 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["Avatar"] = user.Avatar
data["Username"] = user.Name data["Username"] = user.Name
r.HTML(200, "user/profile", data) 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 data["ErrorMsg"] = err
log.Error("user.SignIn: %v", data) log.Error("user.SignIn: %v", data)
r.HTML(500, "base/error", nil)
r.HTML(200, "base/error", nil)
return return
} }
// login success
session.Set("userId", user.Id) session.Set("userId", user.Id)
session.Set("userName", user.Name) session.Set("userName", user.Name)
r.Redirect("/") r.Redirect("/")
@ -119,7 +124,7 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren
default: default:
data["ErrorMsg"] = err data["ErrorMsg"] = err
log.Error("user.SignUp: %v", data) log.Error("user.SignUp: %v", data)
r.HTML(500, "base/error", nil)
r.HTML(200, "base/error", nil)
} }
return return
} }
@ -127,17 +132,18 @@ func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r ren
r.Redirect("/user/login") 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" { if req.Method == "GET" {
r.HTML(200, "user/delete", map[string]interface{}{
"Title": "Delete user",
})
r.HTML(200, "user/delete", data)
return return
} }
u := &models.User{} u := &models.User{}
err := models.DeleteUser(u) 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)
} }

+ 2
- 1
templates/base/error.tmpl View File

@ -1,6 +1,7 @@
{{template "base/head" .}} {{template "base/head" .}}
{{template "base/navbar" .}} {{template "base/navbar" .}}
<div id="gogs-body" class="container"> <div id="gogs-body" class="container">
An error is occurred : {{.ErrorMsg}}
<p>An error is occurred : {{.ErrorMsg}}</p>
<p>Application Version: {{AppVer}}</p>
</div> </div>
{{template "base/footer" .}} {{template "base/footer" .}}

+ 2
- 2
templates/base/navbar.tmpl View File

@ -6,8 +6,8 @@
<a class="gogs-nav-item" href="#">Explore</a> <a class="gogs-nav-item" href="#">Explore</a>
<a class="gogs-nav-item" href="#">Help</a>{{if .IsSigned}} <a class="gogs-nav-item" href="#">Help</a>{{if .IsSigned}}
<a id="gogs-nav-out" class="gogs-nav-item navbar-right navbar-btn btn btn-danger" href="/user/logout/"><i class="fa fa-power-off fa-lg"></i></a> <a id="gogs-nav-out" class="gogs-nav-item navbar-right navbar-btn btn btn-danger" href="/user/logout/"><i class="fa fa-power-off fa-lg"></i></a>
<a id="gogs-nav-avatar" class="gogs-nav-item navbar-right" href="/user/profile" data-toggle="tooltip" data-placement="bottom" title="{{.SignedUserName}}">
<img src="http://1.gravatar.com/avatar/{{.Avatar}}?s=28" alt="user-avatar" title="username"/>
<a id="gogs-nav-avatar" class="gogs-nav-item navbar-right" href="/user/{{.SignedUserName}}" data-toggle="tooltip" data-placement="bottom" title="{{.SignedUserName}}">
<img src="http://1.gravatar.com/avatar/{{.SignedAvatar}}?s=28" alt="user-avatar" title="username"/>
</a> </a>
<a class="navbar-right gogs-nav-item" href="/repo/create" data-toggle="tooltip" data-placement="bottom" title="New Repository"><i class="fa fa-plus fa-lg"></i></a> <a class="navbar-right gogs-nav-item" href="/repo/create" data-toggle="tooltip" data-placement="bottom" title="New Repository"><i class="fa fa-plus fa-lg"></i></a>
<a class="navbar-right gogs-nav-item" href="/user/publickey/list" data-toggle="tooltip" data-placement="bottom" title="Setting"><i class="fa fa-cogs fa-lg"></i></a> <a class="navbar-right gogs-nav-item" href="/user/publickey/list" data-toggle="tooltip" data-placement="bottom" title="Setting"><i class="fa fa-cogs fa-lg"></i></a>

+ 3
- 3
templates/repo/create.tmpl View File

@ -6,15 +6,15 @@
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<p class="form-control-static">{{.UserName}}</p>
<input type="hidden" value="{{.UserId}}" name="userId"/>
<p class="form-control-static">{{.SignedUserName}}</p>
<input type="hidden" value="{{.SignedUserId}}" name="userId"/>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label> <label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label>
<div class="col-md-8"> <div class="col-md-8">
<input name="repo" type="text" class="form-control" placeholder="Type your repository name">
<input name="repo" type="text" class="form-control" placeholder="Type your repository name" required="required">
<span class="help-block">Great repository names are short and memorable. </span> <span class="help-block">Great repository names are short and memorable. </span>
</div> </div>
</div> </div>

+ 9
- 5
web.go View File

@ -41,6 +41,9 @@ var AppHelpers template.FuncMap = map[string]interface{}{
"AppName": func() string { "AppName": func() string {
return utils.Cfg.MustValue("", "APP_NAME") return utils.Cfg.MustValue("", "APP_NAME")
}, },
"AppVer": func() string {
return APP_VER
},
} }
func runWeb(*cli.Context) { func runWeb(*cli.Context) {
@ -48,7 +51,7 @@ func runWeb(*cli.Context) {
m := martini.Classic() m := martini.Classic()
// Middleware.
// Middlewares.
m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}})) m.Use(render.Renderer(render.Options{Funcs: []template.FuncMap{AppHelpers}}))
m.Use(base.InitContext()) 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/login", auth.SignOutRequire(), binding.BindIgnErr(auth.LogInForm{}), user.SignIn)
m.Any("/user/logout", auth.SignInRequire(true), user.SignOut) m.Any("/user/logout", auth.SignInRequire(true), user.SignOut)
m.Any("/user/sign_up", auth.SignOutRequire(), binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) 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/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/create", auth.SignInRequire(true), repo.Create)
m.Any("/repo/delete", auth.SignInRequire(true), repo.Delete) 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", listenAddr := fmt.Sprintf("%s:%s",
utils.Cfg.MustValue("server", "HTTP_ADDR"), utils.Cfg.MustValue("server", "HTTP_ADDR"),

Loading…
Cancel
Save