|
| 1 | +package group |
| 2 | + |
| 3 | +import ( |
| 4 | + "code.gitea.io/gitea/models/db" |
| 5 | + group_model "code.gitea.io/gitea/models/group" |
| 6 | + organization_model "code.gitea.io/gitea/models/organization" |
| 7 | + user_model "code.gitea.io/gitea/models/user" |
| 8 | + "context" |
| 9 | + "xorm.io/builder" |
| 10 | +) |
| 11 | + |
| 12 | +// FindGroupMembers finds all users who have access to a group via team membership |
| 13 | +func FindGroupMembers(ctx context.Context, groupID int64, opts *organization_model.FindOrgMembersOpts) (user_model.UserList, error) { |
| 14 | + cond := builder. |
| 15 | + Select("`team_user`.uid"). |
| 16 | + From("team_user"). |
| 17 | + InnerJoin("org_user", "`org_user`.uid = team_user.uid"). |
| 18 | + InnerJoin("group_team", "`group_team`.team_id = team_user.team_id"). |
| 19 | + Where(builder.Eq{"`org_user`.org_id": opts.OrgID}). |
| 20 | + And(group_model.ParentGroupCond(context.TODO(), "`group_team`.group_id", groupID)) |
| 21 | + if opts.PublicOnly() { |
| 22 | + cond = cond.And(builder.Eq{"`org_user`.is_public": true}) |
| 23 | + } |
| 24 | + sess := db.GetEngine(ctx).Where(builder.In("`user`.id", cond)) |
| 25 | + if opts.ListOptions.PageSize > 0 { |
| 26 | + sess = db.SetSessionPagination(sess, opts) |
| 27 | + users := make([]*user_model.User, 0, opts.ListOptions.PageSize) |
| 28 | + return users, sess.Find(&users) |
| 29 | + } |
| 30 | + |
| 31 | + var users []*user_model.User |
| 32 | + err := sess.Find(&users) |
| 33 | + return users, err |
| 34 | +} |
| 35 | + |
| 36 | +func GetGroupTeams(ctx context.Context, groupID int64) (teams []*organization_model.Team, err error) { |
| 37 | + err = db.GetEngine(ctx). |
| 38 | + Where("`group_team`.group_id = ?", groupID). |
| 39 | + Join("INNER", "group_team", "`group_team`.team_id = `team`.id"). |
| 40 | + Asc("`team`.name"). |
| 41 | + Find(&teams) |
| 42 | + return |
| 43 | +} |
| 44 | + |
| 45 | +func IsGroupMember(ctx context.Context, groupID, userID int64) (bool, error) { |
| 46 | + return db.GetEngine(ctx). |
| 47 | + Where("`group_team`.group_id = ?", groupID). |
| 48 | + Join("INNER", "group_team", "`group_team`.team_id = `team_user`.team_id"). |
| 49 | + And("`team_user`.uid = ?", userID). |
| 50 | + Table("team_user"). |
| 51 | + Exist() |
| 52 | +} |
0 commit comments