Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Sep 10, 2024
2 parents b4e90f2 + de7cf35 commit 04dbb42
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/rocboss/paopao-ce/cmd"
"github.com/rocboss/paopao-ce/internal"
"github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/dao"
"github.com/rocboss/paopao-ce/internal/service"
"github.com/rocboss/paopao-ce/pkg/debug"
"github.com/rocboss/paopao-ce/pkg/utils"
Expand Down Expand Up @@ -47,6 +48,7 @@ func init() {
}

func deferFn() {
dao.CloseDsx()
if cfg.If("Sentry") {
// Flush buffered events before the program terminates.
sentry.Flush(2 * time.Second)
Expand Down
8 changes: 8 additions & 0 deletions internal/conf/db_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func MustGormDB() *gorm.DB {
return _gormdb
}

func CloseGormDB() {
db, err := _gormdb.DB()
if err != nil {
log.Fatalf("close gorm db failed: %s", err)
}
_ = db.Close()
}

func newGormDB() (db *gorm.DB, err error) {
newLogger := logger.New(
logrus.StandardLogger(), // io writer(日志输出的目标,前缀和日志包含的内容)
Expand Down
13 changes: 13 additions & 0 deletions internal/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ func initDsX() {
logrus.Infof("use %s as core.ServantA with version %s", dsaVer.Name(), dsaVer.Version())
}

func CloseDsx() {
if cfg.If("Gorm") {
jinzhu.CloseDbObject()
} else if cfg.If("Sqlx") {
sakila.CloseDbObject()
} else if cfg.If("Sqlc") && cfg.Any("Postgres", "PostgreSQL") {
slonik.CloseDbObject()
} else {
// default use gorm as orm for sql database
jinzhu.CloseDbObject()
}
}

func initOSS() {
var v core.VersionInfo
if cfg.If("AliOSS") {
Expand Down
4 changes: 4 additions & 0 deletions internal/dao/jinzhu/jinzhu.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func NewDataService() (core.DataService, core.VersionInfo) {
return cache.NewCacheDataService(ds), ds
}

func CloseDbObject() {
conf.CloseGormDB()
}

func NewWebDataServantA() (core.WebDataServantA, core.VersionInfo) {
lazyInitial()
db := conf.MustGormDB()
Expand Down
4 changes: 4 additions & 0 deletions internal/dao/sakila/sakila.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil
}

func CloseDbObject() {
logrus.Fatal("not support now")
}
4 changes: 4 additions & 0 deletions internal/dao/slonik/slonik.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ func NewAuthorizationManageService() core.AuthorizationManageService {
logrus.Fatal("not support now")
return nil
}

func CloseDbObject() {
logrus.Fatal("not support now")
}
1 change: 1 addition & 0 deletions internal/model/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type TopicListResp struct {
}

type TweetDetailReq struct {
BaseInfo `form:"-" binding:"-"`
SimpleInfo `form:"-" binding:"-"`
TweetId int64 `form:"id"`
}
Expand Down
5 changes: 5 additions & 0 deletions internal/servants/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ func (s *looseSrv) TweetDetail(req *web.TweetDetailReq) (*web.TweetDetailResp, m
if err != nil {
return nil, web.ErrGetPostFailed
}

// check current user permission
if xerr := checkPostViewPermission(req.User, post, s.Ds); xerr != nil {
return nil, xerr
}
postContents, err := s.Ds.GetPostContentsByIDs([]int64{post.ID})
if err != nil {
return nil, web.ErrGetPostFailed
Expand Down
26 changes: 26 additions & 0 deletions internal/servants/web/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,29 @@ func checkPermision(user *ms.User, targetUserId int64) mir.Error {
}
return nil
}

// checkPostViewPermission 检查当前用户是否可读指定post
func checkPostViewPermission(user *ms.User, post *ms.Post, ds core.DataService) mir.Error {
if post.Visibility == core.PostVisitPublic {
return nil
}

if user == nil {
return web.ErrNoPermission
}

if user.IsAdmin || user.ID == post.UserID {
return nil
}

if post.Visibility == core.PostVisitPrivate {
return web.ErrNoPermission
}

if post.Visibility == core.PostVisitFriend {
if !ds.IsFriend(post.UserID, user.ID) && !ds.IsFriend(user.ID, post.UserID) {
return web.ErrNoPermission
}
}
return nil
}
2 changes: 1 addition & 1 deletion web/src/views/Post.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<n-list class="main-content-wrap" bordered>
<n-list-item>
<n-spin :show="loading">
<div class="detail-wrap" v-if="post.id > 1">
<div class="detail-wrap" v-if="post.id > 0">
<post-detail :post="post" @reload="reloadPost" />
</div>
<div class="empty-wrap" v-else>
Expand Down

0 comments on commit 04dbb42

Please sign in to comment.