diff options
author | Unknwon <joe2010xtmf@163.com> | 2014-07-26 23:53:16 -0400 |
---|---|---|
committer | Unknwon <joe2010xtmf@163.com> | 2014-07-26 23:53:16 -0400 |
commit | 98131614119f39a3f341392c98a4d2320b5b8e21 (patch) | |
tree | 041d90457cd9213d6214faca4001af5a849bbe90 /routers | |
parent | 5e8138341357f367868a32b631495c9d5a286477 (diff) | |
download | gitea-98131614119f39a3f341392c98a4d2320b5b8e21.tar.gz gitea-98131614119f39a3f341392c98a4d2320b5b8e21.zip |
Reser repo owner to organization when error occurs in repo create page, dashboard news feed page, create organization page
Diffstat (limited to 'routers')
-rw-r--r-- | routers/org/org.go | 65 | ||||
-rw-r--r-- | routers/repo/repo.go | 52 | ||||
-rw-r--r-- | routers/user/home.go | 75 |
3 files changed, 89 insertions, 103 deletions
diff --git a/routers/org/org.go b/routers/org/org.go index 74527d6008..9b8a13e614 100644 --- a/routers/org/org.go +++ b/routers/org/org.go @@ -10,12 +10,11 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" - "github.com/gogits/gogs/routers/user" ) const ( HOME base.TplName = "org/home" - NEW base.TplName = "org/new" + CREATE base.TplName = "org/create" SETTINGS base.TplName = "org/settings" ) @@ -55,23 +54,23 @@ func Home(ctx *middleware.Context) { ctx.HTML(200, HOME) } -func New(ctx *middleware.Context) { - ctx.Data["Title"] = "Create An Organization" - ctx.HTML(200, NEW) +func Create(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("new_org") + ctx.HTML(200, CREATE) } -func NewPost(ctx *middleware.Context, form auth.CreateOrgForm) { - ctx.Data["Title"] = "Create An Organization" +func CreatePost(ctx *middleware.Context, form auth.CreateOrgForm) { + ctx.Data["Title"] = ctx.Tr("new_org") if ctx.HasError() { - ctx.HTML(200, NEW) + ctx.HTML(200, CREATE) return } org := &models.User{ Name: form.OrgName, Email: form.Email, - IsActive: true, // NOTE: may need to set false when require e-mail confirmation. + IsActive: true, Type: models.ORGANIZATION, } @@ -80,61 +79,23 @@ func NewPost(ctx *middleware.Context, form auth.CreateOrgForm) { switch err { case models.ErrUserAlreadyExist: ctx.Data["Err_OrgName"] = true - ctx.RenderWithErr("Organization name has been already taken", NEW, &form) + ctx.RenderWithErr(ctx.Tr("form.org_name_been_taken"), CREATE, &form) case models.ErrEmailAlreadyUsed: ctx.Data["Err_Email"] = true - ctx.RenderWithErr("E-mail address has been already used", NEW, &form) + ctx.RenderWithErr(ctx.Tr("form.email_been_used"), CREATE, &form) case models.ErrUserNameIllegal: ctx.Data["Err_OrgName"] = true - ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), NEW, &form) + ctx.RenderWithErr(ctx.Tr("form.illegal_org_name"), CREATE, &form) default: - ctx.Handle(500, "org.NewPost(CreateUser)", err) + ctx.Handle(500, "CreateUser", err) } return } - log.Trace("%s Organization created: %s", ctx.Req.RequestURI, org.Name) + log.Trace("Organization created: %s", org.Name) ctx.Redirect("/org/" + form.OrgName + "/dashboard") } -func Dashboard(ctx *middleware.Context) { - ctx.Data["Title"] = "Dashboard" - ctx.Data["PageIsUserDashboard"] = true - ctx.Data["PageIsOrgDashboard"] = true - - org, err := models.GetUserByName(ctx.Params(":org")) - if err != nil { - if err == models.ErrUserNotExist { - ctx.Handle(404, "org.Dashboard(GetUserByName)", err) - } else { - ctx.Handle(500, "org.Dashboard(GetUserByName)", err) - } - return - } - - if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) - return - } - ctx.Data["Orgs"] = ctx.User.Orgs - ctx.Data["ContextUser"] = org - - ctx.Data["MyRepos"], err = models.GetRepositories(org.Id, true) - if err != nil { - ctx.Handle(500, "org.Dashboard(GetRepositories)", err) - return - } - - actions, err := models.GetFeeds(org.Id, 0, false) - if err != nil { - ctx.Handle(500, "org.Dashboard(GetFeeds)", err) - return - } - ctx.Data["Feeds"] = actions - - ctx.HTML(200, user.DASHBOARD) -} - func Settings(ctx *middleware.Context) { ctx.Data["Title"] = "Settings" diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 8de1e0936e..5e7875e22f 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -36,11 +36,10 @@ func Create(ctx *middleware.Context) { ctx.Data["Licenses"] = models.Licenses ctxUser := ctx.User - orgId := com.StrTo(ctx.Query("org")).MustInt64() - if orgId > 0 { + if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 { org, err := models.GetUserById(orgId) if err != nil && err != models.ErrUserNotExist { - ctx.Handle(500, "home.Dashboard(GetUserById)", err) + ctx.Handle(500, "GetUserById", err) return } ctxUser = org @@ -48,10 +47,10 @@ func Create(ctx *middleware.Context) { ctx.Data["ContextUser"] = ctxUser if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + ctx.Handle(500, "GetOrganizations", err) return } - ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...) + ctx.Data["Orgs"] = ctx.User.Orgs ctx.HTML(200, CREATE) } @@ -64,11 +63,11 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Licenses"] = models.Licenses ctxUser := ctx.User - orgId := com.StrTo(ctx.Query("org")).MustInt64() - if orgId > 0 { - org, err := models.GetUserById(orgId) + // Not equal means current user is an organization. + if form.Uid != ctx.User.Id { + org, err := models.GetUserById(form.Uid) if err != nil && err != models.ErrUserNotExist { - ctx.Handle(500, "home.Dashboard(GetUserById)", err) + ctx.Handle(500, "GetUserById", err) return } ctxUser = org @@ -76,7 +75,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["ContextUser"] = ctxUser if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.CreatePost(GetOrganizations)", err) + ctx.Handle(500, "GetOrganizations", err) return } ctx.Data["Orgs"] = ctx.User.Orgs @@ -86,32 +85,19 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { return } - u := ctx.User - // Not equal means current user is an organization. - if u.Id != form.Uid { - var err error - u, err = models.GetUserById(form.Uid) - if err != nil { - if err == models.ErrUserNotExist { - ctx.Handle(404, "home.CreatePost(GetUserById)", err) - } else { - ctx.Handle(500, "home.CreatePost(GetUserById)", err) - } - return - } - + if ctxUser.IsOrganization() { // Check ownership of organization. - if !u.IsOrgOwner(ctx.User.Id) { + if !ctxUser.IsOrgOwner(ctx.User.Id) { ctx.Error(403) return } } - repo, err := models.CreateRepository(u, form.RepoName, form.Description, + repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description, form.Gitignore, form.License, form.Private, false, form.InitReadme) if err == nil { - log.Trace("Repository created: %s/%s", u.Name, form.RepoName) - ctx.Redirect("/" + u.Name + "/" + form.RepoName) + log.Trace("Repository created: %s/%s", ctxUser.Name, form.RepoName) + ctx.Redirect("/" + ctxUser.Name + "/" + form.RepoName) return } else if err == models.ErrRepoAlreadyExist { ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form) @@ -122,7 +108,7 @@ func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { } if repo != nil { - if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil { + if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } @@ -134,7 +120,7 @@ func Migrate(ctx *middleware.Context) { ctx.Data["PageIsNewRepo"] = true if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Migrate(GetOrganizations)", err) + ctx.Handle(500, "GetOrganizations", err) return } ctx.Data["Orgs"] = ctx.User.Orgs @@ -147,7 +133,7 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.Data["PageIsNewRepo"] = true if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.MigratePost(GetOrganizations)", err) + ctx.Handle(500, "GetOrganizations", err) return } ctx.Data["Orgs"] = ctx.User.Orgs @@ -164,9 +150,9 @@ func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { u, err = models.GetUserById(form.Uid) if err != nil { if err == models.ErrUserNotExist { - ctx.Handle(404, "home.MigratePost(GetUserById)", err) + ctx.Handle(404, "GetUserById", err) } else { - ctx.Handle(500, "home.MigratePost(GetUserById)", err) + ctx.Handle(500, "GetUserById", err) } return } diff --git a/routers/user/home.go b/routers/user/home.go index 186cbe41a7..706c16d91f 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -17,8 +17,8 @@ import ( const ( DASHBOARD base.TplName = "user/dashboard/dashboard" + PULLS base.TplName = "user/dashboard/pulls" ISSUES base.TplName = "user/issues" - PULLS base.TplName = "user/pulls" STARS base.TplName = "user/stars" PROFILE base.TplName = "user/profile" ) @@ -28,26 +28,59 @@ func Dashboard(ctx *middleware.Context) { ctx.Data["PageIsDashboard"] = true ctx.Data["PageIsNews"] = true + var ctxUser *models.User + // Check context type. + orgName := ctx.Params(":org") + if len(orgName) > 0 { + // Organization. + org, err := models.GetUserByName(orgName) + if err != nil { + if err == models.ErrUserNotExist { + ctx.Handle(404, "GetUserByName", err) + } else { + ctx.Handle(500, "GetUserByName", err) + } + return + } + ctxUser = org + } else { + // Normal user. + ctxUser = ctx.User + collaborates, err := models.GetCollaborativeRepos(ctxUser.Name) + if err != nil { + ctx.Handle(500, "GetCollaborativeRepos", err) + return + } + ctx.Data["CollaborateCount"] = len(collaborates) + ctx.Data["CollaborativeRepos"] = collaborates + } + ctx.Data["ContextUser"] = ctxUser + if err := ctx.User.GetOrganizations(); err != nil { - ctx.Handle(500, "home.Dashboard(GetOrganizations)", err) + ctx.Handle(500, "GetOrganizations", err) return } - ctx.Data["ContextUser"] = ctx.User + ctx.Data["Orgs"] = ctx.User.Orgs - repos, err := models.GetRepositories(ctx.User.Id, true) + repos, err := models.GetRepositories(ctxUser.Id, true) if err != nil { ctx.Handle(500, "GetRepositories", err) return } ctx.Data["Repos"] = repos - ctx.Data["CollaborativeRepos"], err = models.GetCollaborativeRepos(ctx.User.Name) - if err != nil { - ctx.Handle(500, "GetCollaborativeRepos", err) - return + // Get mirror repositories. + mirrors := make([]*models.Repository, 0, len(repos)/2) + for _, repo := range repos { + if repo.IsMirror { + mirrors = append(mirrors, repo) + } } + ctx.Data["MirrorCount"] = len(mirrors) + ctx.Data["Mirrors"] = mirrors - actions, err := models.GetFeeds(ctx.User.Id, 0, true) + // Get feeds. + actions, err := models.GetFeeds(ctxUser.Id, 0, false) if err != nil { ctx.Handle(500, "GetFeeds", err) return @@ -57,7 +90,7 @@ func Dashboard(ctx *middleware.Context) { feeds := make([]*models.Action, 0, len(actions)) for _, act := range actions { if act.IsPrivate { - if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName, + if has, _ := models.HasAccess(ctxUser.Name, act.RepoUserName+"/"+act.RepoName, models.READABLE); !has { continue } @@ -68,6 +101,20 @@ func Dashboard(ctx *middleware.Context) { ctx.HTML(200, DASHBOARD) } +func Pulls(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("pull_requests") + ctx.Data["PageIsDashboard"] = true + ctx.Data["PageIsPulls"] = true + + if err := ctx.User.GetOrganizations(); err != nil { + ctx.Handle(500, "GetOrganizations", err) + return + } + ctx.Data["ContextUser"] = ctx.User + + ctx.HTML(200, PULLS) +} + func Profile(ctx *middleware.Context) { ctx.Data["Title"] = "Profile" ctx.Data["PageIsUserProfile"] = true @@ -279,11 +326,3 @@ func Issues(ctx *middleware.Context) { } ctx.HTML(200, ISSUES) } - -func Pulls(ctx *middleware.Context) { - ctx.HTML(200, PULLS) -} - -func Stars(ctx *middleware.Context) { - ctx.HTML(200, STARS) -} |