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

Restrict permission check on repositories and fix some problems #5314

Merged
merged 32 commits into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0bf41c2
fix units permission problems
lunny Nov 10, 2018
15f80b9
fix some bugs and merge LoadUnits to repoAssignment
lunny Nov 11, 2018
40d552c
refactor permission struct and add some copyright heads
lunny Nov 11, 2018
d80fea2
remove unused codes
lunny Nov 11, 2018
fb4a2cb
fix routes units check
lunny Nov 11, 2018
a21bfde
improve permission check
lunny Nov 11, 2018
422ba40
add unit tests for permission
lunny Nov 12, 2018
dae595b
fix typo
lunny Nov 12, 2018
6bed0d4
fix tests
lunny Nov 12, 2018
d5ba3a0
fix some routes
lunny Nov 12, 2018
426980d
fix api permission check
lunny Nov 12, 2018
50d1287
improve permission check
lunny Nov 12, 2018
95d9a58
fix some permission check
lunny Nov 13, 2018
5df61b6
fix tests
lunny Nov 13, 2018
4ee6e1f
fix tests
lunny Nov 13, 2018
de04377
improve some permission check
lunny Nov 13, 2018
66fd8f3
fix some permission check
lunny Nov 14, 2018
11bde94
refactor AccessLevel
lunny Nov 17, 2018
ba60cc8
fix bug
lunny Nov 17, 2018
a978acc
fix tests
lunny Nov 17, 2018
d161315
fix tests
lunny Nov 17, 2018
e5e165c
fix tests
lunny Nov 17, 2018
9253015
fix AccessLevel
lunny Nov 18, 2018
2f65f7a
rename CanAccess
lunny Nov 18, 2018
962be78
fix tests
lunny Nov 18, 2018
9742d63
fix comment
lunny Nov 18, 2018
2db05db
fix bug
lunny Nov 18, 2018
3620109
add missing unit for test repos
lunny Nov 18, 2018
861b3b2
fix bug
lunny Nov 18, 2018
b677d04
rename some functions
lunny Nov 18, 2018
79365d8
fix routes check
lunny Nov 18, 2018
d54bc51
Merge branch 'master' into lunny/fix_units_permissions
lunny Nov 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add unit tests for permission
  • Loading branch information
lunny committed Nov 28, 2018
commit 422ba40b5dcd9cd24f58d931e27480907250555b
86 changes: 85 additions & 1 deletion models/fixtures/repo_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,88 @@
repo_id: 33
type: 5
config: "{}"
created_unix: 1535593231
created_unix: 1535593231

-
id: 17
repo_id: 4
type: 4
config: "{}"
created_unix: 946684810

-
id: 18
repo_id: 4
type: 5
config: "{}"
created_unix: 946684810

-
id: 19
repo_id: 4
type: 1
config: "{}"
created_unix: 946684810

-
id: 20
repo_id: 4
type: 2
config: "{\"EnableTimetracker\":true,\"AllowOnlyContributorsToTrackTime\":true}"
created_unix: 946684810

-
id: 21
repo_id: 4
type: 3
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowSquash\":true}"
created_unix: 946684810

-
id: 22
repo_id: 2
type: 4
config: "{}"
created_unix: 946684810

-
id: 23
repo_id: 2
type: 5
config: "{}"
created_unix: 946684810

-
id: 24
repo_id: 2
type: 1
config: "{}"
created_unix: 946684810

-
id: 25
repo_id: 32
type: 1
config: "{}"
created_unix: 1524304355

-
id: 26
repo_id: 32
type: 2
config: "{}"
created_unix: 1524304355

-
id: 27
repo_id: 24
type: 1
config: "{}"
created_unix: 1524304355

-
id: 28
repo_id: 24
type: 2
config: "{}"
created_unix: 1524304355
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
is_mirror: false

-
id: 32
id: 32 # org public repo
owner_id: 3
lower_name: repo21
name: repo21
Expand Down
27 changes: 27 additions & 0 deletions models/fixtures/team.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,30 @@
authorize: 4 # owner
num_repos: 2
num_members: 1

-
id: 7
org_id: 3
lower_name: test_team
name: test_team
authorize: 2 # write
num_repos: 1
num_members: 1

-
id: 8
org_id: 17
lower_name: test_team
name: test_team
authorize: 2 # write
num_repos: 1
num_members: 1

-
id: 9
org_id: 17
lower_name: review_team
name: review_team
authorize: 1 # read
num_repos: 1
num_members: 1
18 changes: 18 additions & 0 deletions models/fixtures/team_repo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@
org_id: 3
team_id: 1
repo_id: 32

