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

Conversation

lunny
Copy link
Member

@lunny lunny commented Nov 11, 2018

This PR will refactor permission check of repositories, especially units permission check. It will restrict permission check on organization repositories and fix some bugs. It creates a new struct Permission and a function to retrieve permission from User and Repository.

You can know all the permission information from Permission and it has been saved on context.Context when middleware repoAssignment was called. After this PR, we removed some functions like IsRepositoryWriter because we don't know that if the repo is on an organization and there are some teams give permission to the user. Instead it provides CanAccess(unit) and CanWrite(unit) to determine if the User could read or write to code, issues, releases or other units.

This also give a break change about a private repository on an organization. When a user on a team has write permission to a repository but he also is a collaborator with read right to that repository. Before this PR, the team settings will be ignored, but in this PR, the higher permission will be given to the user. Don't know it's a bug or a break change. I added the breaking label.

@lunny lunny added type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality. labels Nov 11, 2018
@lunny lunny added this to the 1.7.0 milestone Nov 11, 2018
Copy link
Member

@techknowlogick techknowlogick left a comment

Choose a reason for hiding this comment

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

I didn't check all files, but Gitea copyright needs to be added in some files.

routers/api/v1/repo/collaborators.go Show resolved Hide resolved
routers/api/v1/api.go Show resolved Hide resolved
routers/api/v1/repo/file.go Show resolved Hide resolved
routers/api/v1/repo/issue_label.go Show resolved Hide resolved
routers/api/v1/repo/label.go Outdated Show resolved Hide resolved
@bkcsoft bkcsoft added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 11, 2018
}

perm.Units = make([]*RepoUnit, 0, len(repo.Units))
for t, _ := range perm.UnitsMode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Lint doesn't like the second argument

Copy link
Member Author

Choose a reason for hiding this comment

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

done.


// Collaborators on organization repos will not be limited by teams units
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.

if isCollaborator, err := repo.isCollaborator(e, user.ID); err != nil {
return perm, err
} else if isCollaborator {
return perm, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly.

@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2016 The Gitea Authors. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

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

2018

Copy link
Member

Choose a reason for hiding this comment

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

I think maybe 2016 because the file might have been modified then.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

Copy link
Member Author

Choose a reason for hiding this comment

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

@techknowlogick @zeripath yes. If I explicitly know the file is modified on some year I will change to that year, but will change it to 2018.

@codecov-io
Copy link

codecov-io commented Nov 12, 2018

Codecov Report

Merging #5314 into master will increase coverage by 0.02%.
The diff coverage is 49.76%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5314      +/-   ##
==========================================
+ Coverage   37.56%   37.59%   +0.02%     
==========================================
  Files         313      315       +2     
  Lines       46611    46714     +103     
==========================================
+ Hits        17510    17560      +50     
- Misses      26607    26657      +50     
- Partials     2494     2497       +3
Impacted Files Coverage Δ
routers/api/v1/repo/release.go 47.9% <ø> (+2.09%) ⬆️
models/repo_unit.go 60.71% <ø> (+2.09%) ⬆️
routers/api/v1/repo/file.go 22.8% <ø> (+0.77%) ⬆️
models/user.go 45.42% <ø> (+0.58%) ⬆️
models/access.go 54.9% <ø> (-5.73%) ⬇️
routers/api/v1/repo/label.go 0% <ø> (ø) ⬆️
routers/api/v1/repo/collaborators.go 0% <ø> (ø) ⬆️
models/branches.go 47.32% <0%> (-1.7%) ⬇️
routers/api/v1/repo/pull.go 17.13% <0%> (-0.16%) ⬇️
routers/api/v1/repo/fork.go 0% <0%> (ø) ⬆️
... and 47 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0222623...d54bc51. Read the comment docs.

@lunny lunny changed the title WIP: Fix units permission problems WIP: Fix permission check problems Nov 13, 2018
@lunny lunny changed the title WIP: Fix permission check problems WIP: Restrict permission check on repositories and fix some problems Nov 13, 2018
@lunny lunny added the pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! label Nov 13, 2018
@lunny lunny force-pushed the lunny/fix_units_permissions branch from e518b8f to 2070b5c Compare November 17, 2018 05:28
@kTitan
Copy link

kTitan commented Nov 17, 2018

Does this PR also fix #4329?

@lunny
Copy link
Member Author

lunny commented Nov 18, 2018

Just place a FIXME and another PR could change that function. This PR will only refactor or fix permissions check.

@lunny lunny changed the title WIP: Restrict permission check on repositories and fix some problems Restrict permission check on repositories and fix some problems Nov 18, 2018
@lunny
Copy link
Member Author

lunny commented Nov 18, 2018

I think this is ready for review

@lunny
Copy link
Member Author

lunny commented Nov 25, 2018

rebased

@bkcsoft bkcsoft added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 26, 2018
@lunny
Copy link
Member Author

lunny commented Nov 27, 2018

@zeripath need your approval.

@bkcsoft bkcsoft added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Nov 27, 2018
@lafriks
Copy link
Member

lafriks commented Nov 27, 2018

@lunny please resolve conflicts

@lunny lunny force-pushed the lunny/fix_units_permissions branch from f90dc26 to 79365d8 Compare November 28, 2018 03:10
@lunny
Copy link
Member Author

lunny commented Nov 28, 2018

@lafriks done.

@zeripath
Copy link
Contributor

Sorry @lunny I've been a little busy so haven't been able to keep up with your changes.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. pr/breaking Merging this PR means builds will break. Needs a description what exactly breaks, and how to fix it! type/bug type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants