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
fix bug
  • Loading branch information
lunny committed Nov 28, 2018
commit 861b3b24b3670d3894db5a100e885f1a18c8eb0c
27 changes: 16 additions & 11 deletions models/repo_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (p *Permission) UnitAccessMode(unitType UnitType) AccessMode {
return p.UnitsMode[unitType]
}

// CanAccess returns true if user has read access to the unit of the repository
// CanAccess returns true if user has mode access to the unit of the repository
func (p *Permission) CanAccess(mode AccessMode, unitType UnitType) bool {
return p.UnitAccessMode(unitType) >= mode
}

// CanAccessAny returns true if user has read access to any of the units of the repository
// CanAccessAny returns true if user has mode access to any of the units of the repository
func (p *Permission) CanAccessAny(mode AccessMode, unitTypes ...UnitType) bool {
for _, u := range unitTypes {
if p.CanAccess(mode, u) {
Expand All @@ -62,6 +62,20 @@ func (p *Permission) CanRead(unitType UnitType) bool {
return p.CanAccess(AccessModeRead, unitType)
}

// CanReadAny returns true if user has read access to any of the units of the repository
func (p *Permission) CanReadAny(unitTypes ...UnitType) bool {
return p.CanAccessAny(AccessModeRead, unitTypes...)
}

// CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
// returns true if isPull is false and user could read to issues
func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
if isPull {
return p.CanRead(UnitTypePullRequests)
}
return p.CanRead(UnitTypeIssues)
}

// CanWrite returns true if user could write to this unit
func (p *Permission) CanWrite(unitType UnitType) bool {
return p.CanAccess(AccessModeWrite, unitType)
Expand All @@ -76,15 +90,6 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
return p.CanWrite(UnitTypeIssues)
}

// CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
// returns true if isPull is false and user could read to issues
func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
if isPull {
return p.CanRead(UnitTypePullRequests)
}
return p.CanRead(UnitTypeIssues)
}

// GetUserRepoPermission returns the user permissions to the repository
func GetUserRepoPermission(repo *Repository, user *User) (Permission, error) {
return getUserRepoPermission(x, repo, user)
Expand Down
10 changes: 5 additions & 5 deletions templates/repo/activity.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
</h2>
<div class="ui divider"></div>

{{if (or (.Permission.CanAccess $.UnitTypeIssues) (.Permission.CanAccess $.UnitTypePullRequests))}}
{{if (or (.Permission.CanRead $.UnitTypeIssues) (.Permission.CanRead $.UnitTypePullRequests))}}
<h4 class="ui top attached header">{{.i18n.Tr "repo.activity.overview"}}</h4>
<div class="ui attached segment two column grid">
{{if .Permission.CanAccess $.UnitTypePullRequests}}
{{if .Permission.CanRead $.UnitTypePullRequests}}
<div class="column">
{{if gt .Activity.ActivePRCount 0}}
<div class="stats-table">
Expand All @@ -41,7 +41,7 @@
{{.i18n.Tr (TrN .i18n.Lang .Activity.ActivePRCount "repo.activity.active_prs_count_1" "repo.activity.active_prs_count_n") .Activity.ActivePRCount | Safe }}
</div>
{{end}}
{{if .Permission.CanAccess $.UnitTypeIssues}}
{{if .Permission.CanRead $.UnitTypeIssues}}
<div class="column">
{{if gt .Activity.ActiveIssueCount 0}}
<div class="stats-table">
Expand All @@ -58,7 +58,7 @@
{{end}}
</div>
<div class="ui attached segment horizontal segments">
{{if .Permission.CanAccess $.UnitTypePullRequests}}
{{if .Permission.CanRead $.UnitTypePullRequests}}
<a href="#merged-pull-requests" class="ui attached segment text center">
<i class="text purple octicon octicon-git-pull-request"></i> <strong>{{.Activity.MergedPRCount}}</strong><br>
{{.i18n.Tr (TrN .i18n.Lang .Activity.MergedPRCount "repo.activity.merged_prs_count_1" "repo.activity.merged_prs_count_n") }}
Expand All @@ -68,7 +68,7 @@
{{.i18n.Tr (TrN .i18n.Lang .Activity.OpenedPRCount "repo.activity.opened_prs_count_1" "repo.activity.opened_prs_count_n") }}
</a>
{{end}}
{{if .Permission.CanAccess $.UnitTypeIssues}}
{{if .Permission.CanRead $.UnitTypeIssues}}
<a href="#closed-issues" class="ui attached segment text center">
<i class="text red octicon octicon-issue-closed"></i> <strong>{{.Activity.ClosedIssueCount}}</strong><br>
{{.i18n.Tr (TrN .i18n.Lang .Activity.ClosedIssueCount "repo.activity.closed_issues_count_1" "repo.activity.closed_issues_count_n") }}
Expand Down
18 changes: 9 additions & 9 deletions templates/repo/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{{.NumStars}}
</a>
</div>
{{if and (not .IsBare) ($.Permission.CanAccess $.UnitTypeCode)}}
{{if and (not .IsBare) ($.Permission.CanRead $.UnitTypeCode)}}
<div class="ui compact labeled button" tabindex="0">
<a class="ui compact button {{if not $.CanSignedUserFork}}poping up{{end}}" {{if $.CanSignedUserFork}}href="{{AppSubUrl}}/repo/fork/{{.ID}}"{{else if $.IsSigned}} data-content="{{$.i18n.Tr "repo.fork_from_self"}}" data-position="top center" data-variation="tiny"{{end}}>
<i class="octicon octicon-repo-forked"></i>{{$.i18n.Tr "repo.fork"}}
Expand All @@ -47,43 +47,43 @@
{{if not .IsDiffCompare}}
<div class="ui tabs container">
<div class="ui tabular stackable menu navbar">
{{if .Permission.CanAccess $.UnitTypeCode}}
{{if .Permission.CanRead $.UnitTypeCode}}
<a class="{{if .PageIsViewCode}}active{{end}} item" href="{{.RepoLink}}{{if (ne .BranchName .Repository.DefaultBranch)}}/src/{{.BranchNameSubURL | EscapePound}}{{end}}">
<i class="octicon octicon-code"></i> {{.i18n.Tr "repo.code"}}
</a>
{{end}}

{{if .Permission.CanAccess $.UnitTypeIssues}}
{{if .Permission.CanRead $.UnitTypeIssues}}
<a class="{{if .PageIsIssueList}}active{{end}} item" href="{{.RepoLink}}/issues">
<i class="octicon octicon-issue-opened"></i> {{.i18n.Tr "repo.issues"}} <span class="ui {{if not .Repository.NumOpenIssues}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenIssues}}</span>
</a>
{{end}}

{{if .Permission.CanAccess $.UnitTypeExternalTracker}}
{{if .Permission.CanRead $.UnitTypeExternalTracker}}
<a class="{{if .PageIsIssueList}}active{{end}} item" href="{{.RepoLink}}/issues" target="_blank" rel="noopener noreferrer">
<i class="octicon octicon-issue-opened"></i> {{.i18n.Tr "repo.issues"}} </span>
</a>
{{end}}

{{if and .Repository.CanEnablePulls (.Permission.CanAccess $.UnitTypePullRequests)}}
{{if and .Repository.CanEnablePulls (.Permission.CanRead $.UnitTypePullRequests)}}
<a class="{{if .PageIsPullList}}active{{end}} item" href="{{.RepoLink}}/pulls">
<i class="octicon octicon-git-pull-request"></i> {{.i18n.Tr "repo.pulls"}} <span class="ui {{if not .Repository.NumOpenPulls}}gray{{else}}blue{{end}} small label">{{.Repository.NumOpenPulls}}</span>
</a>
{{end}}

{{if and (.Permission.CanAccess $.UnitTypeReleases) (not .IsBareRepo) }}
{{if and (.Permission.CanRead $.UnitTypeReleases) (not .IsBareRepo) }}
<a class="{{if .PageIsReleaseList}}active{{end}} item" href="{{.RepoLink}}/releases">
<i class="octicon octicon-tag"></i> {{.i18n.Tr "repo.releases"}} <span class="ui {{if not .Repository.NumReleases}}gray{{else}}blue{{end}} small label">{{.Repository.NumReleases}}</span>
</a>
{{end}}

{{if or (.Permission.CanAccess $.UnitTypeWiki) (.Permission.CanAccess $.UnitTypeExternalWiki)}}
<a class="{{if .PageIsWiki}}active{{end}} item" href="{{.RepoLink}}/wiki" {{if (.Permission.CanAccess $.UnitTypeExternalWiki)}} target="_blank" rel="noopener noreferrer" {{end}}>
{{if or (.Permission.CanRead $.UnitTypeWiki) (.Permission.CanRead $.UnitTypeExternalWiki)}}
<a class="{{if .PageIsWiki}}active{{end}} item" href="{{.RepoLink}}/wiki" {{if (.Permission.CanRead $.UnitTypeExternalWiki)}} target="_blank" rel="noopener noreferrer" {{end}}>
<i class="octicon octicon-book"></i> {{.i18n.Tr "repo.wiki"}}
</a>
{{end}}

{{if and (.Permission.CanAccessAny $.UnitTypePullRequests $.UnitTypeIssues $.UnitTypeReleases) (not .IsBareRepo)}}
{{if and (.Permission.CanReadAny $.UnitTypePullRequests $.UnitTypeIssues $.UnitTypeReleases) (not .IsBareRepo)}}
<a class="{{if .PageIsActivity}}active{{end}} item" href="{{.RepoLink}}/activity">
<i class="octicon octicon-pulse"></i> {{.i18n.Tr "repo.activity"}}
</a>
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/release/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<a href="{{$.RepoLink}}/src/tag/{{.TagName | EscapePound}}" rel=""><i class="tag icon"></i> {{.TagName}}</a>
</h4>
<div class="download">
{{if $.Permission.CanAccess $.UnitTypeCode}}
{{if $.Permission.CanRead $.UnitTypeCode}}
<a href="{{$.RepoLink}}/src/commit/{{.Sha1}}" rel=""><i class="code icon"></i> {{ShortSha .Sha1}}</a>
<a href="{{$.RepoLink}}/archive/{{.TagName | EscapePound}}.zip" rel=""><i class="octicon octicon-file-zip"></i> ZIP</a>
<a href="{{$.RepoLink}}/archive/{{.TagName | EscapePound}}.tar.gz"><i class="octicon octicon-file-zip"></i> TAR.GZ</a>
Expand All @@ -66,7 +66,7 @@
<div class="download">
<h2>{{$.i18n.Tr "repo.release.downloads"}}</h2>
<ul class="list">
{{if $.Permission.CanAccess $.UnitTypeCode}}
{{if $.Permission.CanRead $.UnitTypeCode}}
<li>
<a href="{{$.RepoLink}}/archive/{{.TagName | EscapePound}}.zip" rel=""><strong><i class="octicon octicon-file-zip"></i> {{$.i18n.Tr "repo.release.source_code"}} (ZIP)</strong></a>
</li>
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
</div>
</div>

{{if .Permission.CanAccess $.UnitTypeWiki}}
{{if .Permission.CanRead $.UnitTypeWiki}}
<div class="ui divider"></div>

<div class="item">
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/sub_menu.tmpl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="ui segment sub-menu">
<div class="ui two horizontal center link list">
{{if and (.Permission.CanAccess $.UnitTypeCode) (not .IsBareRepo)}}
{{if and (.Permission.CanRead $.UnitTypeCode) (not .IsBareRepo)}}
<div class="item{{if .PageIsCommits}} active{{end}}">
<a href="{{.RepoLink}}/commits{{if .IsViewBranch}}/branch{{else if .IsViewTag}}/tag{{else if .IsViewCommit}}/commit{{end}}/{{EscapePound .BranchName}}"><i class="octicon octicon-history"></i> <b>{{.CommitsCount}}</b> {{.i18n.Tr (TrN .i18n.Lang .CommitsCount "repo.commit" "repo.commits") }}</a>
</div>
{{end}}
{{if and (.Permission.CanAccess $.UnitTypeCode) (not .IsBareRepo) }}
{{if and (.Permission.CanRead $.UnitTypeCode) (not .IsBareRepo) }}
<div class="item{{if .PageIsBranches}} active{{end}}">
<a href="{{.RepoLink}}/branches/"><i class="octicon octicon-git-branch"></i> <b>{{.BranchesCount}}</b> {{.i18n.Tr (TrN .i18n.Lang .BranchesCount "repo.branch" "repo.branches") }}</a>
</div>
Expand Down