mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
281 lines
7.3 KiB
281 lines
7.3 KiB
10 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
5 years ago
|
// Copyright 2020 The Gitea Authors.
|
||
2 years ago
|
// SPDX-License-Identifier: MIT
|
||
10 years ago
|
|
||
9 years ago
|
package context
|
||
10 years ago
|
|
||
|
import (
|
||
9 years ago
|
"strings"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
"code.gitea.io/gitea/models/perm"
|
||
2 years ago
|
"code.gitea.io/gitea/models/unit"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
9 months ago
|
"code.gitea.io/gitea/modules/markup"
|
||
|
"code.gitea.io/gitea/modules/markup/markdown"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
2 years ago
|
"code.gitea.io/gitea/modules/structs"
|
||
10 years ago
|
)
|
||
|
|
||
8 years ago
|
// Organization contains organization context
|
||
9 years ago
|
type Organization struct {
|
||
5 years ago
|
IsOwner bool
|
||
|
IsMember bool
|
||
|
IsTeamMember bool // Is member of team.
|
||
|
IsTeamAdmin bool // In owner team or team that has admin permission level.
|
||
3 years ago
|
Organization *organization.Organization
|
||
5 years ago
|
OrgLink string
|
||
|
CanCreateOrgRepo bool
|
||
1 year ago
|
PublicMemberOnly bool // Only display public members
|
||
9 years ago
|
|
||
3 years ago
|
Team *organization.Team
|
||
|
Teams []*organization.Team
|
||
9 years ago
|
}
|
||
|
|
||
2 years ago
|
func (org *Organization) CanWriteUnit(ctx *Context, unitType unit.Type) bool {
|
||
2 years ago
|
return org.Organization.UnitPermission(ctx, ctx.Doer, unitType) >= perm.AccessModeWrite
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (org *Organization) CanReadUnit(ctx *Context, unitType unit.Type) bool {
|
||
|
return org.Organization.UnitPermission(ctx, ctx.Doer, unitType) >= perm.AccessModeRead
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func GetOrganizationByParams(ctx *Context) {
|
||
|
orgName := ctx.Params(":org")
|
||
|
|
||
|
var err error
|
||
2 years ago
|
|
||
2 years ago
|
ctx.Org.Organization, err = organization.GetOrgByName(ctx, orgName)
|
||
|
if err != nil {
|
||
|
if organization.IsErrOrgNotExist(err) {
|
||
1 year ago
|
redirectUserID, err := user_model.LookupUserRedirect(ctx, orgName)
|
||
2 years ago
|
if err == nil {
|
||
2 years ago
|
RedirectToUser(ctx.Base, orgName, redirectUserID)
|
||
2 years ago
|
} else if user_model.IsErrUserRedirectNotExist(err) {
|
||
|
ctx.NotFound("GetUserByName", err)
|
||
|
} else {
|
||
|
ctx.ServerError("LookupUserRedirect", err)
|
||
|
}
|
||
|
} else {
|
||
|
ctx.ServerError("GetUserByName", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
8 years ago
|
// HandleOrgAssignment handles organization assignment
|
||
9 years ago
|
func HandleOrgAssignment(ctx *Context, args ...bool) {
|
||
|
var (
|
||
9 years ago
|
requireMember bool
|
||
|
requireOwner bool
|
||
|
requireTeamMember bool
|
||
9 years ago
|
requireTeamAdmin bool
|
||
9 years ago
|
)
|
||
|
if len(args) >= 1 {
|
||
|
requireMember = args[0]
|
||
|
}
|
||
|
if len(args) >= 2 {
|
||
|
requireOwner = args[1]
|
||
|
}
|
||
|
if len(args) >= 3 {
|
||
9 years ago
|
requireTeamMember = args[2]
|
||
|
}
|
||
|
if len(args) >= 4 {
|
||
9 years ago
|
requireTeamAdmin = args[3]
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
var err error
|
||
2 years ago
|
|
||
|
if ctx.ContextUser == nil {
|
||
|
// if Organization is not defined, get it from params
|
||
|
if ctx.Org.Organization == nil {
|
||
|
GetOrganizationByParams(ctx)
|
||
|
if ctx.Written() {
|
||
|
return
|
||
4 years ago
|
}
|
||
10 years ago
|
}
|
||
2 years ago
|
} else if ctx.ContextUser.IsOrganization() {
|
||
|
if ctx.Org == nil {
|
||
|
ctx.Org = &Organization{}
|
||
|
}
|
||
|
ctx.Org.Organization = (*organization.Organization)(ctx.ContextUser)
|
||
|
} else {
|
||
|
// ContextUser is an individual User
|
||
9 years ago
|
return
|
||
|
}
|
||
2 years ago
|
|
||
9 years ago
|
org := ctx.Org.Organization
|
||
2 years ago
|
|
||
|
// Handle Visibility
|
||
|
if org.Visibility != structs.VisibleTypePublic && !ctx.IsSigned {
|
||
|
// We must be signed in to see limited or private organizations
|
||
|
ctx.NotFound("OrgAssignment", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if org.Visibility == structs.VisibleTypePrivate {
|
||
|
requireMember = true
|
||
|
} else if ctx.IsSigned && ctx.Doer.IsRestricted {
|
||
|
requireMember = true
|
||
|
}
|
||
|
|
||
3 years ago
|
ctx.ContextUser = org.AsUser()
|
||
9 years ago
|
ctx.Data["Org"] = org
|
||
10 years ago
|
|
||
9 years ago
|
// Admin has super access.
|
||
3 years ago
|
if ctx.IsSigned && ctx.Doer.IsAdmin {
|
||
9 years ago
|
ctx.Org.IsOwner = true
|
||
|
ctx.Org.IsMember = true
|
||
9 years ago
|
ctx.Org.IsTeamMember = true
|
||
9 years ago
|
ctx.Org.IsTeamAdmin = true
|
||
5 years ago
|
ctx.Org.CanCreateOrgRepo = true
|
||
9 years ago
|
} else if ctx.IsSigned {
|
||
1 year ago
|
ctx.Org.IsOwner, err = org.IsOwnedBy(ctx, ctx.Doer.ID)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
ctx.ServerError("IsOwnedBy", err)
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if ctx.Org.IsOwner {
|
||
|
ctx.Org.IsMember = true
|
||
9 years ago
|
ctx.Org.IsTeamMember = true
|
||
9 years ago
|
ctx.Org.IsTeamAdmin = true
|
||
5 years ago
|
ctx.Org.CanCreateOrgRepo = true
|
||
9 years ago
|
} else {
|
||
1 year ago
|
ctx.Org.IsMember, err = org.IsOrgMember(ctx, ctx.Doer.ID)
|
||
7 years ago
|
if err != nil {
|
||
7 years ago
|
ctx.ServerError("IsOrgMember", err)
|
||
7 years ago
|
return
|
||
10 years ago
|
}
|
||
1 year ago
|
ctx.Org.CanCreateOrgRepo, err = org.CanCreateOrgRepo(ctx, ctx.Doer.ID)
|
||
5 years ago
|
if err != nil {
|
||
|
ctx.ServerError("CanCreateOrgRepo", err)
|
||
|
return
|
||
|
}
|
||
10 years ago
|
}
|
||
9 years ago
|
} else {
|
||
|
// Fake data.
|
||
3 years ago
|
ctx.Data["SignedUser"] = &user_model.User{}
|
||
9 years ago
|
}
|
||
|
if (requireMember && !ctx.Org.IsMember) ||
|
||
|
(requireOwner && !ctx.Org.IsOwner) {
|
||
7 years ago
|
ctx.NotFound("OrgAssignment", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
ctx.Data["IsOrganizationOwner"] = ctx.Org.IsOwner
|
||
9 years ago
|
ctx.Data["IsOrganizationMember"] = ctx.Org.IsMember
|
||
3 years ago
|
ctx.Data["IsPackageEnabled"] = setting.Packages.Enabled
|
||
2 years ago
|
ctx.Data["IsRepoIndexerEnabled"] = setting.Indexer.RepoIndexerEnabled
|
||
3 years ago
|
ctx.Data["IsPublicMember"] = func(uid int64) bool {
|
||
1 year ago
|
is, _ := organization.IsPublicMembership(ctx, ctx.Org.Organization.ID, uid)
|
||
3 years ago
|
return is
|
||
|
}
|
||
5 years ago
|
ctx.Data["CanCreateOrgRepo"] = ctx.Org.CanCreateOrgRepo
|
||
10 years ago
|
|
||
3 years ago
|
ctx.Org.OrgLink = org.AsUser().OrganisationLink()
|
||
9 years ago
|
ctx.Data["OrgLink"] = ctx.Org.OrgLink
|
||
10 years ago
|
|
||
1 year ago
|
// Member
|
||
|
ctx.Org.PublicMemberOnly = ctx.Doer == nil || !ctx.Org.IsMember && !ctx.Doer.IsAdmin
|
||
|
opts := &organization.FindOrgMembersOpts{
|
||
|
OrgID: org.ID,
|
||
|
PublicOnly: ctx.Org.PublicMemberOnly,
|
||
|
}
|
||
1 year ago
|
ctx.Data["NumMembers"], err = organization.CountOrgMembers(ctx, opts)
|
||
1 year ago
|
if err != nil {
|
||
|
ctx.ServerError("CountOrgMembers", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
// Team.
|
||
9 years ago
|
if ctx.Org.IsMember {
|
||
3 years ago
|
shouldSeeAllTeams := false
|
||
9 years ago
|
if ctx.Org.IsOwner {
|
||
3 years ago
|
shouldSeeAllTeams = true
|
||
|
} else {
|
||
1 year ago
|
teams, err := org.GetUserTeams(ctx, ctx.Doer.ID)
|
||
3 years ago
|
if err != nil {
|
||
|
ctx.ServerError("GetUserTeams", err)
|
||
|
return
|
||
|
}
|
||
|
for _, team := range teams {
|
||
|
if team.IncludesAllRepositories && team.AccessMode >= perm.AccessModeAdmin {
|
||
|
shouldSeeAllTeams = true
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if shouldSeeAllTeams {
|
||
1 year ago
|
ctx.Org.Teams, err = org.LoadTeams(ctx)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
ctx.ServerError("LoadTeams", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
} else {
|
||
1 year ago
|
ctx.Org.Teams, err = org.GetUserTeams(ctx, ctx.Doer.ID)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
ctx.ServerError("GetUserTeams", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
1 year ago
|
ctx.Data["NumTeams"] = len(ctx.Org.Teams)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
teamName := ctx.Params(":team")
|
||
|
if len(teamName) > 0 {
|
||
9 years ago
|
teamExists := false
|
||
3 years ago
|
for _, team := range ctx.Org.Teams {
|
||
9 years ago
|
if team.LowerName == strings.ToLower(teamName) {
|
||
9 years ago
|
teamExists = true
|
||
|
ctx.Org.Team = team
|
||
|
ctx.Org.IsTeamMember = true
|
||
|
ctx.Data["Team"] = ctx.Org.Team
|
||
|
break
|
||
10 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
if !teamExists {
|
||
7 years ago
|
ctx.NotFound("OrgAssignment", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
ctx.Data["IsTeamMember"] = ctx.Org.IsTeamMember
|
||
|
if requireTeamMember && !ctx.Org.IsTeamMember {
|
||
7 years ago
|
ctx.NotFound("OrgAssignment", err)
|
||
10 years ago
|
return
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
3 years ago
|
ctx.Org.IsTeamAdmin = ctx.Org.Team.IsOwnerTeam() || ctx.Org.Team.AccessMode >= perm.AccessModeAdmin
|
||
9 years ago
|
ctx.Data["IsTeamAdmin"] = ctx.Org.IsTeamAdmin
|
||
|
if requireTeamAdmin && !ctx.Org.IsTeamAdmin {
|
||
7 years ago
|
ctx.NotFound("OrgAssignment", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
1 year ago
|
ctx.Data["ContextUser"] = ctx.ContextUser
|
||
2 years ago
|
|
||
|
ctx.Data["CanReadProjects"] = ctx.Org.CanReadUnit(ctx, unit.TypeProjects)
|
||
|
ctx.Data["CanReadPackages"] = ctx.Org.CanReadUnit(ctx, unit.TypePackages)
|
||
|
ctx.Data["CanReadCode"] = ctx.Org.CanReadUnit(ctx, unit.TypeCode)
|
||
9 months ago
|
|
||
|
ctx.Data["IsFollowing"] = ctx.Doer != nil && user_model.IsFollowing(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
|
||
|
if len(ctx.ContextUser.Description) != 0 {
|
||
|
content, err := markdown.RenderString(&markup.RenderContext{
|
||
|
Metas: map[string]string{"mode": "document"},
|
||
|
Ctx: ctx,
|
||
|
}, ctx.ContextUser.Description)
|
||
|
if err != nil {
|
||
|
ctx.ServerError("RenderString", err)
|
||
|
return
|
||
|
}
|
||
|
ctx.Data["RenderedDescription"] = content
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
4 years ago
|
// OrgAssignment returns a middleware to handle organization assignment
|
||
4 years ago
|
func OrgAssignment(args ...bool) func(ctx *Context) {
|
||
9 years ago
|
return func(ctx *Context) {
|
||
|
HandleOrgAssignment(ctx, args...)
|
||
10 years ago
|
}
|
||
|
}
|