mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-14 06:31:11 +02:00
refactor subgroup loading, add method to load only groups accessible by a user
This commit is contained in:
parent
af769893ce
commit
e8fe57571a
@ -2,6 +2,7 @@ package group
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
|
"code.gitea.io/gitea/models/unit"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
@ -45,7 +46,7 @@ func init() {
|
|||||||
db.RegisterModel(new(Group))
|
db.RegisterModel(new(Group))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) doLoadSubgroups(ctx context.Context, recursive bool, currentLevel int) error {
|
func (g *Group) doLoadSubgroups(ctx context.Context, recursive bool, cond builder.Cond, currentLevel int) error {
|
||||||
if currentLevel >= 20 {
|
if currentLevel >= 20 {
|
||||||
return ErrGroupTooDeep{
|
return ErrGroupTooDeep{
|
||||||
g.ID,
|
g.ID,
|
||||||
@ -55,15 +56,13 @@ func (g *Group) doLoadSubgroups(ctx context.Context, recursive bool, currentLeve
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
g.Subgroups, err = FindGroups(ctx, &FindGroupsOptions{
|
g.Subgroups, err = FindGroupsByCond(ctx, cond, g.ID)
|
||||||
ParentGroupID: g.ID,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if recursive {
|
if recursive {
|
||||||
for _, group := range g.Subgroups {
|
for _, group := range g.Subgroups {
|
||||||
err = group.doLoadSubgroups(ctx, recursive, currentLevel+1)
|
err = group.doLoadSubgroups(ctx, recursive, cond, currentLevel+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -73,8 +72,14 @@ func (g *Group) doLoadSubgroups(ctx context.Context, recursive bool, currentLeve
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) LoadSubgroups(ctx context.Context, recursive bool) error {
|
func (g *Group) LoadSubgroups(ctx context.Context, recursive bool) error {
|
||||||
err := g.doLoadSubgroups(ctx, recursive, 0)
|
fgo := &FindGroupsOptions{
|
||||||
return err
|
ParentGroupID: g.ID,
|
||||||
|
}
|
||||||
|
return g.doLoadSubgroups(ctx, recursive, fgo.ToConds(), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Group) LoadAccessibleSubgroups(ctx context.Context, recursive bool, doer *user_model.User) error {
|
||||||
|
return g.doLoadSubgroups(ctx, recursive, AccessibleGroupCondition(doer, unit.TypeInvalid), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) LoadAttributes(ctx context.Context) error {
|
func (g *Group) LoadAttributes(ctx context.Context) error {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user