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 34650ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 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
12 changes: 10 additions & 2 deletions models/perm/access/repo_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ func TestUnitAccessMode(t *testing.T) {
assert.Equal(t, perm_model.AccessModeOwner, perm.UnitAccessMode(unit.TypeWiki), "only unit no map, use AccessMode")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
AccessMode: perm_model.AccessModeAdmin,
UnitsMode: map[unit.Type]perm_model.AccessMode{
unit.TypeWiki: perm_model.AccessModeRead,
},
}
assert.Equal(t, perm_model.AccessModeAdmin, perm.UnitAccessMode(unit.TypeWiki), "no unit only map, admin overrides map")

perm = Permission{
AccessMode: perm_model.AccessModeNone,
UnitsMode: map[unit.Type]perm_model.AccessMode{
unit.TypeWiki: perm_model.AccessModeRead,
},
}
assert.Equal(t, perm_model.AccessModeRead, perm.UnitAccessMode(unit.TypeWiki), "no unit only map, use map")

perm = Permission{
AccessMode: perm_model.AccessModeOwner,
AccessMode: perm_model.AccessModeNone,
Units: []*repo_model.RepoUnit{
{Type: unit.TypeWiki, EveryoneAccessMode: perm_model.AccessModeWrite},
},
Expand Down

0 comments on commit 34650ad

Please sign in to comment.