Skip to content

Commit

Permalink
Merge pull request #563 from chhsiao1981/api.getUserPermInfo
Browse files Browse the repository at this point in the history
api.getUserPermInfo
  • Loading branch information
chhsiao1981 authored Oct 2, 2024
2 parents 9cf47b2 + bed586b commit 4cb8ec8
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 58 deletions.
2 changes: 1 addition & 1 deletion api/add_favorite_board.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func AddFavoriteBoard(remoteAddr string, userID bbs.UUserID, params interface{},
return nil, 500, err
}

_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/create_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func CreateArticle(remoteAddr string, userID bbs.UUserID, params interface{}, pa
return nil, 500, err
}

err = CheckUserBoardPermPostable(userID, boardID)
err = CheckUserBoardPermPostable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/create_board.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func CreateBoard(remoteAddr string, userID bbs.UUserID, params interface{}, path
return nil, 400, ErrInvalidPath
}

err = CheckUserBoardPermCreatable(userID)
err = CheckUserBoardPermCreatable(userID, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/create_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func CreateComment(remoteAddr string, userID bbs.UUserID, params interface{}, pa
}
articleID := thePath.FArticleID.ToArticleID()

err = CheckUserBoardPermPostable(userID, boardID)
err = CheckUserBoardPermPostable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/delete_articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func DeleteArticles(remoteAddr string, userID bbs.UUserID, params interface{}, p
return nil, 500, err
}

userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID)
userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand All @@ -59,7 +59,7 @@ func DeleteArticles(remoteAddr string, userID bbs.UUserID, params interface{}, p
articleIDs = append(articleIDs, articleID.ToArticleID())
}

