mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	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 <1012112796@qq.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		@@ -199,10 +199,18 @@ func createOrUpdateIssueNotifications(e Engine, issueID, commentID, notification
 | 
				
			|||||||
	// notify
 | 
						// notify
 | 
				
			||||||
	for userID := range toNotify {
 | 
						for userID := range toNotify {
 | 
				
			||||||
		issue.Repo.Units = nil
 | 
							issue.Repo.Units = nil
 | 
				
			||||||
		if issue.IsPull && !issue.Repo.checkUnitUser(e, userID, false, UnitTypePullRequests) {
 | 
							user, err := getUserByID(e, userID)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								if IsErrUserNotExist(err) {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypePullRequests) {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if !issue.IsPull && !issue.Repo.checkUnitUser(e, userID, false, UnitTypeIssues) {
 | 
							if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypeIssues) {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -425,20 +425,17 @@ func (repo *Repository) getUnits(e Engine) (err error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// CheckUnitUser check whether user could visit the unit of this repository
 | 
					// CheckUnitUser check whether user could visit the unit of this repository
 | 
				
			||||||
func (repo *Repository) CheckUnitUser(userID int64, isAdmin bool, unitType UnitType) bool {
 | 
					func (repo *Repository) CheckUnitUser(user *User, unitType UnitType) bool {
 | 
				
			||||||
	return repo.checkUnitUser(x, userID, isAdmin, unitType)
 | 
						return repo.checkUnitUser(x, user, unitType)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unitType UnitType) bool {
 | 
					func (repo *Repository) checkUnitUser(e Engine, user *User, unitType UnitType) bool {
 | 
				
			||||||
	if isAdmin {
 | 
						if user.IsAdmin {
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	user, err := getUserByID(e, userID)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return false
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	perm, err := getUserRepoPermission(e, repo, user)
 | 
						perm, err := getUserRepoPermission(e, repo, user)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Error("getUserRepoPermission(): %v", err)
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -303,10 +303,8 @@ func ExploreCode(ctx *context.Context) {
 | 
				
			|||||||
		repoIDs []int64
 | 
							repoIDs []int64
 | 
				
			||||||
		err     error
 | 
							err     error
 | 
				
			||||||
		isAdmin bool
 | 
							isAdmin bool
 | 
				
			||||||
		userID  int64
 | 
					 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	if ctx.User != nil {
 | 
						if ctx.User != nil {
 | 
				
			||||||
		userID = ctx.User.ID
 | 
					 | 
				
			||||||
		isAdmin = ctx.User.IsAdmin
 | 
							isAdmin = ctx.User.IsAdmin
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -336,7 +334,7 @@ func ExploreCode(ctx *context.Context) {
 | 
				
			|||||||
		var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
 | 
							var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
 | 
				
			||||||
		repoIDs = make([]int64, 0, len(repoMaps))
 | 
							repoIDs = make([]int64, 0, len(repoMaps))
 | 
				
			||||||
		for id, repo := range repoMaps {
 | 
							for id, repo := range repoMaps {
 | 
				
			||||||
			if repo.CheckUnitUser(userID, isAdmin, models.UnitTypeCode) {
 | 
								if repo.CheckUnitUser(ctx.User, models.UnitTypeCode) {
 | 
				
			||||||
				rightRepoMap[id] = repo
 | 
									rightRepoMap[id] = repo
 | 
				
			||||||
				repoIDs = append(repoIDs, id)
 | 
									repoIDs = append(repoIDs, id)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -140,7 +140,7 @@ func Create(ctx *context.Context) {
 | 
				
			|||||||
	templateID := ctx.QueryInt64("template_id")
 | 
						templateID := ctx.QueryInt64("template_id")
 | 
				
			||||||
	if templateID > 0 {
 | 
						if templateID > 0 {
 | 
				
			||||||
		templateRepo, err := models.GetRepositoryByID(templateID)
 | 
							templateRepo, err := models.GetRepositoryByID(templateID)
 | 
				
			||||||
		if err == nil && templateRepo.CheckUnitUser(ctxUser.ID, ctxUser.IsAdmin, models.UnitTypeCode) {
 | 
							if err == nil && templateRepo.CheckUnitUser(ctxUser, models.UnitTypeCode) {
 | 
				
			||||||
			ctx.Data["repo_template"] = templateID
 | 
								ctx.Data["repo_template"] = templateID
 | 
				
			||||||
			ctx.Data["repo_template_name"] = templateRepo.Name
 | 
								ctx.Data["repo_template_name"] = templateRepo.Name
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user