Skip to content

Commit

Permalink
fix permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 15, 2024
1 parent 5a4b286 commit c4c6a00
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 10 additions & 7 deletions models/perm/access/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

// Permission contains all the permissions related variables to a repository for a user
Expand All @@ -40,15 +41,17 @@ func (p *Permission) HasAccess() bool {

// UnitAccessMode returns current user access mode to the specify unit of the repository
func (p *Permission) UnitAccessMode(unitType unit.Type) perm_model.AccessMode {
if len(p.UnitsMode) == 0 {
for _, u := range p.Units {
if u.Type == unitType {
return p.AccessMode
}
// if the units map contains the access mode, use it, but admin/owner mode could override it
if m, ok := p.UnitsMode[unitType]; ok {
return util.Iif(p.AccessMode >= perm_model.AccessModeAdmin, p.AccessMode, m)
}
// if the units map does not contain the access mode, return the default access mode if the unit exists
for _, u := range p.Units {
if u.Type == unitType {
return p.AccessMode
}
return perm_model.AccessModeNone
}
return p.UnitsMode[unitType]
return perm_model.AccessModeNone
}

// CanAccess returns true if user has mode access to the unit of the repository
Expand Down
2 changes: 1 addition & 1 deletion models/perm/access/repo_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestUnitAccessMode(t *testing.T) {
unit.TypeWiki: perm_model.AccessModeRead,
},
}
assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeWiki), "no unit only map, use map")
assert.Equal(t, perm_model.AccessModeOwner, perm.UnitAccessMode(unit.TypeWiki), "no unit only map, use map")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
Expand Down

0 comments on commit c4c6a00

Please sign in to comment.