Merge branch 'feat/0.5.0/timeline_ai' of git.backyard.segmentfault.com:opensource/answer into feat/0.5.0/timeline_ai

This commit is contained in:
aichy126 2022-11-25 15:30:46 +08:00
commit d5b2ab1557
5 changed files with 19 additions and 31 deletions

View File

@ -158,7 +158,6 @@ func (ac *AnswerController) Update(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
rank.AnswerEditRank,
@ -168,11 +167,12 @@ func (ac *AnswerController) Update(ctx *gin.Context) {
handler.HandleResponse(ctx, err, nil)
return
}
if !canList[0] {
req.CanEdit = canList[0]
req.NoNeedReview = canList[1]
if !req.CanEdit {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
return
}
req.NoNeedReview = canList[1]
_, err = ac.answerService.Update(ctx, req)
if err != nil {
@ -211,7 +211,6 @@ func (ac *AnswerController) AnswerList(ctx *gin.Context) {
return
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
rank.AnswerEditRank,

View File

@ -93,7 +93,6 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) {
userID := middleware.GetLoginUserIDFromContext(ctx)
req := &schema.QuestionPermission{}
canList, err := qc.rankService.CheckOperationPermissions(ctx, userID, []string{
rank.QuestionAddRank,
rank.QuestionEditRank,
rank.QuestionDeleteRank,
}, id)
@ -101,14 +100,9 @@ func (qc *QuestionController) GetQuestion(ctx *gin.Context) {
handler.HandleResponse(ctx, err, nil)
return
}
req.CanAdd = canList[0]
req.CanEdit = canList[1]
req.CanDelete = canList[2]
req.CanEdit = canList[0]
req.CanDelete = canList[1]
req.CanClose = middleware.GetIsAdminFromContext(ctx)
if !req.CanAdd {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
return
}
info, err := qc.questionService.GetQuestionAndAddPV(ctx, id, userID, req)
if err != nil {
@ -253,7 +247,6 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
canList, err := qc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
rank.QuestionAddRank,
rank.QuestionEditRank,
rank.QuestionDeleteRank,
rank.QuestionEditWithoutReviewRank,
@ -262,15 +255,10 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
handler.HandleResponse(ctx, err, nil)
return
}
req.CanAdd = canList[0]
req.CanEdit = canList[1]
req.CanDelete = canList[2]
req.NoNeedReview = canList[3]
req.CanEdit = canList[0]
req.CanDelete = canList[1]
req.NoNeedReview = canList[2]
req.CanClose = middleware.GetIsAdminFromContext(ctx)
if !req.CanAdd {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
return
}
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
if !req.CanEdit {
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)

View File

@ -28,7 +28,7 @@ func NewUserRepo(data *data.Data, configRepo config.ConfigRepo) usercommon.UserR
// AddUser add user
func (ur *userRepo) AddUser(ctx context.Context, user *entity.User) (err error) {
_, err = ur.data.DB.Insert(user)
_, err = ur.data.DB.UseBool("is_admin").Insert(user)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -122,7 +122,7 @@ func (ur *userRepo) UpdateInfo(ctx context.Context, userInfo *entity.User) (err
// GetByUserID get user info by user id
func (ur *userRepo) GetByUserID(ctx context.Context, userID string) (userInfo *entity.User, exist bool, err error) {
userInfo = &entity.User{}
exist, err = ur.data.DB.Where("id = ?", userID).Get(userInfo)
exist, err = ur.data.DB.Where("id = ?", userID).UseBool("is_admin").Get(userInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -131,7 +131,7 @@ func (ur *userRepo) GetByUserID(ctx context.Context, userID string) (userInfo *e
func (ur *userRepo) BatchGetByID(ctx context.Context, ids []string) ([]*entity.User, error) {
list := make([]*entity.User, 0)
err := ur.data.DB.In("id", ids).Find(&list)
err := ur.data.DB.In("id", ids).UseBool("is_admin").Find(&list)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -141,7 +141,7 @@ func (ur *userRepo) BatchGetByID(ctx context.Context, ids []string) ([]*entity.U
// GetByUsername get user by username
func (ur *userRepo) GetByUsername(ctx context.Context, username string) (userInfo *entity.User, exist bool, err error) {
userInfo = &entity.User{}
exist, err = ur.data.DB.Where("username = ?", username).Get(userInfo)
exist, err = ur.data.DB.Where("username = ?", username).UseBool("is_admin").Get(userInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -151,7 +151,7 @@ func (ur *userRepo) GetByUsername(ctx context.Context, username string) (userInf
// GetByEmail get user by email
func (ur *userRepo) GetByEmail(ctx context.Context, email string) (userInfo *entity.User, exist bool, err error) {
userInfo = &entity.User{}
exist, err = ur.data.DB.Where("e_mail = ?", email).Get(userInfo)
exist, err = ur.data.DB.Where("e_mail = ?", email).UseBool("is_admin").Get(userInfo)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}

View File

@ -31,6 +31,8 @@ type AnswerUpdateReq struct {
EditSummary string `validate:"omitempty" json:"edit_summary"` // edit_summary
IsAdmin bool `json:"-"`
NoNeedReview bool `json:"-"`
// whether user can edit it
CanEdit bool `json:"-"`
}
type AnswerListReq struct {

View File

@ -98,7 +98,7 @@ func (rs *RankService) CheckOperationPermission(ctx context.Context, userID stri
if len(objectID) > 0 {
objectInfo, err := rs.objectInfoService.GetInfo(ctx, objectID)
if err != nil {
log.Error(err)
return can, err
}
// if the user is this object creator, the user can operate this object.
if objectInfo != nil &&
@ -131,7 +131,7 @@ func (rs *RankService) CheckOperationPermissions(ctx context.Context, userID str
if len(objectID) > 0 {
objectInfo, err := rs.objectInfoService.GetInfo(ctx, objectID)
if err != nil {
log.Error(err)
return can, err
}
// if the user is this object creator, the user can operate this object.
if objectInfo != nil &&
@ -162,10 +162,9 @@ func (rs *RankService) checkUserRank(ctx context.Context, userID string, userRan
if err != nil {
return false, err
}
currentUserRank := userRank
if currentUserRank < requireRank {
if userRank < requireRank || requireRank < 0 {
log.Debugf("user %s want to do action %s, but rank %d < %d",
userID, action, currentUserRank, requireRank)
userID, action, userRank, requireRank)
return false, nil
}
return true, nil