Skip to content

Commit

Permalink
always check map length instead of nil
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Apr 15, 2024
1 parent 8ba2d63 commit 69396b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion models/migrations/v1_11/v111.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func AddBranchProtectionCanPushAndEnableWhitelist(x *xorm.Engine) error {
if err != nil {
return false, err
}
if perm.UnitsMode == nil {
if len(perm.UnitsMode) == 0 {
for _, u := range perm.Units {
if u.Type == UnitTypeCode {
return AccessModeWrite <= perm.AccessMode, nil
Expand Down
27 changes: 11 additions & 16 deletions models/perm/access/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
type Permission struct {
AccessMode perm_model.AccessMode
Units []*repo_model.RepoUnit
UnitsMode map[unit.Type]perm_model.AccessMode
UnitsMode map[unit.Type]perm_model.AccessMode // zero length means use the AccessMode above
}

// IsOwner returns true if current user is the owner of repository.
Expand All @@ -33,17 +33,14 @@ func (p *Permission) IsAdmin() bool {
return p.AccessMode >= perm_model.AccessModeAdmin
}

// HasAccess returns true if the current user has at least read access to any unit of this repository
// HasAccess returns true if the current user might have at least read access to any unit of this repository
func (p *Permission) HasAccess() bool {
if p.UnitsMode == nil {
return p.AccessMode >= perm_model.AccessModeRead
}
return len(p.UnitsMode) > 0
return len(p.UnitsMode) > 0 || p.AccessMode >= perm_model.AccessModeRead
}

// UnitAccessMode returns current user accessmode to the specify unit of the repository
// UnitAccessMode returns current user access mode to the specify unit of the repository
func (p *Permission) UnitAccessMode(unitType unit.Type) perm_model.AccessMode {
if p.UnitsMode == nil {
if len(p.UnitsMode) == 0 {
for _, u := range p.Units {
if u.Type == unitType {
return p.AccessMode
Expand Down Expand Up @@ -145,6 +142,12 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
}
}()

if err := repo.LoadUnits(ctx); err != nil {
return perm, err
}
perm.Units = repo.Units
perm.UnitsMode = make(map[unit.Type]perm_model.AccessMode)

// anonymous user visit private repo.
// TODO: anonymous user visit public unit of private repo???
if user == nil && repo.IsPrivate {
Expand All @@ -171,12 +174,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
return perm, nil
}

if err := repo.LoadUnits(ctx); err != nil {
return perm, err
}

perm.Units = repo.Units

// anonymous visit public repo
if user == nil {
perm.AccessMode = perm_model.AccessModeRead
Expand All @@ -202,8 +199,6 @@ func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, use
return perm, nil
}

perm.UnitsMode = make(map[unit.Type]perm_model.AccessMode)

// Collaborators on organization
if isCollaborator {
for _, u := range repo.Units {
Expand Down
2 changes: 1 addition & 1 deletion services/convert/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func ToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo a
func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInRepo access_model.Permission, isParent bool) *api.Repository {
var parent *api.Repository

if permissionInRepo.Units == nil && permissionInRepo.UnitsMode == nil {
if len(permissionInRepo.Units) == 0 && len(permissionInRepo.UnitsMode) == 0 {
// If Units and UnitsMode are both nil, it means that it's a hard coded permission,
// like access_model.Permission{AccessMode: perm.AccessModeAdmin}.
// So we need to load units for the repo, or UnitAccessMode will always return perm.AccessModeNone.
Expand Down

0 comments on commit 69396b5

Please sign in to comment.