mirror of https://github.com/rocboss/paopao-ce
commit
7c2ad0bd82
@ -0,0 +1,117 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
"github.com/rocboss/paopao-ce/pkg/types"
|
||||
)
|
||||
|
||||
const (
|
||||
ActRegisterUser act = iota
|
||||
ActCreatePublicTweet
|
||||
ActCreatePublicAttachment
|
||||
ActCreatePublicPicture
|
||||
ActCreatePublicVideo
|
||||
ActCreatePrivateTweet
|
||||
ActCreatePrivateAttachment
|
||||
ActCreatePrivatePicture
|
||||
ActCreatePrivateVideo
|
||||
ActCreateFriendTweet
|
||||
ActCreateFriendAttachment
|
||||
ActCreateFriendPicture
|
||||
ActCreateFriendVideo
|
||||
ActCreatePublicComment
|
||||
ActCreatePublicPicureComment
|
||||
ActCreateFriendComment
|
||||
ActCreateFriendPicureComment
|
||||
ActCreatePrivateComment
|
||||
ActCreatePrivatePicureComment
|
||||
ActStickTweet
|
||||
ActTopTweet
|
||||
ActLockTweet
|
||||
ActVisibleTweet
|
||||
ActDeleteTweet
|
||||
ActCreateActivationCode
|
||||
)
|
||||
|
||||
type act uint8
|
||||
|
||||
type FriendFilter map[int64]types.Empty
|
||||
|
||||
type Action struct {
|
||||
Act act
|
||||
UserId int64
|
||||
}
|
||||
|
||||
type AuthorizationManageService interface {
|
||||
IsAllow(user *model.User, action *Action) bool
|
||||
GetFriendFilter(userId int64) FriendFilter
|
||||
GetFriendIds(userId int64) []int64
|
||||
}
|
||||
|
||||
func (f FriendFilter) IsFriend(userId int64) bool {
|
||||
// _, yesno := f[userId]
|
||||
// return yesno
|
||||
// so, you are friend with all world now
|
||||
return true
|
||||
}
|
||||
|
||||
// IsAllow default true if user is admin
|
||||
func (a act) IsAllow(user *model.User, userId int64, isFriend bool, isActivation bool) bool {
|
||||
if user.IsAdmin {
|
||||
return true
|
||||
}
|
||||
if user.ID == userId && isActivation {
|
||||
switch a {
|
||||
case ActCreatePublicTweet,
|
||||
ActCreatePublicAttachment,
|
||||
ActCreatePublicPicture,
|
||||
ActCreatePublicVideo,
|
||||
ActCreatePrivateTweet,
|
||||
ActCreatePrivateAttachment,
|
||||
ActCreatePrivatePicture,
|
||||
ActCreatePrivateVideo,
|
||||
ActCreateFriendTweet,
|
||||
ActCreateFriendAttachment,
|
||||
ActCreateFriendPicture,
|
||||
ActCreateFriendVideo,
|
||||
ActCreatePrivateComment,
|
||||
ActCreatePrivatePicureComment,
|
||||
ActStickTweet,
|
||||
ActLockTweet,
|
||||
ActVisibleTweet,
|
||||
ActDeleteTweet:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if user.ID == userId && !isActivation {
|
||||
switch a {
|
||||
case ActCreatePrivateTweet,
|
||||
ActCreatePrivateComment,
|
||||
ActStickTweet,
|
||||
ActLockTweet,
|
||||
ActDeleteTweet:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if isFriend && isActivation {
|
||||
switch a {
|
||||
case ActCreatePublicComment,
|
||||
ActCreatePublicPicureComment,
|
||||
ActCreateFriendComment,
|
||||
ActCreateFriendPicureComment:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if !isFriend && isActivation {
|
||||
switch a {
|
||||
case ActCreatePublicComment,
|
||||
ActCreatePublicPicureComment:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/conf"
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
)
|
||||
|
||||
func newSimpleAuthorizationManageService() *simpleAuthorizationManageService {
|
||||
return &simpleAuthorizationManageService{
|
||||
db: conf.DBEngine,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *simpleAuthorizationManageService) IsAllow(user *model.User, action *core.Action) bool {
|
||||
// user is activation if had bind phone
|
||||
isActivation := (len(user.Phone) != 0)
|
||||
isFriend := s.isFriend(action.UserId)
|
||||
// TODO: just use defaut act authorization chek rule now
|
||||
return action.Act.IsAllow(user, action.UserId, isFriend, isActivation)
|
||||
}
|
||||
|
||||
// GetFriendFilter _userId保留未来使用
|
||||
func (s *simpleAuthorizationManageService) GetFriendFilter(_userId int64) core.FriendFilter {
|
||||
// TODO: just return an empty friend fileter now
|
||||
return core.FriendFilter{}
|
||||
}
|
||||
|
||||
func (s *simpleAuthorizationManageService) GetFriendIds(_userId int64) []int64 {
|
||||
// TODO: just retrun empty now
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *simpleAuthorizationManageService) isFriend(_userId int64) bool {
|
||||
// friend with all world now
|
||||
return true
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"github.com/rocboss/paopao-ce/internal/core"
|
||||
"github.com/rocboss/paopao-ce/internal/model"
|
||||
)
|
||||
|
||||
func (s *tweetSearchFilter) filterResp(user *model.User, resp *core.QueryResp) {
|
||||
// 管理员不过滤
|
||||
if user != nil && user.IsAdmin {
|
||||
return
|
||||
}
|
||||
|
||||
var item *model.PostFormated
|
||||
items := resp.Items
|
||||
latestIndex := len(items) - 1
|
||||
if user == nil {
|
||||
for i := 0; i <= latestIndex; i++ {
|
||||
item = items[i]
|
||||
if item.Visibility != model.PostVisitPublic {
|
||||
items[i] = items[latestIndex]
|
||||
items = items[:latestIndex]
|
||||
resp.Total--
|
||||
latestIndex--
|
||||
i--
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var cutFriend, cutPrivate bool
|
||||
friendFilter := s.ams.GetFriendFilter(user.ID)
|
||||
for i := 0; i <= latestIndex; i++ {
|
||||
item = items[i]
|
||||
cutFriend = (item.Visibility == model.PostVisitFriend && !friendFilter.IsFriend(item.UserID))
|
||||
cutPrivate = (item.Visibility == model.PostVisitPrivate && user.ID != item.UserID)
|
||||
if cutFriend || cutPrivate {
|
||||
items[i] = items[latestIndex]
|
||||
items = items[:latestIndex]
|
||||
resp.Total--
|
||||
latestIndex--
|
||||
i--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp.Items = items
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package types
|
||||
|
||||
type Empty = struct{}
|
||||
|
||||
type Any = interface{}
|
||||
|
||||
type AnySlice = []interface{}
|
Loading…
Reference in new issue