1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

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

This commit is contained in:
Unknown 2014-03-07 17:08:21 -05:00
parent ba5c7ea771
commit a2a59f8ad1
11 changed files with 96 additions and 83 deletions

View file

@ -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())

View file

@ -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)

View file

@ -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}

View file

@ -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
}
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

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

View file

@ -6,8 +6,8 @@
<a class="gogs-nav-item" href="#">Explore</a>
<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-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 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>

View file

@ -6,15 +6,15 @@
<div class="form-group">
<label class="col-md-2 control-label">Owner<strong class="text-danger">*</strong></label>
<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 class="form-group">
<label class="col-md-2 control-label">Repository<strong class="text-danger">*</strong></label>
<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>
</div>
</div>

14
web.go
View file

@ -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"),