articlePermMap, err := CheckUserArticlesPermDeletable(userID, boardID, articleIDs, userBoardPermReadable)
articlePermMap, err := CheckUserArticlesPermDeletable(userID, boardID, articleIDs, userBoardPermReadable, c)
if err != nil {
return nil, 500, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/edit_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func EditArticleDetail(remoteAddr string, userID bbs.UUserID, params interface{}
articleID := thePath.FArticleID.ToArticleID()

// check permission
err = CheckUserArticlePermEditable(userID, boardID, articleID, true)
err = CheckUserArticlePermEditable(userID, boardID, articleID, true, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/get_article_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func GetArticleBlocks(remoteAddr string, userID bbs.UUserID, params interface{},
articleID := thePath.FArticleID.ToArticleID()

// check permission
err = CheckUserArticlePermReadable(userID, boardID, articleID, true)
err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/get_article_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func GetArticleDetail(remoteAddr string, userID bbs.UUserID, params interface{},
articleID := thePath.FArticleID.ToArticleID()

// check permission
err = CheckUserArticlePermReadable(userID, boardID, articleID, true)
err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/get_board_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetBoardDetail(remoteAddr string, userID bbs.UUserID, params interface{}, p
}

// is board-valid-user
_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/get_board_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func GetBoardSummary(remoteAddr string, userID bbs.UUserID, params interface{},
return nil, 400, err
}

_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down
43 changes: 25 additions & 18 deletions api/get_user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ func GetUserInfo(remoteAddr string, userID bbs.UUserID, params interface{}, path
return nil, 400, ErrInvalidPath
}

return tryGetUserInfo(userID, thePath.UserID, c)
}

func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context) (result *GetUserInfoResult, statusCode int, err error) {
userPermInfo, err := schema.GetUserPermInfo(userID)
userPermInfo, err := getUserPermInfo(userID, c)
if err != nil {
return nil, 500, err
}
Expand All @@ -130,23 +126,12 @@ func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context)

updateNanoTS := types.NowNanoTS()

// get backend data
var result_b pttbbsapi.GetUserResult

urlMap := map[string]string{
"uid": string(queryUserID),
}
url := utils.MergeURL(urlMap, pttbbsapi.GET_USER_R)

statusCode, err = utils.BackendGet(c, url, nil, nil, &result_b)
userDetail, statusCode, err := tryGetUserInfo(userID, thePath.UserID, updateNanoTS, c)
if err != nil {
return nil, statusCode, err
}

userDetail, err := deserializeUserDetailAndUpdateDB(result_b, updateNanoTS)
if err != nil {
return nil, 500, err
}
queryUserID := thePath.UserID

userNewInfo, err := schema.GetUserNewInfo(queryUserID)
if err != nil {
Expand All @@ -168,6 +153,28 @@ func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, c *gin.Context)
return result, 200, nil
}

func tryGetUserInfo(userID bbs.UUserID, queryUserID bbs.UUserID, updateNanoTS types.NanoTS, c *gin.Context) (userDetail *schema.UserDetail, statusCode int, err error) {
// get backend data
var result_b pttbbsapi.GetUserResult

urlMap := map[string]string{
"uid": string(queryUserID),
}
url := utils.MergeURL(urlMap, pttbbsapi.GET_USER_R)

statusCode, err = utils.BackendGet(c, url, nil, nil, &result_b)
if err != nil {
return nil, statusCode, err
}

userDetail, err = deserializeUserDetailAndUpdateDB(result_b, updateNanoTS)
if err != nil {
return nil, 500, err
}

return userDetail, 0, nil
}

func NewUserInfoResult(userDetail_db *schema.UserDetail, userNewInfo_db *schema.UserNewInfo, userIDEmail_db *schema.UserIDEmail, userEmail_db *schema.UserEmail, userPermInfo *schema.UserPermInfo) (result *GetUserInfoResult) {
if userNewInfo_db == nil {
userNewInfo_db = &schema.UserNewInfo{}
Expand Down
2 changes: 1 addition & 1 deletion api/load_article_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func LoadArticleComments(remoteAddr string, userID bbs.UUserID, params interface
articleID := thePath.FArticleID.ToArticleID()

// check permission
err = CheckUserArticlePermReadable(userID, boardID, articleID, true)
err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c)
if err != nil {
return nil, statusCode, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/load_bottom_articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func LoadBottomArticles(remoteAddr string, userID bbs.UUserID, params interface{
return nil, 500, err
}

userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID)
userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand All @@ -50,7 +50,7 @@ func LoadBottomArticles(remoteAddr string, userID bbs.UUserID, params interface{
articleIDs[idx] = each.ArticleID
}

articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm)
articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c)
if err != nil {
return nil, 500, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/load_class_boards.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func LoadClassBoards(remoteAddr string, userID bbs.UUserID, params interface{},
}

// is board-valid-user
_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, statusCode, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/load_general_articles.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func LoadGeneralArticles(remoteAddr string, userID bbs.UUserID, params interface
}

// check board permission
userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID)
userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand All @@ -79,7 +79,7 @@ func LoadGeneralArticles(remoteAddr string, userID bbs.UUserID, params interface
}

// check article permission
articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm)
articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c)
if err != nil {
return nil, 500, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/load_general_articles_by_keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func LoadGeneralArticlesByKeyword(remoteAddr string, userID bbs.UUserID, params
}

// check board permission
userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID)
userBoardPerm, err := CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, 403, err
}
Expand Down Expand Up @@ -55,7 +55,7 @@ func LoadGeneralArticlesByKeyword(remoteAddr string, userID bbs.UUserID, params
}

// check article permission
articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm)
articlePermEditableMap, articlePermDeletableMap, err := CheckUserArticlesPermEditableDeletable(userID, boardID, articleIDs, userBoardPerm, c)
if err != nil {
return nil, 500, err
}
Expand Down
2 changes: 1 addition & 1 deletion api/reply_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ReplyComments(remoteAddr string, userID bbs.UUserID, params interface{}, pa
articleID := thePath.FArticleID.ToArticleID()

// check permission
err = CheckUserArticlePermReadable(userID, boardID, articleID, true)
err = CheckUserArticlePermReadable(userID, boardID, articleID, true, c)
if err != nil {
return nil, 403, err
}
Expand Down
25 changes: 13 additions & 12 deletions api/user_article_perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package api
import (
"github.com/Ptt-official-app/go-pttbbs/bbs"
"github.com/Ptt-official-app/go-pttbbsweb/schema"
"github.com/gin-gonic/gin"
)

// CheckUserArticlePermReadable
//
// Readable
func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool) (err error) {
func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool, c *gin.Context) (err error) {
if isCheckBoard {
_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return err
}
Expand All @@ -35,9 +36,9 @@ func CheckUserArticlePermReadable(userID bbs.UUserID, boardID bbs.BBoardID, arti
// CheckUserArticlePermEditable
//
// Editable
func CheckUserArticlePermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool) (err error) {
func CheckUserArticlePermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, isCheckBoard bool, c *gin.Context) (err error) {
if isCheckBoard {
_, err = CheckUserBoardPermReadable(userID, boardID)
_, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -69,9 +70,9 @@ func checkUserArticlePermEditableCore(userID bbs.UUserID, boardID bbs.BBoardID,
// CheckUserArticlesPermEditable
//
// articles Editable
func CheckUserArticlesPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable) (articlePermMap map[bbs.ArticleID]error, err error) {
func CheckUserArticlesPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable, c *gin.Context) (articlePermMap map[bbs.ArticleID]error, err error) {
if userBoardPerm == nil {
userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID)
userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -113,8 +114,8 @@ func checkUserArticlesPermEditableCore(userID bbs.UUserID, articlesPermInfo []*s
// CheckUserArticlePermDeletable
//
// Deletable
func CheckUserArticlePermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID) (err error) {
userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID)
func CheckUserArticlePermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleID bbs.ArticleID, c *gin.Context) (err error) {
userBoardPermReadable, err := CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -153,9 +154,9 @@ func checkUserArticlePermDeletableCore(userID bbs.UUserID, boardID bbs.BBoardID,
// CheckUserArticlePermDeletable
//
// Deletable
func CheckUserArticlesPermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPermReadable *UserBoardPermReadable) (articlePermMap map[bbs.ArticleID]error, err error) {
func CheckUserArticlesPermDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPermReadable *UserBoardPermReadable, c *gin.Context) (articlePermMap map[bbs.ArticleID]error, err error) {
if userBoardPermReadable == nil {
userBoardPermReadable, err = CheckUserBoardPermReadable(userID, boardID)
userBoardPermReadable, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -200,9 +201,9 @@ func checkUserArticlesPermDeletableCore(userID bbs.UUserID, articlesPermInfo []*
// CheckUserArticlesPermEditable
//
// articles Editable
func CheckUserArticlesPermEditableDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable) (articlePermEditableMap map[bbs.ArticleID]error, articlePermDeletableMap map[bbs.ArticleID]error, err error) {
func CheckUserArticlesPermEditableDeletable(userID bbs.UUserID, boardID bbs.BBoardID, articleIDs []bbs.ArticleID, userBoardPerm *UserBoardPermReadable, c *gin.Context) (articlePermEditableMap map[bbs.ArticleID]error, articlePermDeletableMap map[bbs.ArticleID]error, err error) {
if userBoardPerm == nil {
userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID)
userBoardPerm, err = CheckUserBoardPermReadable(userID, boardID, c)
if err != nil {
return nil, nil, err
}
Expand Down
17 changes: 9 additions & 8 deletions api/user_board_perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Ptt-official-app/go-pttbbs/ptttype"
"github.com/Ptt-official-app/go-pttbbsweb/schema"
"github.com/Ptt-official-app/go-pttbbsweb/types"
"github.com/gin-gonic/gin"
)

type UserBoardPermReadable struct {
Expand All @@ -30,8 +31,8 @@ var COOLDOWN_LIMIT = []*CooldownLimit{
}

// CheckUserBoardPermCreatable
func CheckUserBoardPermCreatable(userID bbs.UUserID) (err error) {
userPermInfo, err := schema.GetUserPermInfo(userID)
func CheckUserBoardPermCreatable(userID bbs.UUserID, c *gin.Context) (err error) {
userPermInfo, err := getUserPermInfo(userID, c)
if err != nil {
return err
}
Expand All @@ -57,8 +58,8 @@ func checkUserBoardPermCreatableCore(userPermInfo *schema.UserPermInfo) (err err
// CheckUserBoardPermReadable
//
// https://github.com/ptt/pttbbs/blob/master/mbbsd/board.c#L185
func CheckUserBoardPermReadable(userID bbs.UUserID, boardID bbs.BBoardID) (userBoardPerm *UserBoardPermReadable, err error) {
userPermInfo, err := schema.GetUserPermInfo(userID)
func CheckUserBoardPermReadable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (userBoardPerm *UserBoardPermReadable, err error) {
userPermInfo, err := getUserPermInfo(userID, c)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,8 +149,8 @@ func checkUserBoardPermReadableCore(userID bbs.UUserID, boardID bbs.BBoardID, us
// CheckUserBoardPermPostable
//
// https://github.com/ptt/pttbbs/blob/master/mbbsd/cache.c#L209
func CheckUserBoardPermPostable(userID bbs.UUserID, boardID bbs.BBoardID) (err error) {
userPermInfo, err := schema.GetUserPermInfo(userID)
func CheckUserBoardPermPostable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (err error) {
userPermInfo, err := getUserPermInfo(userID, c)
if err != nil {
return err
}
Expand Down Expand Up @@ -260,8 +261,8 @@ func checkUserBoardPermPostableCore(userID bbs.UUserID, boardID bbs.BBoardID, us
}

// CheckUserBoardPermEditable
func CheckUserBoardPermEditable(userID bbs.UUserID, boardID bbs.BBoardID) (err error) {
userPermInfo, err := schema.GetUserPermInfo(userID)
func CheckUserBoardPermEditable(userID bbs.UUserID, boardID bbs.BBoardID, c *gin.Context) (err error) {
userPermInfo, err := getUserPermInfo(userID, c)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 4cb8ec8

Please sign in to comment.