-
id: 9
org_id: 3
team_id: 7
repo_id: 32

-
id: 10
org_id: 17
team_id: 8
repo_id: 24

-
id: 11
org_id: 17
team_id: 9
repo_id: 24
15 changes: 15 additions & 0 deletions models/fixtures/team_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,18 @@
id: 42
team_id: 6
type: 7

-
id: 43
team_id: 7
type: 2 # issues

-
id: 44
team_id: 8
type: 2 # issues

-
id: 45
team_id: 9
type: 1 # code
18 changes: 18 additions & 0 deletions models/fixtures/team_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@
id: 8
org_id: 19
team_id: 6
uid: 20

-
id: 9
org_id: 3
team_id: 7
uid: 15

-
id: 10
org_id: 17
team_id: 8
uid: 2

-
id: 11
org_id: 17
team_id: 9
uid: 20
52 changes: 5 additions & 47 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,58 +325,16 @@ func (repo *Repository) CheckUnitUser(userID int64, isAdmin bool, unitType UnitT
}

func (repo *Repository) checkUnitUser(e Engine, userID int64, isAdmin bool, unitType UnitType) bool {
if err := repo.getUnitsByUserID(e, userID, isAdmin); err != nil {
user, err := getUserByID(e, userID)
if err != nil {
return false
}

for _, unit := range repo.Units {
if unit.Type == unitType {
return true
}
}
return false
}

func (repo *Repository) getUnitsByUserID(e Engine, userID int64, isAdmin bool) (err error) {
if repo.Units != nil {
return nil
}

if err = repo.getUnits(e); err != nil {
return err
} else if err = repo.getOwner(e); err != nil {
return err
}

if !repo.Owner.IsOrganization() || userID == 0 || isAdmin || !repo.IsPrivate {
return nil
}

// Collaborators will not be limited
if isCollaborator, err := repo.isCollaborator(e, userID); err != nil {
return err
} else if isCollaborator {
return nil
}

teams, err := getUserRepoTeams(e, repo.OwnerID, userID, repo.ID)
perm, err := getUserRepoPermission(e, repo, user)
if err != nil {
return err
}

// unique
var newRepoUnits = make([]*RepoUnit, 0, len(repo.Units))
for _, u := range repo.Units {
for _, team := range teams {
if team.unitEnabled(e, u.Type) {
newRepoUnits = append(newRepoUnits, u)
break
}
}
return false
}

repo.Units = newRepoUnits
return nil
return perm.CanAccess(unitType)
}

// UnitEnabled if this repository has the given unit enabled
Expand Down
28 changes: 23 additions & 5 deletions models/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func (p *Permission) CanAccess(unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= AccessModeRead
}

// CanAccessAny returns true if user has read access to any of the units of the repository
func (p *Permission) CanAccessAny(unitTypes ...UnitType) bool {
for _, u := range unitTypes {
if p.CanAccess(u) {
return true
}
}
return false
}

// CanWrite returns true if user could write to this unit
func (p *Permission) CanWrite(unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= AccessModeWrite
Expand Down Expand Up @@ -105,19 +115,23 @@ func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permiss
return
}

// Collaborators on organization repos will not be limited by teams units
perm.UnitsMode = make(map[UnitType]AccessMode)

// Collaborators on organization
if isCollaborator, err := repo.isCollaborator(e, user.ID); err != nil {
return perm, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the other returns rely on the default named return values.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this err is a local variable not that err return variable.

} else if isCollaborator {
return perm, nil
for _, u := range repo.Units {
perm.UnitsMode[u.Type] = perm.AccessMode
}
}

// get units mode from teams
teams, err := getUserRepoTeams(e, repo.OwnerID, user.ID, repo.ID)
if err != nil {
return
}

perm.UnitsMode = make(map[UnitType]AccessMode)
for _, u := range repo.Units {
var found bool
for _, team := range teams {
Expand All @@ -130,13 +144,17 @@ func getUserRepoPermission(e Engine, repo *Repository, user *User) (perm Permiss
}
}

// for a public repo on an organization, user have read permission on non-team defined units.
if !found && !repo.IsPrivate {
perm.UnitsMode[u.Type] = AccessModeRead
if _, ok := perm.UnitsMode[u.Type]; !ok {
perm.UnitsMode[u.Type] = AccessModeRead
}
}
}

// remove no permission units
perm.Units = make([]*RepoUnit, 0, len(repo.Units))
for t, _ := range perm.UnitsMode {
for t := range perm.UnitsMode {
for _, u := range repo.Units {
if u.Type == t {
perm.Units = append(perm.Units, u)
Expand Down
Loading