Skip to content

Commit ec5677b

Browse files
Simplify CheckUnitUser logic (#12854)
if check one user's unit in different repos, it's not necessary to get user data every time. Signed-off-by: a1012112796 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 07995e2 commit ec5677b

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

models/notification.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,18 @@ func createOrUpdateIssueNotifications(e Engine, issueID, commentID, notification
199199
// notify
200200
for userID := range toNotify {
201201
issue.Repo.Units = nil
202-
if issue.IsPull && !issue.Repo.checkUnitUser(e, userID, false, UnitTypePullRequests) {
202+
user, err := getUserByID(e, userID)
203+
if err != nil {
204+
if IsErrUserNotExist(err) {
205+
continue
206+
}
207+
208+
return err
209+
}
210+
if issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypePullRequests) {
203211
continue
204212
}
205-
if !issue.IsPull && !issue.Repo.checkUnitUser(e, userID, false, UnitTypeIssues) {
213+
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypeIssues) {
206214
continue
207215
}
208216

models/repo.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,17 @@ func (repo *Repository) getUnits(e Engine) (err error) {
425425
}
426426

427427
// CheckUnitUser check whether user could visit the unit of this repository
428-
func (repo *Repository) CheckUnitUser(userID int64, isAdmin bool, unitType UnitType) bool {
429-
return repo.checkUnitUser(x, userID, isAdmin, unitType)
428+
func (repo *Repository) CheckUnitUser(user *User, unitType UnitType) bool {
429+
return repo.checkUnitUser(x, user, unitType)
430430
}
431431

432-
func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unitType UnitType) bool {
433-
if isAdmin {
432+
func (repo *Repository) checkUnitUser(e Engine, user *User, unitType UnitType) bool {
433+
if user.IsAdmin {
434434
return true
435435
}
436-
user, err := getUserByID(e, userID)
437-
if err != nil {
438-
return false
439-
}
440436
perm, err := getUserRepoPermission(e, repo, user)
441437
if err != nil {
438+
log.Error("getUserRepoPermission(): %v", err)
442439
return false
443440
}
444441

routers/home.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,8 @@ func ExploreCode(ctx *context.Context) {
303303
repoIDs []int64
304304
err error
305305
isAdmin bool
306-
userID int64
307306
)
308307
if ctx.User != nil {
309-
userID = ctx.User.ID
310308
isAdmin = ctx.User.IsAdmin
311309
}
312310

@@ -336,7 +334,7 @@ func ExploreCode(ctx *context.Context) {
336334
var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
337335
repoIDs = make([]int64, 0, len(repoMaps))
338336
for id, repo := range repoMaps {
339-
if repo.CheckUnitUser(userID, isAdmin, models.UnitTypeCode) {
337+
if repo.CheckUnitUser(ctx.User, models.UnitTypeCode) {
340338
rightRepoMap[id] = repo
341339
repoIDs = append(repoIDs, id)
342340
}

routers/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func Create(ctx *context.Context) {
140140
templateID := ctx.QueryInt64("template_id")
141141
if templateID > 0 {
142142
templateRepo, err := models.GetRepositoryByID(templateID)
143-
if err == nil && templateRepo.CheckUnitUser(ctxUser.ID, ctxUser.IsAdmin, models.UnitTypeCode) {
143+
if err == nil && templateRepo.CheckUnitUser(ctxUser, models.UnitTypeCode) {
144144
ctx.Data["repo_template"] = templateID
145145
ctx.Data["repo_template_name"] = templateRepo.Name
146146
}

0 commit comments

Comments
 (0)