|  | 
|  | 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. | 
|  | 2 | +// SPDX-License-Identifier: MIT | 
|  | 3 | + | 
|  | 4 | +package organization | 
|  | 5 | + | 
|  | 6 | +import ( | 
|  | 7 | +	"context" | 
|  | 8 | +	"fmt" | 
|  | 9 | +	"strings" | 
|  | 10 | + | 
|  | 11 | +	"code.gitea.io/gitea/models/db" | 
|  | 12 | +	"code.gitea.io/gitea/models/perm" | 
|  | 13 | +	user_model "code.gitea.io/gitea/models/user" | 
|  | 14 | +	"code.gitea.io/gitea/modules/structs" | 
|  | 15 | + | 
|  | 16 | +	"xorm.io/builder" | 
|  | 17 | +) | 
|  | 18 | + | 
|  | 19 | +// SearchOrganizationsOptions options to filter organizations | 
|  | 20 | +type SearchOrganizationsOptions struct { | 
|  | 21 | +	db.ListOptions | 
|  | 22 | +	All bool | 
|  | 23 | +} | 
|  | 24 | + | 
|  | 25 | +// FindOrgOptions finds orgs options | 
|  | 26 | +type FindOrgOptions struct { | 
|  | 27 | +	db.ListOptions | 
|  | 28 | +	UserID         int64 | 
|  | 29 | +	IncludePrivate bool | 
|  | 30 | +} | 
|  | 31 | + | 
|  | 32 | +func queryUserOrgIDs(userID int64, includePrivate bool) *builder.Builder { | 
|  | 33 | +	cond := builder.Eq{"uid": userID} | 
|  | 34 | +	if !includePrivate { | 
|  | 35 | +		cond["is_public"] = true | 
|  | 36 | +	} | 
|  | 37 | +	return builder.Select("org_id").From("org_user").Where(cond) | 
|  | 38 | +} | 
|  | 39 | + | 
|  | 40 | +func (opts FindOrgOptions) ToConds() builder.Cond { | 
|  | 41 | +	var cond builder.Cond = builder.Eq{"`user`.`type`": user_model.UserTypeOrganization} | 
|  | 42 | +	if opts.UserID > 0 { | 
|  | 43 | +		cond = cond.And(builder.In("`user`.`id`", queryUserOrgIDs(opts.UserID, opts.IncludePrivate))) | 
|  | 44 | +	} | 
|  | 45 | +	if !opts.IncludePrivate { | 
|  | 46 | +		cond = cond.And(builder.Eq{"`user`.visibility": structs.VisibleTypePublic}) | 
|  | 47 | +	} | 
|  | 48 | +	return cond | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +func (opts FindOrgOptions) ToOrders() string { | 
|  | 52 | +	return "`user`.lower_name ASC" | 
|  | 53 | +} | 
|  | 54 | + | 
|  | 55 | +// GetOrgsCanCreateRepoByUserID returns a list of organizations where given user ID | 
|  | 56 | +// are allowed to create repos. | 
|  | 57 | +func GetOrgsCanCreateRepoByUserID(ctx context.Context, userID int64) ([]*Organization, error) { | 
|  | 58 | +	orgs := make([]*Organization, 0, 10) | 
|  | 59 | + | 
|  | 60 | +	return orgs, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`user`.id").From("`user`"). | 
|  | 61 | +		Join("INNER", "`team_user`", "`team_user`.org_id = `user`.id"). | 
|  | 62 | +		Join("INNER", "`team`", "`team`.id = `team_user`.team_id"). | 
|  | 63 | +		Where(builder.Eq{"`team_user`.uid": userID}). | 
|  | 64 | +		And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})))). | 
|  | 65 | +		Asc("`user`.name"). | 
|  | 66 | +		Find(&orgs) | 
|  | 67 | +} | 
|  | 68 | + | 
|  | 69 | +// MinimalOrg represents a simple organization with only the needed columns | 
|  | 70 | +type MinimalOrg = Organization | 
|  | 71 | + | 
|  | 72 | +// GetUserOrgsList returns all organizations the given user has access to | 
|  | 73 | +func GetUserOrgsList(ctx context.Context, user *user_model.User) ([]*MinimalOrg, error) { | 
|  | 74 | +	schema, err := db.TableInfo(new(user_model.User)) | 
|  | 75 | +	if err != nil { | 
|  | 76 | +		return nil, err | 
|  | 77 | +	} | 
|  | 78 | + | 
|  | 79 | +	outputCols := []string{ | 
|  | 80 | +		"id", | 
|  | 81 | +		"name", | 
|  | 82 | +		"full_name", | 
|  | 83 | +		"visibility", | 
|  | 84 | +		"avatar", | 
|  | 85 | +		"avatar_email", | 
|  | 86 | +		"use_custom_avatar", | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	selectColumns := &strings.Builder{} | 
|  | 90 | +	for i, col := range outputCols { | 
|  | 91 | +		fmt.Fprintf(selectColumns, "`%s`.%s", schema.Name, col) | 
|  | 92 | +		if i < len(outputCols)-1 { | 
|  | 93 | +			selectColumns.WriteString(", ") | 
|  | 94 | +		} | 
|  | 95 | +	} | 
|  | 96 | +	columnsStr := selectColumns.String() | 
|  | 97 | + | 
|  | 98 | +	var orgs []*MinimalOrg | 
|  | 99 | +	if err := db.GetEngine(ctx).Select(columnsStr). | 
|  | 100 | +		Table("user"). | 
|  | 101 | +		Where(builder.In("`user`.`id`", queryUserOrgIDs(user.ID, true))). | 
|  | 102 | +		Find(&orgs); err != nil { | 
|  | 103 | +		return nil, err | 
|  | 104 | +	} | 
|  | 105 | + | 
|  | 106 | +	type orgCount struct { | 
|  | 107 | +		OrgID     int64 | 
|  | 108 | +		RepoCount int | 
|  | 109 | +	} | 
|  | 110 | +	var orgCounts []orgCount | 
|  | 111 | +	if err := db.GetEngine(ctx). | 
|  | 112 | +		Select("owner_id AS org_id, COUNT(DISTINCT(repository.id)) as repo_count"). | 
|  | 113 | +		Table("repository"). | 
|  | 114 | +		Join("INNER", "org_user", "owner_id = org_user.org_id"). | 
|  | 115 | +		Where("org_user.uid = ?", user.ID). | 
|  | 116 | +		And(builder.Or( | 
|  | 117 | +			builder.Eq{"repository.is_private": false}, | 
|  | 118 | +			builder.In("repository.id", builder.Select("repo_id").From("team_repo"). | 
|  | 119 | +				InnerJoin("team_user", "team_user.team_id = team_repo.team_id"). | 
|  | 120 | +				Where(builder.Eq{"team_user.uid": user.ID})), | 
|  | 121 | +			builder.In("repository.id", builder.Select("repo_id").From("collaboration"). | 
|  | 122 | +				Where(builder.Eq{"user_id": user.ID})), | 
|  | 123 | +		)). | 
|  | 124 | +		GroupBy("owner_id").Find(&orgCounts); err != nil { | 
|  | 125 | +		return nil, err | 
|  | 126 | +	} | 
|  | 127 | + | 
|  | 128 | +	orgCountMap := make(map[int64]int, len(orgCounts)) | 
|  | 129 | +	for _, orgCount := range orgCounts { | 
|  | 130 | +		orgCountMap[orgCount.OrgID] = orgCount.RepoCount | 
|  | 131 | +	} | 
|  | 132 | + | 
|  | 133 | +	for _, org := range orgs { | 
|  | 134 | +		org.NumRepos = orgCountMap[org.ID] | 
|  | 135 | +	} | 
|  | 136 | + | 
|  | 137 | +	return orgs, nil | 
|  | 138 | +} | 
0 commit comments