From ae4c13529cc8a2ffdf14e11496388d65e83a14fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Thu, 9 Jan 2025 17:01:44 -0500 Subject: [PATCH] update group_unit.go - export `GetUnitsByGroupID` - add `GetGroupUnit` function to retrieve a specific unit in a group - add `GetMaxGroupUnit` function that returns a specific type of group unit with the highest permissions granted --- models/group/group_unit.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/models/group/group_unit.go b/models/group/group_unit.go index 89b3c131cf..30c968b978 100644 --- a/models/group/group_unit.go +++ b/models/group/group_unit.go @@ -10,7 +10,7 @@ import ( // GroupUnit describes all units of a repository group type GroupUnit struct { ID int64 `xorm:"pk autoincr"` - GroupID int64 `xorm:"INDEX"` + GroupID int64 `xorm:"UNIQUE(s)"` TeamID int64 `xorm:"UNIQUE(s)"` Type unit.Type `xorm:"UNIQUE(s)"` AccessMode perm.AccessMode @@ -20,6 +20,33 @@ func (g *GroupUnit) Unit() unit.Unit { return unit.Units[g.Type] } -func getUnitsByGroupID(ctx context.Context, groupID int64) (units []*GroupUnit, err error) { +func GetUnitsByGroupID(ctx context.Context, groupID int64) (units []*GroupUnit, err error) { return units, db.GetEngine(ctx).Where("group_id = ?", groupID).Find(&units) } + +func GetGroupUnit(ctx context.Context, groupID, teamID int64, unitType unit.Type) (unit *GroupUnit, err error) { + unit = new(GroupUnit) + _, err = db.GetEngine(ctx). + Where("group_id = ?", groupID). + And("team_id = ?", teamID). + And("type = ?", unitType). + Get(unit) + return +} + +func GetMaxGroupUnit(ctx context.Context, groupID int64, unitType unit.Type) (unit *GroupUnit, err error) { + units := make([]*GroupUnit, 0) + err = db.GetEngine(ctx). + Where("group_id = ?", groupID). + And("type = ?", unitType). + Find(&units) + if err != nil { + return + } + for _, u := range units { + if unit == nil || u.AccessMode > unit.AccessMode { + unit = u + } + } + return +}