]> source.dussan.org Git - gitea.git/commitdiff
Clean repo code
authorUnknown <joe2010xtmf@163.com>
Mon, 5 May 2014 23:58:13 +0000 (19:58 -0400)
committerUnknown <joe2010xtmf@163.com>
Mon, 5 May 2014 23:58:13 +0000 (19:58 -0400)
13 files changed:
cmd/web.go
modules/auth/admin.go
modules/auth/auth.go
modules/auth/authentication.go
modules/auth/repo.go
modules/middleware/binding/binding.go
modules/middleware/repo.go
routers/admin/admin.go
routers/admin/user.go
routers/repo/repo.go
routers/repo/setting.go
routers/user/user.go
templates/admin/nav.tmpl

index f81512554a7a9a617272cc3821e06d093da649a0..855ce7721c6970445f780388ef86c6ade953f2b4 100644 (file)
@@ -130,7 +130,7 @@ func runWeb(*cli.Context) {
        m.Get("/user/:username", ignSignIn, user.Profile)
 
        m.Group("/repo", func(r martini.Router) {
-               r.Get("/create", repo.Create) // TODO
+               r.Get("/create", repo.Create)
                r.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost)
                r.Get("/migrate", repo.Migrate)
                r.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost)
@@ -165,14 +165,19 @@ func runWeb(*cli.Context) {
                m.Get("/template/**", dev.TemplatePreview)
        }
 
+       reqOwner := middleware.RequireOwner
+
        m.Group("/:username/:reponame", func(r martini.Router) {
                r.Get("/settings", repo.Setting)
-               r.Post("/settings", repo.SettingPost)
+               r.Post("/settings", bindIgnErr(auth.RepoSettingForm{}), repo.SettingPost)
                r.Get("/settings/collaboration", repo.Collaboration)
                r.Post("/settings/collaboration", repo.CollaborationPost)
-               r.Get("/settings/hooks", repo.WebHooks)
+               r.Get("/settings/hooks", repo.WebHooks) // TODO
                r.Get("/settings/hooks/add", repo.WebHooksAdd)
                r.Get("/settings/hooks/id", repo.WebHooksEdit)
+       }, reqSignIn, middleware.RepoAssignment(true), reqOwner())
+
+       m.Group("/:username/:reponame", func(r martini.Router) {
                r.Get("/action/:action", repo.Action)
                r.Get("/issues/new", repo.CreateIssue)
                r.Post("/issues/new", bindIgnErr(auth.CreateIssueForm{}), repo.CreateIssuePost)
index 39d2d09620044e0de3074cb751cd9864891efea7..02a4dc489af9b686907e4a7387c5a9cca4ec8dac 100644 (file)
@@ -11,7 +11,6 @@ import (
        "github.com/go-martini/martini"
 
        "github.com/gogits/gogs/modules/base"
-       "github.com/gogits/gogs/modules/log"
        "github.com/gogits/gogs/modules/middleware/binding"
 )
 
@@ -36,20 +35,6 @@ func (f *AdminEditUserForm) Name(field string) string {
 }
 
 func (f *AdminEditUserForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
-       if req.Method == "GET" || errors.Count() == 0 {
-               return
-       }
-
        data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
-       data["HasError"] = true
-       AssignForm(f, data)
-
-       if len(errors.Overall) > 0 {
-               for _, err := range errors.Overall {
-                       log.Error("AdminEditUserForm.Validate: %v", err)
-               }
-               return
-       }
-
        validate(errors, data, f)
 }
index a0e00c10c55c56cdbeae6def762e75775fb1c733..a7b281e4c9e80846a2285419e43df8fe426656d2 100644 (file)
@@ -183,20 +183,6 @@ func (f *InstallForm) Name(field string) string {
 }
 
 func (f *InstallForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
-       if req.Method == "GET" || errors.Count() == 0 {
-               return
-       }
-
        data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
-       data["HasError"] = true
-       AssignForm(f, data)
-
-       if len(errors.Overall) > 0 {
-               for _, err := range errors.Overall {
-                       log.Error("InstallForm.Validate: %v", err)
-               }
-               return
-       }
-
        validate(errors, data, f)
 }
index 376a52e9a7b546a1ed4069f003eb7db5f34f5732..51a9469cbd5f9a98c000f5fbb88352d33fdb5807 100644 (file)
@@ -11,7 +11,6 @@ import (
        "github.com/go-martini/martini"
 
        "github.com/gogits/gogs/modules/base"
-       "github.com/gogits/gogs/modules/log"
        "github.com/gogits/gogs/modules/middleware/binding"
 )
 
@@ -44,20 +43,6 @@ func (f *AuthenticationForm) Name(field string) string {
 }
 
 func (f *AuthenticationForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
-       if req.Method == "GET" || errors.Count() == 0 {
-               return
-       }
-
        data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
-       data["HasError"] = true
-       AssignForm(f, data)
-
-       if len(errors.Overall) > 0 {
-               for _, err := range errors.Overall {
-                       log.Error("AuthenticationForm.Validate: %v", err)
-               }
-               return
-       }
-
        validate(errors, data, f)
 }
index e61e8202ff259f88dd621988393f97b93fa7908a..54fa99c83d2c79dfd368129ccd917ae686686520 100644 (file)
@@ -11,12 +11,11 @@ import (
        "github.com/go-martini/martini"
 
        "github.com/gogits/gogs/modules/base"
-       "github.com/gogits/gogs/modules/log"
        "github.com/gogits/gogs/modules/middleware/binding"
 )
 
 type CreateRepoForm struct {
-       RepoName    string `form:"repo" binding:"Required;AlphaDash"`
+       RepoName    string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"`
        Private     bool   `form:"private"`
        Description string `form:"desc" binding:"MaxSize(100)"`
        Language    string `form:"language"`
@@ -33,21 +32,7 @@ func (f *CreateRepoForm) Name(field string) string {
 }
 
 func (f *CreateRepoForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
-       if req.Method == "GET" || errors.Count() == 0 {
-               return
-       }
-
        data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
-       data["HasError"] = true
-       AssignForm(f, data)
-
-       if len(errors.Overall) > 0 {
-               for _, err := range errors.Overall {
-                       log.Error("CreateRepoForm.Validate: %v", err)
-               }
-               return
-       }
-
        validate(errors, data, f)
 }
 
@@ -55,7 +40,7 @@ type MigrateRepoForm struct {
        Url          string `form:"url" binding:"Url"`
        AuthUserName string `form:"auth_username"`
        AuthPasswd   string `form:"auth_password"`
-       RepoName     string `form:"repo" binding:"Required;AlphaDash"`
+       RepoName     string `form:"repo" binding:"Required;AlphaDash;MaxSize(100)"`
        Mirror       bool   `form:"mirror"`
        Private      bool   `form:"private"`
        Description  string `form:"desc" binding:"MaxSize(100)"`
@@ -71,20 +56,30 @@ func (f *MigrateRepoForm) Name(field string) string {
 }
 
 func (f *MigrateRepoForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
-       if req.Method == "GET" || errors.Count() == 0 {
-               return
-       }
-
        data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
-       data["HasError"] = true
-       AssignForm(f, data)
+       validate(errors, data, f)
+}
 
-       if len(errors.Overall) > 0 {
-               for _, err := range errors.Overall {
-                       log.Error("MigrateRepoForm.Validate: %v", err)
-               }
-               return
+type RepoSettingForm struct {
+       RepoName    string `form:"name" binding:"Required;AlphaDash;MaxSize(100)"`
+       Description string `form:"desc" binding:"MaxSize(100)"`
+       Website     string `form:"url" binding:"Url;MaxSize(100)"`
+       Branch      string `form:"branch"`
+       Interval    int    `form:"interval"`
+       Private     bool   `form:"private"`
+       GoGet       bool   `form:"goget"`
+}
+
+func (f *RepoSettingForm) Name(field string) string {
+       names := map[string]string{
+               "RepoName":    "Repository name",
+               "Description": "Description",
+               "Website":     "Website address",
        }
+       return names[field]
+}
 
+func (f *RepoSettingForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) {
+       data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
        validate(errors, data, f)
 }
index 6de595515464b43871d170eaca893c47ad72a0d3..93fb51d994ff4fdb458f2221ca0298eb78917718 100644 (file)
@@ -267,7 +267,10 @@ func validateStruct(errors *BindingErrors, obj interface{}) {
                                        break
                                }
                        case rule == "Url":
-                               if !urlPattern.MatchString(fmt.Sprintf("%v", fieldValue)) {
+                               str := fmt.Sprintf("%v", fieldValue)
+                               if len(str) == 0 {
+                                       continue
+                               } else if !urlPattern.MatchString(str) {
                                        errors.Fields[field.Name] = BindingUrlError
                                        break
                                }
index f681ac139ac61ec4919b15143c1a4327cc668617..7d0cc97970787cc9d129904bcaeea1ead562ae87 100644 (file)
@@ -7,6 +7,7 @@ package middleware
 import (
        "errors"
        "fmt"
+       "net/url"
        "strings"
 
        "github.com/go-martini/martini"
@@ -238,3 +239,17 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler {
                ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching
        }
 }
+
+func RequireOwner() martini.Handler {
+       return func(ctx *Context) {
+               if !ctx.Repo.IsOwner {
+                       if !ctx.IsSigned {
+                               ctx.SetCookie("redirect_to", "/"+url.QueryEscape(ctx.Req.RequestURI))
+                               ctx.Redirect("/user/login")
+                               return
+                       }
+                       ctx.Handle(404, ctx.Req.RequestURI, nil)
+                       return
+               }
+       }
+}
index eafe8cb41b8c8ca822eda203a928cfaeda438ced..96721bfdebfc6bd35ef30d558f998c53e5ce0443 100644 (file)
@@ -112,38 +112,38 @@ func Users(ctx *middleware.Context) {
        ctx.Data["PageIsUsers"] = true
 
        var err error
-       ctx.Data["Users"], err = models.GetUsers(100, 0)
+       ctx.Data["Users"], err = models.GetUsers(200, 0)
        if err != nil {
-               ctx.Handle(200, "admin.Users", err)
+               ctx.Handle(500, "admin.Users", err)
                return
        }
        ctx.HTML(200, "admin/users")
 }
 
-func Auths(ctx *middleware.Context) {
-       ctx.Data["Title"] = "Auth Sources"
-       ctx.Data["PageIsAuths"] = true
+func Repositories(ctx *middleware.Context) {
+       ctx.Data["Title"] = "Repository Management"
+       ctx.Data["PageIsRepos"] = true
 
        var err error
-       ctx.Data["Sources"], err = models.GetAuths()
+       ctx.Data["Repos"], err = models.GetRepos(200, 0)
        if err != nil {
-               ctx.Handle(200, "admin.Auths", err)
+               ctx.Handle(500, "admin.Repositories", err)
                return
        }
-       ctx.HTML(200, "admin/auths")
+       ctx.HTML(200, "admin/repos")
 }
 
-func Repositories(ctx *middleware.Context) {
-       ctx.Data["Title"] = "Repository Management"
-       ctx.Data["PageIsRepos"] = true
+func Auths(ctx *middleware.Context) {
+       ctx.Data["Title"] = "Auth Sources"
+       ctx.Data["PageIsAuths"] = true
 
        var err error
-       ctx.Data["Repos"], err = models.GetRepos(100, 0)
+       ctx.Data["Sources"], err = models.GetAuths()
        if err != nil {
-               ctx.Handle(200, "admin.Repositories", err)
+               ctx.Handle(500, "admin.Auths", err)
                return
        }
-       ctx.HTML(200, "admin/repos")
+       ctx.HTML(200, "admin/auths")
 }
 
 func Config(ctx *middleware.Context) {
index f2e1b0473001310386a0998d62258c1244cf1346..0d60ee9a764f3e6d58860ae607d5f0889004f9f7 100644 (file)
@@ -34,16 +34,15 @@ func NewUserPost(ctx *middleware.Context, form auth.RegisterForm) {
        ctx.Data["Title"] = "New Account"
        ctx.Data["PageIsUsers"] = true
 
+       if ctx.HasError() {
+               ctx.HTML(200, "admin/users/new")
+               return
+       }
+
        if form.Password != form.RetypePasswd {
-               ctx.Data["HasError"] = true
                ctx.Data["Err_Password"] = true
                ctx.Data["Err_RetypePasswd"] = true
-               ctx.Data["ErrorMsg"] = "Password and re-type password are not same"
-               auth.AssignForm(form, ctx.Data)
-       }
-
-       if ctx.HasError() {
-               ctx.HTML(200, "admin/users/new")
+               ctx.RenderWithErr("Password and re-type password are not same.", "admin/users/new", &form)
                return
        }
 
index e82c6ae988285865fe1793e38513d497c975f730..19c9dddc6ee58220e042f8670f053715b598eba7 100644 (file)
@@ -8,13 +8,14 @@ import (
        "encoding/base64"
        "errors"
        "fmt"
-       "github.com/gogits/git"
        "path"
        "path/filepath"
        "strings"
 
        "github.com/go-martini/martini"
 
+       "github.com/gogits/git"
+
        "github.com/gogits/gogs/models"
        "github.com/gogits/gogs/modules/auth"
        "github.com/gogits/gogs/modules/base"
index b0ec7618300dd0a60a8dc2ed8aa83a379f7d2c19..c308739207754b9733f2094b2283a440a10af174 100644 (file)
@@ -11,6 +11,7 @@ import (
        "github.com/gogits/git"
 
        "github.com/gogits/gogs/models"
+       "github.com/gogits/gogs/modules/auth"
        "github.com/gogits/gogs/modules/base"
        "github.com/gogits/gogs/modules/log"
        "github.com/gogits/gogs/modules/mailer"
@@ -18,27 +19,22 @@ import (
 )
 
 func Setting(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Handle(404, "repo.Setting", nil)
-               return
-       }
-
        ctx.Data["IsRepoToolbarSetting"] = true
        ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - settings"
        ctx.HTML(200, "repo/setting")
 }
 
-func SettingPost(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Error(404)
-               return
-       }
-
+func SettingPost(ctx *middleware.Context, form auth.RepoSettingForm) {
        ctx.Data["IsRepoToolbarSetting"] = true
 
        switch ctx.Query("action") {
        case "update":
-               newRepoName := ctx.Query("name")
+               if ctx.HasError() {
+                       ctx.HTML(200, "repo/setting")
+                       return
+               }
+
+               newRepoName := form.RepoName
                // Check if repository name has been changed.
                if ctx.Repo.Repository.Name != newRepoName {
                        isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName)
@@ -57,15 +53,15 @@ func SettingPost(ctx *middleware.Context) {
                        ctx.Repo.Repository.Name = newRepoName
                }
 
-               br := ctx.Query("branch")
+               br := form.Branch
 
                if git.IsBranchExist(models.RepoPath(ctx.User.Name, ctx.Repo.Repository.Name), br) {
                        ctx.Repo.Repository.DefaultBranch = br
                }
-               ctx.Repo.Repository.Description = ctx.Query("desc")
-               ctx.Repo.Repository.Website = ctx.Query("site")
-               ctx.Repo.Repository.IsPrivate = ctx.Query("private") == "on"
-               ctx.Repo.Repository.IsGoget = ctx.Query("goget") == "on"
+               ctx.Repo.Repository.Description = form.Description
+               ctx.Repo.Repository.Website = form.Website
+               ctx.Repo.Repository.IsPrivate = form.Private
+               ctx.Repo.Repository.IsGoget = form.GoGet
                if err := models.UpdateRepository(ctx.Repo.Repository); err != nil {
                        ctx.Handle(404, "repo.SettingPost(update)", err)
                        return
@@ -73,12 +69,9 @@ func SettingPost(ctx *middleware.Context) {
                log.Trace("%s Repository updated: %s/%s", ctx.Req.RequestURI, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
 
                if ctx.Repo.Repository.IsMirror {
-                       if len(ctx.Query("interval")) > 0 {
-                               var err error
-                               ctx.Repo.Mirror.Interval, err = base.StrTo(ctx.Query("interval")).Int()
-                               if err != nil {
-                                       log.Error("repo.SettingPost(get mirror interval): %v", err)
-                               } else if err = models.UpdateMirror(ctx.Repo.Mirror); err != nil {
+                       if form.Interval > 0 {
+                               ctx.Repo.Mirror.Interval = form.Interval
+                               if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
                                        log.Error("repo.SettingPost(UpdateMirror): %v", err)
                                }
                        }
@@ -125,11 +118,6 @@ func SettingPost(ctx *middleware.Context) {
 }
 
 func Collaboration(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Error(404)
-               return
-       }
-
        repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
        ctx.Data["IsRepoToolbarCollaboration"] = true
        ctx.Data["Title"] = repoLink + " - collaboration"
@@ -166,11 +154,6 @@ func Collaboration(ctx *middleware.Context) {
 }
 
 func CollaborationPost(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Error(404)
-               return
-       }
-
        repoLink := strings.TrimPrefix(ctx.Repo.RepoLink, "/")
        name := strings.ToLower(ctx.Query("collaborator"))
        if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
@@ -215,33 +198,18 @@ func CollaborationPost(ctx *middleware.Context) {
 }
 
 func WebHooks(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Handle(404, "repo.WebHooks", nil)
-               return
-       }
-
        ctx.Data["IsRepoToolbarWebHooks"] = true
        ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Web Hooks"
        ctx.HTML(200, "repo/hooks")
 }
 
 func WebHooksAdd(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Handle(404, "repo.WebHooksAdd", nil)
-               return
-       }
-
        ctx.Data["IsRepoToolbarWebHooks"] = true
        ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Web Hook"
        ctx.HTML(200, "repo/hooks_add")
 }
 
 func WebHooksEdit(ctx *middleware.Context) {
-       if !ctx.Repo.IsOwner {
-               ctx.Handle(404, "repo.WebHooksEdit", nil)
-               return
-       }
-
        ctx.Data["IsRepoToolbarWebHooks"] = true
        ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Web Hook Name"
        ctx.HTML(200, "repo/hooks_edit")
index 7af65fc585a74f55a820e2a3e4fd0ee1cdbd16ec..e33353abdb65234300c13b328b932427e721e8a2 100644 (file)
@@ -217,7 +217,7 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) {
        if form.Password != form.RetypePasswd {
                ctx.Data["Err_Password"] = true
                ctx.Data["Err_RetypePasswd"] = true
-               ctx.RenderWithErr("Password and re-type password are not same", "user/signup", &form)
+               ctx.RenderWithErr("Password and re-type password are not same.", "user/signup", &form)
                return
        }
 
index f27b8bb24ba0944ca537508dc85676a599866ce8..5ba449579605d9150a74952f8c38e7455994c8b8 100644 (file)
@@ -3,7 +3,7 @@
         <li class="list-group-item{{if .PageIsDashboard}} active{{end}}"><a href="/admin"><i class="fa fa-tachometer fa-lg"></i> Dashboard</a></li>
         <li class="list-group-item{{if .PageIsUsers}} active{{end}}"><a href="/admin/users"><i class="fa fa-users fa-lg"></i> Users</a></li>
         <li class="list-group-item{{if .PageIsRepos}} active{{end}}"><a href="/admin/repos"><i class="fa fa-book fa-lg"></i> Repositories</a></li>
-        <li class="list-group-item{{if .PageIsConfig}} active{{end}}"><a href="/admin/config"><i class="fa fa-cogs fa-lg"></i> Configuration</a></li>
         <li class="list-group-item{{if .PageIsAuths}} active{{end}}"><a href="/admin/auths"><i class="fa fa-certificate fa-lg"></i> Authentication</a></li>
+        <li class="list-group-item{{if .PageIsConfig}} active{{end}}"><a href="/admin/config"><i class="fa fa-cogs fa-lg"></i> Configuration</a></li>
     </ul>
 </div>
\ No newline at end of file