From 902372067cad9cfe7360473d1b985217a308a949 Mon Sep 17 00:00:00 2001 From: Rob Richards Date: Thu, 23 Mar 2017 11:57:43 -0500 Subject: [PATCH] user/settings: add repositories panel (#4312) * Add Repositories panel to user settings issue #4277 * modified personal repo settings format --- cmd/web.go | 5 ++- routers/user/setting.go | 63 +++++++++++++++++++++++++++++ templates/user/settings/navbar.tmpl | 3 ++ templates/user/settings/repos.tmpl | 49 ++++++++++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 templates/user/settings/repos.tmpl diff --git a/cmd/web.go b/cmd/web.go index 0948028c3..b92e5d050 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -255,7 +255,10 @@ func runWeb(ctx *cli.Context) error { m.Get("", user.SettingsOrganizations) m.Post("/leave", user.SettingsLeaveOrganization) }) - + m.Group("/repos", func() { + m.Get("", user.SettingsRepos) + m.Post("/delete", user.SettingsDeleteRepo) + }) m.Route("/delete", "GET,POST", user.SettingsDelete) }, reqSignIn, func(ctx *context.Context) { ctx.Data["PageIsUserSettings"] = true diff --git a/routers/user/setting.go b/routers/user/setting.go index c2c273c22..70e00a3d3 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -7,9 +7,11 @@ package user import ( "fmt" "io/ioutil" + "net/url" "strings" "github.com/Unknwon/com" + "github.com/Unknwon/paginater" log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" @@ -30,6 +32,7 @@ const ( SETTINGS_SOCIAL base.TplName = "user/settings/social" SETTINGS_APPLICATIONS base.TplName = "user/settings/applications" SETTINGS_ORGANIZATIONS base.TplName = "user/settings/organizations" + SETTINGS_REPOS base.TplName = "user/settings/repos" SETTINGS_DELETE base.TplName = "user/settings/delete" NOTIFICATION base.TplName = "user/notification" SECURITY base.TplName = "user/security" @@ -450,6 +453,66 @@ func SettingsLeaveOrganization(ctx *context.Context) { }) } +func SettingsRepos(ctx *context.Context) { + + ctx.Data["Title"] = ctx.Tr("admin.repositories") + ctx.Data["PageIsSettingsRepositories"] = true + + keyword := ctx.Query("q") + page := ctx.QueryInt("page") + if page <= 0 { + page = 1 + } + + repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{ + Keyword: keyword, + UserID: ctx.User.ID, + OrderBy: "lower_name", + Page: page, + PageSize: setting.UI.Admin.RepoPagingNum, + }) + if err != nil { + ctx.Handle(500, "SearchRepositoryByName", err) + return + } + if err = models.RepositoryList(repos).LoadAttributes(); err != nil { + ctx.Handle(500, "LoadAttributes", err) + return + } + + ctx.Data["Keyword"] = keyword + ctx.Data["Total"] = count + ctx.Data["Page"] = paginater.New(int(count), setting.UI.Admin.RepoPagingNum, page, 5) + ctx.Data["Repos"] = repos + ctx.HTML(200, SETTINGS_REPOS) +} + +func SettingsDeleteRepo(ctx *context.Context) { + repo, err := models.GetRepositoryByID(ctx.QueryInt64("id")) + if err != nil { + ctx.Handle(500, "GetRepositoryByID", err) + return + } + // make sure the user owns the repository or is an admin before allowing them to delete it + if repo.OwnerID == ctx.User.ID || ctx.User.IsAdmin { + if err := models.DeleteRepository(repo.MustOwner().ID, repo.ID); err != nil { + ctx.Handle(500, "DeleteRepository", err) + return + } + log.Trace("Repository deleted: %s/%s", repo.MustOwner().Name, repo.Name) + + ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success")) + ctx.JSON(200, map[string]interface{}{ + "redirect": setting.AppSubUrl + "/user/settings/repos?page=" + ctx.Query("page") + "&q=" + url.QueryEscape(ctx.Query("q")), + }) + } else { + // logged in user doesn't have rights to delete this repository + err := errors.New("You do not have rights to delete repository '" + repo.FullName() + "'") + ctx.Handle(403, "SettingsDeleteRepo", err) + } + +} + func SettingsDelete(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsDelete"] = true diff --git a/templates/user/settings/navbar.tmpl b/templates/user/settings/navbar.tmpl index 4293fed09..a7744b5a0 100644 --- a/templates/user/settings/navbar.tmpl +++ b/templates/user/settings/navbar.tmpl @@ -22,6 +22,9 @@ {{.i18n.Tr "settings.orgs"}} + + {{.i18n.Tr "admin.repositories"}} + {{.i18n.Tr "settings.delete"}} diff --git a/templates/user/settings/repos.tmpl b/templates/user/settings/repos.tmpl new file mode 100644 index 000000000..e19dbfb4e --- /dev/null +++ b/templates/user/settings/repos.tmpl @@ -0,0 +1,49 @@ +{{template "base/head" .}} +
+
+
+ {{template "user/settings/navbar" .}} +
+ {{template "base/alert" .}} +

+ {{.i18n.Tr "admin.repositories"}} ({{.i18n.Tr "admin.total" .Total}}) +

+
+ {{template "admin/base/search" .}} +
+ + {{range .Repos}} +
+
+
+ + + {{.Owner.Name}}/{{.Name}} + + {{.Size | FileSize}} + {{if .IsPrivate}} + + {{end}} +
+
+
+ {{end}} + + {{template "admin/base/page" .}} +
+
+
+
+ + +{{template "base/footer" .}}