From 3a2eee08278a71320140129ec29c8586264d9968 Mon Sep 17 00:00:00 2001 From: slene Date: Mon, 17 Mar 2014 14:36:28 +0800 Subject: [PATCH] fix repo setting and delete repo --- models/repo.go | 3 ++ modules/auth/repo.go | 7 ---- routers/repo/repo.go | 54 +++++++++++------------------ routers/repo/single.go | 3 +- templates/repo/delete.tmpl | 12 ------- templates/repo/setting.tmpl | 68 ++++++++++++++++++++++++++----------- web.go | 5 +-- 7 files changed, 77 insertions(+), 75 deletions(-) delete mode 100644 templates/repo/delete.tmpl diff --git a/models/repo.go b/models/repo.go index cfca3583f8..3b35f49753 100644 --- a/models/repo.go +++ b/models/repo.go @@ -307,6 +307,9 @@ func DeleteRepository(userId, repoId int64, userName string) (err error) { } session := orm.NewSession() + if err = session.Begin(); err != nil { + return err + } if _, err = session.Delete(&Repository{Id: repoId}); err != nil { session.Rollback() return err diff --git a/modules/auth/repo.go b/modules/auth/repo.go index ac1b6b699b..2cc93744ce 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -17,7 +17,6 @@ import ( ) type CreateRepoForm struct { - UserId int64 `form:"userId"` RepoName string `form:"repo" binding:"Required;AlphaDash"` Visibility string `form:"visibility"` Description string `form:"desc" binding:"MaxSize(100)"` @@ -52,9 +51,3 @@ func (f *CreateRepoForm) Validate(errors *binding.Errors, req *http.Request, con validate(errors, data, f) } - -type DeleteRepoForm struct { - UserId int64 `form:"userId" binding:"Required"` - UserName string `form:"userName" binding:"Required"` - RepoId int64 `form:"repoId" binding:"Required"` -} diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 116c199b7d..edd8862794 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -20,49 +20,35 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { return } - if ctx.HasError() { - ctx.Render.HTML(200, "repo/create", ctx.Data) + if _, err := models.CreateRepository(ctx.User, + form.RepoName, form.Description, form.Language, form.License, + form.Visibility == "private", form.InitReadme == "on"); err == nil { + ctx.Render.Redirect("/"+ctx.User.Name+"/"+form.RepoName, 302) return - } - - // TODO: access check - - user, err := models.GetUserById(form.UserId) - if err != nil { - if err.Error() == models.ErrUserNotExist.Error() { - ctx.RenderWithErr("User does not exist", "repo/create", &form) - return - } - } - - if err == nil { - if _, err = models.CreateRepository(user, - form.RepoName, form.Description, form.Language, form.License, - form.Visibility == "private", form.InitReadme == "on"); err == nil { - ctx.Render.Redirect("/"+user.Name+"/"+form.RepoName, 302) - return - } - } - - if err.Error() == models.ErrRepoAlreadyExist.Error() { + } else if err == models.ErrRepoAlreadyExist { ctx.RenderWithErr("Repository name has already been used", "repo/create", &form) return } - - ctx.Handle(200, "repo.Create", err) } -func Delete(ctx *middleware.Context, form auth.DeleteRepoForm) { - ctx.Data["Title"] = "Delete repository" - - if ctx.Req.Method == "GET" { - ctx.Render.HTML(200, "repo/delete", ctx.Data) +func SettingPost(ctx *middleware.Context) { + if !ctx.Repo.IsOwner { + ctx.Render.Error(404) return } - if err := models.DeleteRepository(form.UserId, form.RepoId, form.UserName); err != nil { - ctx.Handle(200, "repo.Delete", err) - return + switch ctx.Query("action") { + case "delete": + if len(ctx.Repo.Repository.Name) == 0 || ctx.Repo.Repository.Name != ctx.Query("repository") { + ctx.Data["ErrorMsg"] = "Please make sure you entered repository name is correct." + ctx.Render.HTML(200, "repo/setting", ctx.Data) + return + } + + if err := models.DeleteRepository(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.LowerName); err != nil { + ctx.Handle(200, "repo.Delete", err) + return + } } ctx.Render.Redirect("/", 302) diff --git a/routers/repo/single.go b/routers/repo/single.go index c4189ad6d0..fdc4e6e7f8 100644 --- a/routers/repo/single.go +++ b/routers/repo/single.go @@ -65,7 +65,8 @@ func Single(ctx *middleware.Context, params martini.Params) { } func Setting(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsValid { + if !ctx.Repo.IsOwner { + ctx.Render.Error(404) return } diff --git a/templates/repo/delete.tmpl b/templates/repo/delete.tmpl deleted file mode 100644 index 0b95c3fb18..0000000000 --- a/templates/repo/delete.tmpl +++ /dev/null @@ -1,12 +0,0 @@ -{{template "base/head" .}} -{{template "base/navbar" .}} -
-
-
-
- -
-
-
-
-{{template "base/footer" .}} \ No newline at end of file diff --git a/templates/repo/setting.tmpl b/templates/repo/setting.tmpl index 225e229429..06f0ed4d50 100644 --- a/templates/repo/setting.tmpl +++ b/templates/repo/setting.tmpl @@ -4,30 +4,60 @@ {{template "repo/toolbar" .}}
-

Repository Settings

-
-
-

Repository Options

+
+ {{if .ErrorMsg}}

{{.ErrorMsg}}

{{end}} +
+
+ Repository Options +
+
+ +
-
-

Delete Repository

-

Unexpected bad things will happen if you don't read this!

-

This action CANNOT be undone. This will delete the repository, wiki, issues, and comments permanently.

+
+
+ Danger Zone +
+
+ +
+
Delete this repository.
+
Once you delete a repository, there is no going back. Please be certain.
+ -
- - - -
- -
+ +
diff --git a/web.go b/web.go index 16f39bdb0e..86ea8a5bc0 100644 --- a/web.go +++ b/web.go @@ -68,11 +68,12 @@ func runWeb(*cli.Context) { m.Get("/user/:username", middleware.SignInRequire(false), user.Profile) m.Any("/repo/create", middleware.SignInRequire(true), binding.BindIgnErr(auth.CreateRepoForm{}), repo.Create) - m.Any("/repo/delete", middleware.SignInRequire(true), binding.Bind(auth.DeleteRepoForm{}), repo.Delete) m.Get("/help", routers.Help) - m.Get("/:username/:reponame/settings", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Setting) + m.Post("/:username/:reponame/settings", middleware.SignInRequire(true), middleware.RepoAssignment(true), repo.SettingPost) + m.Get("/:username/:reponame/settings", middleware.SignInRequire(true), middleware.RepoAssignment(true), repo.Setting) + m.Get("/:username/:reponame/commits", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Commits) m.Get("/:username/:reponame/issues", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Issues) m.Get("/:username/:reponame/pulls", middleware.SignInRequire(false), middleware.RepoAssignment(true), repo.Pulls)