0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-04-05 20:44:42 +02:00

add helper functions for dealing with group hierarchies

This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2024-12-27 22:03:44 -05:00
parent 2760a0e29f
commit 5bc2e43f96
No known key found for this signature in database
GPG Key ID: 924A5F6AF051E87C

View File

@ -161,6 +161,42 @@ func FindGroupsByCond(ctx context.Context, cond builder.Cond, parentGroupID int6
Find(&groups)
}
// GetParentGroupChain returns a slice containing a group and its ancestors
func GetParentGroupChain(ctx context.Context, groupID int64) (GroupList, error) {
groupList := make([]*Group, 0, 20)
currentGroupID := groupID
for {
if currentGroupID < 1 {
break
}
if len(groupList) >= 20 {
return nil, ErrGroupTooDeep{currentGroupID}
}
currentGroup, err := GetGroupByID(ctx, currentGroupID)
if err != nil {
return nil, err
}
groupList = append(groupList, currentGroup)
currentGroupID = currentGroup.ParentGroupID
}
return groupList, nil
}
// ParentGroupCond returns a condition matching a group and its ancestors
func ParentGroupCond(idStr string, groupID int64) builder.Cond {
groupList, err := GetParentGroupChain(db.DefaultContext, groupID)
if err != nil {
log.Info("Error building group cond: %w", err)
return builder.NotIn(idStr)
}
return builder.In(
idStr,
util.SliceMap[*Group, int64](groupList, func(it *Group) int64 {
return it.ID
}),
)
}
type ErrGroupNotExist struct {
ID int64
}