Skip to content

Commit 73db62b

Browse files
committed
图书增加权限校验
1 parent 89f3b11 commit 73db62b

File tree

7 files changed

+213
-202
lines changed

7 files changed

+213
-202
lines changed

config.example.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
"MaxIdle" : 3, /*最大的空闲连接数*/
1818
"MaxActive" : 5 /*最大的激活连接数*/
1919
},
20-
"mongodb": {
21-
"URL" : "", /*mongodb连接地址, 可以不填,不填的话,就没有计划任务和数据统计功能*/
22-
"Database" : "golang123"
20+
"mongodb": { /*mongodb连接地址, 可以不填,不填的话,就没有数据统计功能*/
21+
"URL" : ""
2322
},
2423
"go": {
2524
"APIPoweredBy" : "Golang123 API",/*后台go加的X-Powered-By*/
@@ -30,12 +29,12 @@
3029
"LogDir" : "", /*日志文件所在的目录,如果不设的话,默认在项目目录下*/
3130
"APIPrefix" : "/api", /*api服务请求前缀*/
3231
/*图片上传的目录, 默认是golang123/website/static/upload/img目录*/
33-
/*如果要修改的话,请使用绝对路径,不要使用相对路径*/
32+
/*如果要修改的话,请使用绝对路径,不要使用相对路径, 并在Nginx配置中,将修改后的目录配置为静态目录*/
3433
"UploadImgDir" : "",
3534
"ImgPath" : "/upload/img", /*上传后的图片请求地址前缀*/
3635
"MaxMultipartMemory" : 3, /*上传的图片最大允许的大小,单位MB*/
3736
"Port" : 8023, /*go监听的端口*/
38-
"CronEnabled" : false, /*是否允许计划任务*/
37+
"StatsEnabled" : false, /*是否开启数据统计功能, 开启的话要安装mongodb*/
3938
"TokenSecret" : "Jxa26iOirBiYX8iLCJuYm5iy", /*TokenSecret,请修改*/
4039
"TokenMaxAge" : 86400, /*token多久过期,单位秒*/
4140
"PassSalt" : "xrjoN1qR", /*用户密码加盐,请修改*/

controller/book/book.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,28 @@ func Chapters(c *gin.Context) {
420420
SendErrJSON("错误的图书id", c)
421421
return
422422
}
423+
424+
var book model.Book
425+
if err := model.DB.Where("status != ?", model.BookVerifyFail).First(&book, id).Error; err != nil {
426+
SendErrJSON("错误的图书id", c)
427+
return
428+
}
429+
430+
if book.ReadLimits == model.BookReadLimitsPrivate {
431+
userInter, _ := c.Get("user")
432+
if userInter == nil {
433+
SendErrJSON("没有权限", c)
434+
return
435+
}
436+
user := userInter.(model.User)
437+
if user.ID != book.UserID {
438+
SendErrJSON("没有权限.", c)
439+
return
440+
}
441+
}
442+
423443
var chapters []model.BookChapter
424-
if err := model.DB.Model(&model.BookChapter{}).Where("book_id = ?", id).Order("created_at desc").Find(&chapters).Error; err != nil {
444+
if err := model.DB.Model(&model.BookChapter{}).Where("book_id = ?", id).Select("id, name").Order("created_at desc").Find(&chapters).Error; err != nil {
425445
fmt.Println(err)
426446
SendErrJSON("error", c)
427447
return

controller/common/upload.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func Upload(c *gin.Context) (map[string]interface{}, error) {
3333
}
3434
var mimeType = mime.TypeByExtension(ext)
3535

36+
fmt.Printf("filename %s, index %d, ext %s, mimeType %s\n", filename, index, ext, mimeType)
37+
if mimeType == "" && ext == ".jpeg" {
38+
mimeType = "image/jpeg"
39+
}
3640
if mimeType == "" {
3741
return nil, errors.New("无效的图片类型")
3842
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
func main() {
1717
fmt.Println("gin.Version: ", gin.Version)
1818
if config.ServerConfig.Env != model.DevelopmentMode {
19+
gin.SetMode(gin.ReleaseMode)
1920
// Disable Console Color, you don't need console color when writing the logs to file.
2021
gin.DisableConsoleColor()
2122
// Logging to a file.

router/route.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ func Route(router *gin.Engine) {
134134
api.GET("/books/user/public/:userID", book.UserPublicBooks)
135135
api.GET("/books/info/:id", middleware.SetContextUser,
136136
book.Info)
137-
api.GET("/books/chapters/:bookID", book.Chapters)
137+
api.GET("/books/chapters/:bookID", middleware.SetContextUser,
138+
book.Chapters)
138139
api.GET("/books/chapter/:chapterID", middleware.SetContextUser,
139140
book.Chapter)
140141
api.POST("/books", middleware.SigninRequired,

0 commit comments

Comments
 (0)