Skip to content

Commit

Permalink
fixed: check permission of post view.
Browse files Browse the repository at this point in the history
  • Loading branch information
rocboss committed Sep 8, 2024
1 parent b4f30f2 commit a4348f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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
}

0 comments on commit a4348f2

Please sign in to comment.