diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 5801c29..3e871f8 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -158,6 +158,13 @@ func Routers(ctx gocontext.Context) *web.Route { m.Combo("").Patch(reqToken(), reqOrgOwnership(), bind(hat_api.EditOrgOption{}), org.Edit) }, orgAssignment(true)) + m.Group("/teams/{teamid}", func() { + m.Group("/repos", func() { + m.Put("/{org}", org.AddTeamAllRepository) + m.Delete("/{org}", org.RemoveTeamAllRepository) + }) + }, orgAssignment(false, true), reqToken(), reqTeamMembership()) + m.Group("/admin", func() { m.Group("/users", func() { m.Group("/{username}", func() { @@ -397,6 +404,43 @@ func reqOrgOwnership() func(ctx *context.APIContext) { } } +// reqTeamMembership user should be an team member, or a site admin +func reqTeamMembership() func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + if ctx.Context.IsUserSiteAdmin() { + return + } + if ctx.Org.Team == nil { + ctx.Error(http.StatusInternalServerError, "", "reqTeamMembership: unprepared context") + return + } + + orgID := ctx.Org.Team.OrgID + isOwner, err := organization.IsOrganizationOwner(ctx, orgID, ctx.Doer.ID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err) + return + } else if isOwner { + return + } + + if isTeamMember, err := organization.IsTeamMember(ctx, orgID, ctx.Org.Team.ID, ctx.Doer.ID); err != nil { + ctx.Error(http.StatusInternalServerError, "IsTeamMember", err) + return + } else if !isTeamMember { + isOrgMember, err := organization.IsOrganizationMember(ctx, orgID, ctx.Doer.ID) + if err != nil { + ctx.Error(http.StatusInternalServerError, "IsOrganizationMember", err) + } else if isOrgMember { + ctx.Error(http.StatusForbidden, "", "Must be a team member") + } else { + ctx.NotFound() + } + return + } + } +} + func reqWebhooksEnabled() func(ctx *context.APIContext) { return func(ctx *context.APIContext) { if setting.DisableWebhooks { diff --git a/routers/hat/org/team.go b/routers/hat/org/team.go new file mode 100644 index 0000000..07092a5 --- /dev/null +++ b/routers/hat/org/team.go @@ -0,0 +1,44 @@ +package org + +import ( + "net/http" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" +) + +func AddTeamAllRepository(ctx *context.APIContext) { + if ctx.Written() { + return + } + + if ctx.Org.Team.IncludesAllRepositories { + ctx.Error(http.StatusForbidden, "", "Team include all repository not be allow to edit") + return + } + + if err := models.AddAllRepositories(ctx.Org.Team); err != nil { + ctx.Error(http.StatusInternalServerError, "AddAllRepositories", err) + return + } + + ctx.Status(http.StatusNoContent) +} + +func RemoveTeamAllRepository(ctx *context.APIContext) { + + if ctx.Written() { + return + } + + if ctx.Org.Team.IncludesAllRepositories { + ctx.Error(http.StatusForbidden, "", "Team include all repository not be allow to edit") + return + } + + if err := models.RemoveAllRepositories(ctx.Org.Team); err != nil { + ctx.Error(http.StatusInternalServerError, "RemoveAllRepositories", err) + return + } + ctx.Status(http.StatusNoContent) +}