From 8461218d63563299d66959acc1fd630cc6927d1d Mon Sep 17 00:00:00 2001 From: zicla Date: Sun, 21 Apr 2019 02:50:17 +0800 Subject: [PATCH] Finish the basic feature of Propfind. --- rest/config.go | 4 +++ rest/dav_controller.go | 62 +++++++++++++++++++++++++++++++++++++----- rest/dav_service.go | 6 +--- rest/user_dao.go | 13 ++++++++- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/rest/config.go b/rest/config.go index d4f293a..7710341 100644 --- a/rest/config.go +++ b/rest/config.go @@ -11,6 +11,10 @@ const ( //用户身份的cookie字段名 COOKIE_AUTH_KEY = "_ak" + //用户身份的Authorization字段名 + AUTHORIZATION_KEY = "Authorization" + + //数据库表前缀 tank200表示当前应用版本是tank:2.0.x版,数据库结构发生变化必然是中型升级 TABLE_PREFIX = "tank20_" diff --git a/rest/dav_controller.go b/rest/dav_controller.go index ad23ad3..6561ae2 100644 --- a/rest/dav_controller.go +++ b/rest/dav_controller.go @@ -2,9 +2,9 @@ package rest import ( "fmt" + "io/ioutil" "net/http" "regexp" - "tank/rest/dav" ) /** @@ -67,6 +67,30 @@ func (this *DavController) Init() { } } +//通过BasicAuth的方式授权。 +func (this *DavController) CheckCurrentUser(writer http.ResponseWriter, request *http.Request) *User { + + username, password, ok := request.BasicAuth() + if !ok { + //要求前端使用Basic的形式授权 + writer.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) + + panic(ConstWebResult(CODE_WRAPPER_LOGIN)) + + } + + user := this.userDao.FindByUsername(username) + if user == nil { + this.PanicBadRequest("邮箱或密码错误") + } else { + if !MatchBcrypt(password, user.Password) { + this.PanicBadRequest("邮箱或密码错误") + } + } + + return user +} + //注册自己的路由。 func (this *DavController) RegisterRoutes() map[string]func(writer http.ResponseWriter, request *http.Request) { @@ -97,21 +121,45 @@ func (this *DavController) HandleRoutes(writer http.ResponseWriter, request *htt //完成系统安装 func (this *DavController) Index(writer http.ResponseWriter, request *http.Request, subPath string) { - this.logger.Info("请求访问来了:%s %s", request.RequestURI, subPath) + this.logger.Info("--------- URI: %s SUB_PATH: %s ---------", request.RequestURI, subPath) + + //获取请求者 + user := this.CheckCurrentUser(writer, request) method := request.Method if method == "PROPFIND" { - this.davService.HandlePropfind(writer, request, subPath) + this.davService.HandlePropfind(writer, request, user, subPath) } else { - handler := &dav.Handler{ - FileSystem: dav.Dir("D:/Group/Golang/src/webdav/tmp"), - LockSystem: dav.NewMemLS(), + /*打印所有HEADER以及请求参数*/ + + fmt.Printf("\n------ 请求: %s ------\n", request.URL) + + fmt.Printf("\n------Method:------\n") + fmt.Println(request.Method) + + fmt.Printf("\n------Header:------\n") + for key, value := range request.Header { + fmt.Printf("%s = %s\n", key, value) } - handler.ServeHTTP(writer, request) + fmt.Printf("\n------请求参数:------\n") + for key, value := range request.Form { + fmt.Printf("%s = %s\n", key, value) + } + + fmt.Printf("\n------Body:------\n") + body, err := ioutil.ReadAll(request.Body) + if err != nil { + fmt.Println("读取body时出错" + err.Error()) + } + fmt.Println(string(body)) + + fmt.Println("------------------") + + this.PanicBadRequest("该方法还不支持。") } } diff --git a/rest/dav_service.go b/rest/dav_service.go index 0d1e248..550ddc1 100644 --- a/rest/dav_service.go +++ b/rest/dav_service.go @@ -140,16 +140,13 @@ func (this *DavService) Propstats(matter *Matter, propfind dav.Propfind) []dav.P } //处理 方法 -func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, subPath string) { +func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http.Request, user *User, subPath string) { fmt.Printf("列出文件/文件夹 %s\n", subPath) //获取请求的层数。暂不支持 infinity depth := this.ParseDepth(request) - //获取请求者 - user := this.checkUser(writer, request) - //读取请求参数。按照用户的参数请求返回内容。 propfind, _, err := dav.ReadPropfind(request.Body) this.PanicError(err) @@ -163,7 +160,6 @@ func (this *DavService) HandlePropfind(writer http.ResponseWriter, request *http matter = this.matterDao.checkByUserUuidAndPath(user.Uuid, subPath) } - var matters []*Matter if depth == 0 { matters = []*Matter{matter} diff --git a/rest/user_dao.go b/rest/user_dao.go index 31f2120..28d68cf 100644 --- a/rest/user_dao.go +++ b/rest/user_dao.go @@ -49,12 +49,23 @@ func (this *UserDao) CheckByUuid(uuid string) *User { } // Read - var user *User = &User{} + var user = &User{} db := CONTEXT.DB.Where(&User{Base: Base{Uuid: uuid}}).First(user) this.PanicError(db.Error) return user } +//按照邮箱查询用户。 +func (this *UserDao) FindByUsername(username string) *User { + + var user = &User{} + db := CONTEXT.DB.Where(&User{Username: username}).First(user) + if db.Error != nil { + return nil + } + return user +} + //按照邮箱查询用户。 func (this *UserDao) FindByEmail(email string) *User {