Skip to content

Commit e92f4ef

Browse files
committed
feat(serverHandler/responseData): add field LoginAvail
1 parent 6149dda commit e92f4ef

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

src/i18n/translation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package i18n
22

33
type Translation struct {
4+
LoginLabel string
45
MkdirLabel string
56

67
UploadFilesLabel string
@@ -38,6 +39,7 @@ type Translation struct {
3839
}
3940

4041
var translationEnUs = Translation{
42+
LoginLabel: "Login",
4143
MkdirLabel: "Create dir",
4244

4345
UploadFilesLabel: "Files",
@@ -75,6 +77,7 @@ var translationEnUs = Translation{
7577
}
7678

7779
var translationZhSimp = Translation{
80+
LoginLabel: "登录",
7881
MkdirLabel: "建目录",
7982

8083
UploadFilesLabel: "文件",
@@ -112,6 +115,7 @@ var translationZhSimp = Translation{
112115
}
113116

114117
var translationZhTrad = Translation{
118+
LoginLabel: "登入",
115119
MkdirLabel: "建目錄",
116120

117121
UploadFilesLabel: "檔案",

src/serverHandler/auth.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ func (h *aliasHandler) needAuth(w http.ResponseWriter, r *http.Request) {
99
w.Header().Set("WWW-Authenticate", "Basic realm=\"files\"")
1010
}
1111

12-
func (h *aliasHandler) verifyAuth(r *http.Request) (username string, success bool, err error) {
13-
var password string
14-
var hasAuthReq bool
15-
username, password, hasAuthReq = r.BasicAuth()
12+
func (h *aliasHandler) verifyAuth(r *http.Request, needAuth bool) (username string, success bool, err error) {
13+
user, pass, hasAuthReq := r.BasicAuth()
1614

17-
if !hasAuthReq {
18-
err = errors.New(r.RemoteAddr + " missing auth info")
19-
return
15+
if hasAuthReq && h.users.Auth(user, pass) {
16+
return user, true, nil
17+
}
18+
19+
if !needAuth {
20+
return "", true, nil
2021
}
2122

22-
success = h.users.Auth(username, password)
23-
if !success {
23+
if !hasAuthReq {
24+
err = errors.New(r.RemoteAddr + " missing auth info")
25+
} else {
2426
err = errors.New(r.RemoteAddr + " auth failed")
2527
}
28+
2629
return
2730
}
2831

src/serverHandler/responseData.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type responseData struct {
5353
HasDeletable bool
5454
CanArchive bool
5555
CanCors bool
56+
LoginAvail bool
5657

5758
errors []error
5859
Status int
@@ -307,11 +308,8 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
307308
status := http.StatusOK
308309

309310
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
310-
authUserName := ""
311-
authSuccess := true
311+
authUserName, authSuccess, _authErr := h.verifyAuth(r, needAuth)
312312
if needAuth {
313-
var _authErr error
314-
authUserName, authSuccess, _authErr = h.verifyAuth(r)
315313
if _authErr != nil {
316314
errs = append(errs, _authErr)
317315
}
@@ -419,6 +417,7 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
419417
hasDeletable := canDelete && len(subItems) > len(aliasSubItems)
420418
canArchive := h.getCanArchive(subItems, rawReqPath, reqFsPath)
421419
canCors := h.getCanCors(rawReqPath, reqFsPath)
420+
loginAvail := len(authUserName) == 0 && h.users.Len() > 0
422421

423422
context := pathContext{
424423
download: isDownload,
@@ -455,6 +454,7 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
455454
HasDeletable: hasDeletable,
456455
CanArchive: canArchive,
457456
CanCors: canCors,
457+
LoginAvail: loginAvail,
458458

459459
errors: errs,
460460
Status: status,

src/user/list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ type List struct {
1010
namesEqualFunc util.StrEqualFunc
1111
}
1212

13+
func (list *List) Len() int {
14+
return len(list.users)
15+
}
16+
1317
func (list *List) findIndex(username string) int {
1418
for i := range list.users {
1519
if list.namesEqualFunc(list.users[i].getName(), username) {

0 commit comments

Comments
 (0)