0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-08-01 10:39:17 +02:00
gitea/services/org/team.go
2024-10-30 10:59:08 -07:00

83 lines
2.5 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package org
import (
"context"
"strings"
"code.gitea.io/gitea/models"
org_model "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
unit_model "code.gitea.io/gitea/models/unit"
)
type UpdateTeamOptions struct {
TeamName string
Description string
IsAdmin bool
IncludesAllRepositories bool
CanCreateOrgRepo bool
UnitPerms map[unit_model.Type]perm.AccessMode
}
func UpdateTeam(ctx context.Context, team *org_model.Team, opts UpdateTeamOptions) error {
var changedCols []string
var newAccessMode perm.AccessMode
if opts.IsAdmin {
newAccessMode = perm.AccessModeAdmin
} else {
// if newAccessMode is less than admin accessmode, then it should be general accessmode,
// so we should calculate the minial accessmode from units accessmodes.
newAccessMode = unit_model.MinUnitAccessMode(opts.UnitPerms)
}
if !team.IsOwnerTeam() {
if team.Name != opts.TeamName {
team.Name = opts.TeamName
team.LowerName = strings.ToLower(opts.TeamName)
changedCols = append(changedCols, "name", "lower_name")
}
if team.AccessMode != newAccessMode {
team.AccessMode = newAccessMode
changedCols = append(changedCols, "authorize")
}
if team.IncludesAllRepositories != opts.IncludesAllRepositories {
team.IncludesAllRepositories = opts.IncludesAllRepositories
changedCols = append(changedCols, "includes_all_repositories")
}
if len(opts.UnitPerms) > 0 {
units := make([]*org_model.TeamUnit, 0, len(opts.UnitPerms))
for tp, perm := range opts.UnitPerms {
units = append(units, &org_model.TeamUnit{
OrgID: team.OrgID,
TeamID: team.ID,
Type: tp,
AccessMode: perm,
})
}
team.Units = units
changedCols = append(changedCols, "units")
}
if team.CanCreateOrgRepo != opts.CanCreateOrgRepo {
team.CanCreateOrgRepo = opts.CanCreateOrgRepo
changedCols = append(changedCols, "can_create_org_repo")
}
} else { // make the possible legacy data correct, we force to update these fields
team.CanCreateOrgRepo = true
team.IncludesAllRepositories = true
changedCols = append(changedCols, "can_create_org_repo", "includes_all_repositories")
}
if team.Description != opts.Description {
changedCols = append(changedCols, "description")
team.Description = opts.Description
}
return models.UpdateTeam(ctx, team, changedCols...)
}