@@ -114,53 +114,91 @@ type SearchRepoOptions struct {
114
114
115
115
// SearchRepositoryByName takes keyword and part of repository name to search,
116
116
// it returns results in given range and number of total results.
117
- func SearchRepositoryByName (opts * SearchRepoOptions ) (repos RepositoryList , count int64 , err error ) {
118
- var cond = builder .NewCond ()
117
+ func SearchRepositoryByName (opts * SearchRepoOptions ) (repos RepositoryList , _ int64 , _ error ) {
118
+ // Set owner of searched repositories if present in options
119
+ searchOwnerID := opts .OwnerID
120
+
121
+ // Set owner ID to Searcher ID if Owner is not filled
122
+ if opts .OwnerID <= 0 && opts .Searcher != nil {
123
+ searchOwnerID = opts .Searcher .ID
124
+ }
125
+ // Check if user with Owner ID exists
126
+ if searchOwnerID > 0 {
127
+ userExists , err := GetUser (& User {ID : searchOwnerID })
128
+ if err != nil {
129
+ return nil , 0 , err
130
+ }
131
+ if userExists == false {
132
+ return nil , 0 , ErrUserNotExist {UID : searchOwnerID }
133
+ }
134
+ }
135
+
136
+ // Set public search by default
137
+ isPublicSearch := true
138
+
139
+ // Allow to search for owners private data
140
+ if opts .Searcher != nil && (opts .Searcher .ID == searchOwnerID || opts .Searcher .IsAdmin ) {
141
+ isPublicSearch = false
142
+ }
143
+
144
+ // Check and set page to correct number
119
145
if opts .Page <= 0 {
120
146
opts .Page = 1
121
147
}
122
148
123
- var starJoin bool
124
- if opts .Starred && opts .OwnerID > 0 {
149
+ var cond = builder .NewCond ()
150
+
151
+ // Include starred repositories by Owner
152
+ if opts .Starred && searchOwnerID > 0 {
125
153
cond = builder.Eq {
126
- "star.uid" : opts . OwnerID ,
154
+ "star.uid" : searchOwnerID ,
127
155
}
128
- starJoin = true
129
156
}
130
157
131
- opts . Keyword = strings . ToLower ( opts . Keyword )
158
+ // Add repository name keyword to search for
132
159
if opts .Keyword != "" {
160
+ opts .Keyword = strings .ToLower (opts .Keyword )
133
161
cond = cond .And (builder.Like {"lower_name" , opts .Keyword })
134
162
}
135
163
136
- // Append conditions
137
- if ! opts .Starred && opts .OwnerID > 0 {
138
- var searcherReposCond builder.Cond = builder.Eq {"owner_id" : opts .OwnerID }
139
- if opts .Searcher != nil {
140
- var ownerIds []int64
164
+ // Exclude private repositories
165
+ // if it is set in options
166
+ // or is public search
167
+ if ! opts .Private || isPublicSearch {
168
+ cond = cond .And (builder.Eq {"is_private" : false })
169
+ }
170
+
171
+ if searchOwnerID > 0 {
172
+ // Set user access conditions
173
+ var accessCond = builder .NewCond ()
141
174
142
- ownerIds = append (ownerIds , opts .Searcher .ID )
143
- err = opts .Searcher .GetOrganizations (true )
175
+ // Add Owner ID to access conditions
176
+ accessCond = builder.Eq {"owner_id" : searchOwnerID }
177
+
178
+ // Include collaborative repositories
179
+ if opts .Collaborate {
180
+ // Get owner organizations
181
+ orgs , err := GetOrgUsersByUserID (searchOwnerID , ! isPublicSearch )
144
182
145
183
if err != nil {
146
184
return nil , 0 , fmt .Errorf ("Organization: %v" , err )
147
185
}
148
186
149
- for _ , org := range opts .Searcher .Orgs {
150
- ownerIds = append (ownerIds , org .ID )
187
+ var ownerIds []int64
188
+ for _ , org := range orgs {
189
+ ownerIds = append (ownerIds , org .OrgID )
151
190
}
152
191
153
- searcherReposCond = searcherReposCond .Or (builder .In ("owner_id" , ownerIds ))
154
- if opts .Collaborate {
155
- searcherReposCond = searcherReposCond .Or (builder .Expr ("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ? AND owner_id != ?)" ,
156
- opts .Searcher .ID , opts .Searcher .ID ))
157
- }
192
+ // Add repositories from related organizations
193
+ accessCond = accessCond .Or (builder .In ("owner_id" , ownerIds ))
194
+
195
+ // Add repositories where user is set as collaborator directly
196
+ accessCond = accessCond .Or (builder .Expr ("id IN (SELECT repo_id FROM `access` WHERE access.user_id = ? AND owner_id != ?)" ,
197
+ searchOwnerID , searchOwnerID ))
158
198
}
159
- cond = cond .And (searcherReposCond )
160
- }
161
199
162
- if ! opts . Private {
163
- cond = cond .And (builder. Eq { "is_private" : false } )
200
+ // Add user access conditions to search
201
+ cond = cond .And (accessCond )
164
202
}
165
203
166
204
if len (opts .OrderBy ) == 0 {
@@ -170,23 +208,15 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
170
208
sess := x .NewSession ()
171
209
defer sess .Close ()
172
210
173
- if starJoin {
174
- count , err = sess .
175
- Join ("INNER" , "star" , "star.repo_id = repository.id" ).
176
- Where (cond ).
177
- Count (new (Repository ))
178
- if err != nil {
179
- return nil , 0 , fmt .Errorf ("Count: %v" , err )
180
- }
211
+ if opts .Starred {
212
+ sess = sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
213
+ }
181
214
182
- sess .Join ("INNER" , "star" , "star.repo_id = repository.id" )
183
- } else {
184
- count , err = sess .
185
- Where (cond ).
186
- Count (new (Repository ))
187
- if err != nil {
188
- return nil , 0 , fmt .Errorf ("Count: %v" , err )
189
- }
215
+ count , err := sess .
216
+ Where (cond ).
217
+ Count (new (Repository ))
218
+ if err != nil {
219
+ return nil , 0 , fmt .Errorf ("Count: %v" , err )
190
220
}
191
221
192
222
repos = make ([]* Repository , 0 , opts .PageSize )
@@ -204,7 +234,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
204
234
}
205
235
}
206
236
207
- return
237
+ return repos , count , nil
208
238
}
209
239
210
240
// Repositories returns all repositories
0 commit comments