2022-03-29 08:29:02 +02:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-03-29 08:29:02 +02:00
|
|
|
|
|
|
|
package organization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/models/perm"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-10-18 07:50:37 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-12-07 08:27:36 +01:00
|
|
|
|
|
|
|
"xorm.io/builder"
|
2022-03-29 08:29:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ___________
|
|
|
|
// \__ ___/___ _____ _____
|
|
|
|
// | |_/ __ \\__ \ / \
|
|
|
|
// | |\ ___/ / __ \| Y Y \
|
|
|
|
// |____| \___ >____ /__|_| /
|
|
|
|
// \/ \/ \/
|
|
|
|
|
|
|
|
// ErrTeamAlreadyExist represents a "TeamAlreadyExist" kind of error.
|
|
|
|
type ErrTeamAlreadyExist struct {
|
|
|
|
OrgID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrTeamAlreadyExist checks if an error is a ErrTeamAlreadyExist.
|
|
|
|
func IsErrTeamAlreadyExist(err error) bool {
|
|
|
|
_, ok := err.(ErrTeamAlreadyExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrTeamAlreadyExist) Error() string {
|
|
|
|
return fmt.Sprintf("team already exists [org_id: %d, name: %s]", err.OrgID, err.Name)
|
|
|
|
}
|
|
|
|
|
2022-10-18 07:50:37 +02:00
|
|
|
func (err ErrTeamAlreadyExist) Unwrap() error {
|
|
|
|
return util.ErrAlreadyExist
|
|
|
|
}
|
|
|
|
|
2022-03-29 08:29:02 +02:00
|
|
|
// ErrTeamNotExist represents a "TeamNotExist" error
|
|
|
|
type ErrTeamNotExist struct {
|
|
|
|
OrgID int64
|
|
|
|
TeamID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrTeamNotExist checks if an error is a ErrTeamNotExist.
|
|
|
|
func IsErrTeamNotExist(err error) bool {
|
|
|
|
_, ok := err.(ErrTeamNotExist)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrTeamNotExist) Error() string {
|
|
|
|
return fmt.Sprintf("team does not exist [org_id %d, team_id %d, name: %s]", err.OrgID, err.TeamID, err.Name)
|
|
|
|
}
|
|
|
|
|
2022-10-18 07:50:37 +02:00
|
|
|
func (err ErrTeamNotExist) Unwrap() error {
|
|
|
|
return util.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2022-03-29 08:29:02 +02:00
|
|
|
// OwnerTeamName return the owner team name
|
|
|
|
const OwnerTeamName = "Owners"
|
|
|
|
|
|
|
|
// Team represents a organization team.
|
|
|
|
type Team struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
OrgID int64 `xorm:"INDEX"`
|
|
|
|
LowerName string
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
AccessMode perm.AccessMode `xorm:"'authorize'"`
|
|
|
|
Repos []*repo_model.Repository `xorm:"-"`
|
|
|
|
Members []*user_model.User `xorm:"-"`
|
|
|
|
NumRepos int
|
|
|
|
NumMembers int
|
|
|
|
Units []*TeamUnit `xorm:"-"`
|
|
|
|
IncludesAllRepositories bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
CanCreateOrgRepo bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Team))
|
|
|
|
db.RegisterModel(new(TeamUser))
|
|
|
|
db.RegisterModel(new(TeamRepo))
|
|
|
|
db.RegisterModel(new(TeamUnit))
|
2022-10-19 14:40:28 +02:00
|
|
|
db.RegisterModel(new(TeamInvite))
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 00:35:11 +02:00
|
|
|
func (t *Team) LogString() string {
|
2022-03-29 08:29:02 +02:00
|
|
|
if t == nil {
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 00:35:11 +02:00
|
|
|
return "<Team nil>"
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-22 00:35:11 +02:00
|
|
|
return fmt.Sprintf("<Team %d:%s OrgID=%d AccessMode=%s>", t.ID, t.Name, t.OrgID, t.AccessMode.LogString())
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
2023-02-19 09:31:39 +01:00
|
|
|
// LoadUnits load a list of available units for a team
|
|
|
|
func (t *Team) LoadUnits(ctx context.Context) (err error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
if t.Units != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Units, err = getUnitsByTeamID(ctx, t.ID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUnitNames returns the team units names
|
|
|
|
func (t *Team) GetUnitNames() (res []string) {
|
|
|
|
if t.AccessMode >= perm.AccessModeAdmin {
|
|
|
|
return unit.AllUnitKeyNames()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range t.Units {
|
|
|
|
res = append(res, unit.Units[u.Type].NameKey)
|
|
|
|
}
|
2022-06-20 12:02:49 +02:00
|
|
|
return res
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUnitsMap returns the team units permissions
|
|
|
|
func (t *Team) GetUnitsMap() map[string]string {
|
|
|
|
m := make(map[string]string)
|
|
|
|
if t.AccessMode >= perm.AccessModeAdmin {
|
|
|
|
for _, u := range unit.Units {
|
2024-04-17 17:58:37 +02:00
|
|
|
m[u.NameKey] = t.AccessMode.ToString()
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, u := range t.Units {
|
2024-04-17 17:58:37 +02:00
|
|
|
m[u.Unit().NameKey] = u.AccessMode.ToString()
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsOwnerTeam returns true if team is owner team.
|
|
|
|
func (t *Team) IsOwnerTeam() bool {
|
|
|
|
return t.Name == OwnerTeamName
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMember returns true if given user is a member of team.
|
2023-10-03 12:30:41 +02:00
|
|
|
func (t *Team) IsMember(ctx context.Context, userID int64) bool {
|
|
|
|
isMember, err := IsTeamMember(ctx, t.OrgID, t.ID, userID)
|
2022-03-29 08:29:02 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error("IsMember: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return isMember
|
|
|
|
}
|
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
// LoadRepositories returns paginated repositories in team of organization.
|
|
|
|
func (t *Team) LoadRepositories(ctx context.Context) (err error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
if t.Repos != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
t.Repos, err = GetTeamRepositories(ctx, &SearchTeamRepoOptions{
|
|
|
|
TeamID: t.ID,
|
|
|
|
})
|
2022-06-20 12:02:49 +02:00
|
|
|
return err
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-03 03:48:26 +01:00
|
|
|
// LoadMembers returns paginated members in team of organization.
|
|
|
|
func (t *Team) LoadMembers(ctx context.Context) (err error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
t.Members, err = GetTeamMembers(ctx, &SearchMembersOptions{
|
|
|
|
TeamID: t.ID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:31:37 +02:00
|
|
|
// UnitEnabled returns true if the team has the given unit type enabled
|
2022-12-10 03:46:31 +01:00
|
|
|
func (t *Team) UnitEnabled(ctx context.Context, tp unit.Type) bool {
|
|
|
|
return t.UnitAccessMode(ctx, tp) > perm.AccessModeNone
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 10:31:37 +02:00
|
|
|
// UnitAccessMode returns the access mode for the given unit type, "none" for non-existent units
|
2022-12-10 03:46:31 +01:00
|
|
|
func (t *Team) UnitAccessMode(ctx context.Context, tp unit.Type) perm.AccessMode {
|
2024-04-17 10:31:37 +02:00
|
|
|
accessMode, _ := t.UnitAccessModeEx(ctx, tp)
|
|
|
|
return accessMode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Team) UnitAccessModeEx(ctx context.Context, tp unit.Type) (accessMode perm.AccessMode, exist bool) {
|
2023-02-19 09:31:39 +01:00
|
|
|
if err := t.LoadUnits(ctx); err != nil {
|
2022-03-29 08:29:02 +02:00
|
|
|
log.Warn("Error loading team (ID: %d) units: %s", t.ID, err.Error())
|
|
|
|
}
|
2024-04-17 10:31:37 +02:00
|
|
|
for _, u := range t.Units {
|
|
|
|
if u.Type == tp {
|
|
|
|
return u.AccessMode, true
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-17 10:31:37 +02:00
|
|
|
return perm.AccessModeNone, false
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUsableTeamName tests if a name could be as team name
|
|
|
|
func IsUsableTeamName(name string) error {
|
|
|
|
switch name {
|
|
|
|
case "new":
|
|
|
|
return db.ErrNameReserved{Name: name}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
// GetTeam returns team by given team name and organization.
|
|
|
|
func GetTeam(ctx context.Context, orgID int64, name string) (*Team, error) {
|
2023-12-07 08:27:36 +01:00
|
|
|
t, exist, err := db.Get[Team](ctx, builder.Eq{"org_id": orgID, "lower_name": strings.ToLower(name)})
|
2022-03-29 08:29:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-12-07 08:27:36 +01:00
|
|
|
} else if !exist {
|
2022-03-29 08:29:02 +02:00
|
|
|
return nil, ErrTeamNotExist{orgID, 0, name}
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamIDsByNames returns a slice of team ids corresponds to names.
|
2023-10-03 12:30:41 +02:00
|
|
|
func GetTeamIDsByNames(ctx context.Context, orgID int64, names []string, ignoreNonExistent bool) ([]int64, error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
ids := make([]int64, 0, len(names))
|
|
|
|
for _, name := range names {
|
2023-10-03 12:30:41 +02:00
|
|
|
u, err := GetTeam(ctx, orgID, name)
|
2022-03-29 08:29:02 +02:00
|
|
|
if err != nil {
|
|
|
|
if ignoreNonExistent {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-22 13:48:42 +02:00
|
|
|
return nil, err
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
ids = append(ids, u.ID)
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOwnerTeam returns team by given team name and organization.
|
|
|
|
func GetOwnerTeam(ctx context.Context, orgID int64) (*Team, error) {
|
2022-05-20 16:08:52 +02:00
|
|
|
return GetTeam(ctx, orgID, OwnerTeamName)
|
2022-03-29 08:29:02 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
// GetTeamByID returns team by given ID.
|
|
|
|
func GetTeamByID(ctx context.Context, teamID int64) (*Team, error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
t := new(Team)
|
|
|
|
has, err := db.GetEngine(ctx).ID(teamID).Get(t)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrTeamNotExist{0, teamID, ""}
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamNamesByID returns team's lower name from a list of team ids.
|
2023-10-03 12:30:41 +02:00
|
|
|
func GetTeamNamesByID(ctx context.Context, teamIDs []int64) ([]string, error) {
|
2022-03-29 08:29:02 +02:00
|
|
|
if len(teamIDs) == 0 {
|
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var teamNames []string
|
2023-10-03 12:30:41 +02:00
|
|
|
err := db.GetEngine(ctx).Table("team").
|
2022-03-29 08:29:02 +02:00
|
|
|
Select("lower_name").
|
|
|
|
In("id", teamIDs).
|
|
|
|
Asc("name").
|
|
|
|
Find(&teamNames)
|
|
|
|
|
|
|
|
return teamNames, err
|
|
|
|
}
|
|
|
|
|
2022-08-25 04:31:57 +02:00
|
|
|
// IncrTeamRepoNum increases the number of repos for the given team by 1
|
|
|
|
func IncrTeamRepoNum(ctx context.Context, teamID int64) error {
|
|
|
|
_, err := db.GetEngine(ctx).Incr("num_repos").ID(teamID).Update(new(Team))
|
|
|
|
return err
|
|
|
|
}
|