Skip to content

Commit 3385803

Browse files
committed
refactor(serverHandler): simplify perm check
1 parent 7a05566 commit 3385803

File tree

3 files changed

+27
-125
lines changed

3 files changed

+27
-125
lines changed

src/serverHandler/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (h *aliasHandler) visitTreeNode(
104104
return
105105
}
106106

107-
if fInfo.IsDir() && h.getCanIndex(urlPath, fsPath, userId) {
107+
if fInfo.IsDir() && h.index.match(urlPath, fsPath, userId) {
108108
childInfos, _, _ := h.mergeAlias(urlPath, fInfo, childInfos, true)
109109
childInfos = h.FilterItems(childInfos)
110110

src/serverHandler/perm.go

Lines changed: 19 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package serverHandler
22

3-
import (
4-
"mjpclab.dev/ghfs/src/util"
5-
"os"
6-
)
3+
import "mjpclab.dev/ghfs/src/util"
74

85
type hierarchyAvailability struct {
96
global bool
@@ -28,6 +25,24 @@ func newHierarchyAvailability(
2825
}
2926
}
3027

28+
func (ha *hierarchyAvailability) match(urlPath, fsPath string, userId int) bool {
29+
if ha.global {
30+
return true
31+
}
32+
33+
if hasUrlOrDirPrefix(ha.urls, urlPath, ha.dirs, fsPath) {
34+
return true
35+
}
36+
37+
if userId >= 0 {
38+
if _, match := hasUrlOrDirPrefixUsers(ha.urlsUsers, urlPath, ha.dirsUsers, fsPath, userId); match {
39+
return true
40+
}
41+
}
42+
43+
return false
44+
}
45+
3146
func hasUrlOrDirPrefix(urls []string, reqUrl string, dirs []string, reqDir string) bool {
3247
for _, url := range urls {
3348
if util.HasUrlPrefixDir(reqUrl, url) {
@@ -79,117 +94,3 @@ func hasUrlOrDirPrefixUsers(urlsUsers pathIntsList, reqUrl string, dirsUsers pat
7994

8095
return
8196
}
82-
83-
func (h *aliasHandler) getCanIndex(rawReqPath, reqFsPath string, userId int) bool {
84-
if h.index.global {
85-
return true
86-
}
87-
88-
if hasUrlOrDirPrefix(h.index.urls, rawReqPath, h.index.dirs, reqFsPath) {
89-
return true
90-
}
91-
92-
if userId >= 0 {
93-
if _, match := hasUrlOrDirPrefixUsers(h.index.urlsUsers, rawReqPath, h.index.dirsUsers, reqFsPath, userId); match {
94-
return true
95-
}
96-
}
97-
98-
return false
99-
}
100-
101-
func (h *aliasHandler) getCanUpload(info os.FileInfo, rawReqPath, reqFsPath string, userId int) bool {
102-
if info == nil || !info.IsDir() {
103-
return false
104-
}
105-
106-
if h.upload.global {
107-
return true
108-
}
109-
110-
if hasUrlOrDirPrefix(h.upload.urls, rawReqPath, h.upload.dirs, reqFsPath) {
111-
return true
112-
}
113-
114-
if userId >= 0 {
115-
if _, match := hasUrlOrDirPrefixUsers(h.upload.urlsUsers, rawReqPath, h.upload.dirsUsers, reqFsPath, userId); match {
116-
return true
117-
}
118-
}
119-
120-
return false
121-
}
122-
123-
func (h *aliasHandler) getCanMkdir(info os.FileInfo, rawReqPath, reqFsPath string, userId int) bool {
124-
if info == nil || !info.IsDir() {
125-
return false
126-
}
127-
128-
if h.mkdir.global {
129-
return true
130-
}
131-
132-
if hasUrlOrDirPrefix(h.mkdir.urls, rawReqPath, h.mkdir.dirs, reqFsPath) {
133-
return true
134-
}
135-
136-
if userId >= 0 {
137-
if _, match := hasUrlOrDirPrefixUsers(h.mkdir.urlsUsers, rawReqPath, h.mkdir.dirsUsers, reqFsPath, userId); match {
138-
return true
139-
}
140-
}
141-
142-
return false
143-
}
144-
145-
func (h *aliasHandler) getCanDelete(info os.FileInfo, rawReqPath, reqFsPath string, userId int) bool {
146-
if info == nil || !info.IsDir() {
147-
return false
148-
}
149-
150-
if h.delete.global {
151-
return true
152-
}
153-
154-
if hasUrlOrDirPrefix(h.delete.urls, rawReqPath, h.delete.dirs, reqFsPath) {
155-
return true
156-
}
157-
158-
if userId >= 0 {
159-
if _, match := hasUrlOrDirPrefixUsers(h.delete.urlsUsers, rawReqPath, h.delete.dirsUsers, reqFsPath, userId); match {
160-
return true
161-
}
162-
}
163-
164-
return false
165-
}
166-
167-
func (h *aliasHandler) getCanArchive(subInfos []os.FileInfo, rawReqPath, reqFsPath string, userId int) bool {
168-
if len(subInfos) == 0 {
169-
return false
170-
}
171-
172-
if h.archive.global {
173-
return true
174-
}
175-
176-
if hasUrlOrDirPrefix(h.archive.urls, rawReqPath, h.archive.dirs, reqFsPath) {
177-
return true
178-
}
179-
180-
if userId >= 0 {
181-
if _, match := hasUrlOrDirPrefixUsers(h.archive.urlsUsers, rawReqPath, h.archive.dirsUsers, reqFsPath, userId); match {
182-
return true
183-
}
184-
}
185-
186-
return false
187-
}
188-
189-
func (h *aliasHandler) getCanCors(rawReqPath, reqFsPath string) bool {
190-
if h.cors.global {
191-
return true
192-
}
193-
194-
return hasUrlOrDirPrefix(h.cors.urls, rawReqPath, h.cors.dirs, reqFsPath)
195-
}

src/serverHandler/sessionData.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
394394
}
395395
}
396396

397-
canIndex := authSuccess && h.getCanIndex(vhostReqPath, fsPath, authUserId)
397+
canIndex := authSuccess && h.index.match(vhostReqPath, fsPath, authUserId)
398398
indexFile, indexItem, _statIdxErr := h.statIndexFile(vhostReqPath, fsPath, item, canIndex && redirectAction == noRedirect)
399399
if _statIdxErr != nil {
400400
errs = append(errs, _statIdxErr)
@@ -453,12 +453,13 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
453453

454454
subItemPrefix := getSubItemPrefix(currDirRelPath, vhostReqPath, tailSlash)
455455

456-
canUpload := allowAccess && authSuccess && h.getCanUpload(item, vhostReqPath, fsPath, authUserId)
457-
canMkdir := allowAccess && authSuccess && h.getCanMkdir(item, vhostReqPath, fsPath, authUserId)
458-
canDelete := allowAccess && authSuccess && h.getCanDelete(item, vhostReqPath, fsPath, authUserId)
456+
isDir := item != nil && item.IsDir()
457+
canUpload := allowAccess && authSuccess && isDir && h.upload.match(vhostReqPath, fsPath, authUserId)
458+
canMkdir := allowAccess && authSuccess && isDir && h.mkdir.match(vhostReqPath, fsPath, authUserId)
459+
canDelete := allowAccess && authSuccess && isDir && h.delete.match(vhostReqPath, fsPath, authUserId)
459460
hasDeletable := canDelete && len(subItems) > len(aliasSubItems)
460-
canArchive := allowAccess && authSuccess && h.getCanArchive(subItems, vhostReqPath, fsPath, authUserId)
461-
canCors := allowAccess && authSuccess && h.getCanCors(vhostReqPath, fsPath)
461+
canArchive := allowAccess && authSuccess && h.archive.match(vhostReqPath, fsPath, authUserId)
462+
canCors := allowAccess && authSuccess && h.cors.match(vhostReqPath, fsPath, authUserId)
462463
loginAvail := len(authUserName) == 0 && h.users.Len() > 0
463464

464465
context := pathContext{

0 commit comments

Comments
 (0)