Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify CheckUnitUser logic #12854

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,18 @@ func createOrUpdateIssueNotifications(e Engine, issueID, commentID, notification
// notify
for userID := range toNotify {
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
}
if !issue.IsPull && !issue.Repo.checkUnitUser(e, userID, false, UnitTypeIssues) {
if !issue.IsPull && !issue.Repo.checkUnitUser(e, user, UnitTypeIssues) {
continue
}

Expand Down
13 changes: 5 additions & 8 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,20 +425,17 @@ func (repo *Repository) getUnits(e Engine) (err error) {
}

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

func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unitType UnitType) bool {
if isAdmin {
func (repo *Repository) checkUnitUser(e Engine, user *User, unitType UnitType) bool {
if user.IsAdmin {
return true
}
user, err := getUserByID(e, userID)
if err != nil {
return false
}
perm, err := getUserRepoPermission(e, repo, user)
if err != nil {
log.Error("getUserRepoPermission(): %v", err)
return false
}

Expand Down
4 changes: 1 addition & 3 deletions routers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,8 @@ func ExploreCode(ctx *context.Context) {
repoIDs []int64
err error
isAdmin bool
userID int64
)
if ctx.User != nil {
userID = ctx.User.ID
isAdmin = ctx.User.IsAdmin
}

Expand Down Expand Up @@ -336,7 +334,7 @@ func ExploreCode(ctx *context.Context) {
var rightRepoMap = make(map[int64]*models.Repository, len(repoMaps))
repoIDs = make([]int64, 0, len(repoMaps))
for id, repo := range repoMaps {
if repo.CheckUnitUser(userID, isAdmin, models.UnitTypeCode) {
if repo.CheckUnitUser(ctx.User, models.UnitTypeCode) {
rightRepoMap[id] = repo
repoIDs = append(repoIDs, id)
}
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func Create(ctx *context.Context) {
templateID := ctx.QueryInt64("template_id")
if templateID > 0 {
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_name"] = templateRepo.Name
}
Expand Down