db

package
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2023 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserIdTag uint8 = 0x1
	RoleIdTag uint8 = 0x2
)

Variables

View Source
var (
	// Unauthenticated
	ErrInvalidToken = newUnauthenticatedError("invalid token")

	// NotFound
	ErrUserNotFound = newNotFoundError("user not found")
	ErrRoleNotFound = newNotFoundError("role not found")

	ErrAcctIdNotFound = newNotFoundError("acct id not found")

	// AlreadyExists
	ErrAcctAlreadyExists = newAlreadyExistsError("acct already exists")

	// InvalidArgument
	ErrInvalidNonce  = newInvalidArgumentError("invalid nonce")
	ErrInvalidAppId  = newInvalidArgumentError("invalid app id")
	ErrInvalidAcctId = newInvalidArgumentError("invalid acct id")

	// Internal
	ErrMalformedSessData = newInternalError("malformed session data")

	// Unavailable
	ErrDatabaseUnavailable = newUnavailableError("database unavailable")
)

Functions

func AddCodesToGift

func AddCodesToGift(ctx context.Context, appId, giftId string, giftCodes []string) (err error)

func Allow

func Allow(ctx context.Context, appId string, keys []string) (err error)

func BanUsers

func BanUsers(
	ctx context.Context, appId string, userIds []string,
	banTo time.Time, banFor string) (err error)

func BindAcctIdToUser

func BindAcctIdToUser(
	ctx context.Context, appId, userId string,
	acctIds []string, takeOverIfDuplicated bool) (
	_ []string, err error)

func Block

func Block(ctx context.Context, appId string, keys []string, banTo time.Time, banFor string) (err error)

func CancelOrder

func CancelOrder(ctx context.Context, appId, orderId, reason string) (err error)

设置订单已取消

func CheckNonce

func CheckNonce(ctx context.Context, appId, nonce string) (err error)

func CreateGift

func CreateGift(ctx context.Context, appId string, gift *Gift, codes []string) (err error)

func CreateOrder

func CreateOrder(ctx context.Context, appId string, data *v1pb.PurchaseData) (err error)

创建订单

func DecId

func DecId(id string) (appKey uint32, tag uint8, err error)

func DelCodesFromGift

func DelCodesFromGift(ctx context.Context, appId string, codes []string) (err error)

func FulfillOrder

func FulfillOrder(ctx context.Context, appId, orderId string) (err error)

func GetAcctIdByDetail

func GetAcctIdByDetail(
	ctx context.Context, appId string, keyVals map[string]string) (
	acctIds []string, err error)

func GetOrder

func GetOrder(ctx context.Context, appId, orderId string) (_ *v1pb.PurchaseData, err error)

查询订单

func IdBelongToAppId

func IdBelongToAppId(appId string, ids ...string) bool

func LoginUser

func LoginUser(
	ctx context.Context, app *App, clientIp, deviceId string,
	acctIds []string, createIfNotFound bool) (
	_ *User, _ *Sess, err error)

func LogoutUser

func LogoutUser(ctx context.Context, userIds ...string) (err error)

func PayOrder

func PayOrder(ctx context.Context, appId, orderId, transactionId, receipt string) (err error)

设置订单完成,必须提供原始凭证ID以备查验

func RevokeGift

func RevokeGift(ctx context.Context, appId, giftId string) (err error)

func SetRoleMetadata

func SetRoleMetadata(
	ctx context.Context, appId, roleId string,
	md map[string]string) (err error)

func SetUserMetadata

func SetUserMetadata(
	ctx context.Context, appId, userId string,
	md map[string]string) (err error)

func SignInRole

func SignInRole(
	ctx context.Context, appId, roleId string) (err error)

func UnbanUsers

func UnbanUsers(
	ctx context.Context, appId string, userIds []string) (err error)

func UnbindAcctIdFromUser

func UnbindAcctIdFromUser(
	ctx context.Context, appId, userId string, acctIds []string) (
	_ []string, err error)

func UpdateAcctDetail

func UpdateAcctDetail(
	ctx context.Context, appId, acctId, detail string) (err error)

func UpdateGift

func UpdateGift(ctx context.Context, appId string, gift *Gift) (err error)

func WatchApps

func WatchApps(ch chan<- []*App) func()

Types

type Admin

type Admin struct {
	Id          string        `bson:"_id"`
	Name        string        `bson:"name"`
	Description string        `bson:"description"`
	Secret      string        `bson:"secret,omitempty"`
	Permissions []*Permission `bson:"permissions,omitempty"`
}

func FindAdminById

func FindAdminById(id string) *Admin

func (*Admin) IsPermitted

func (x *Admin) IsPermitted(path string) bool

type App

type App struct {
	// 应用ID
	Id string `bson:"_id"`
	// 数值形式的应用ID
	Key uint32 `bson:"key"`
	// 应用签名密钥,授权访问
	Secret string `bson:"secret,omitempty"`
	// 应用指纹,特异化应用数据,增加安全性
	Fingerprint string `bson:"fingerprint,omitempty"`
	// 允许的服务
	Permissions []*Permission `bson:"permissions,omitempty"`
	// contains filtered or unexported fields
}

func FindAppById

func FindAppById(id string) *App

func ListApps

func ListApps() []*App

func (*App) IsPermitted

func (x *App) IsPermitted(path string) bool

type BlockData

type BlockData struct {
	Key string `bson:"_id"`
	// 封禁时间
	BanAt time.Time `bson:"ban_at,omitempty"`
	// 封禁时间
	BanTo time.Time `bson:"ban_to,omitempty"`
	// 封禁原因
	BanFor string `bson:"ban_for,omitempty"`
}

func IsBlocked

func IsBlocked(ctx context.Context, appId string, keys []string) (_ *BlockData, err error)

type Gift

type Gift struct {
	// ID
	Id string `bson:"_id,omitempty"`
	// 更新时间
	UpdateAt time.Time `bson:"update_at,omitempty"`
	// 过期时间
	ExpireAt time.Time `bson:"expire_at,omitempty"`
	// 有效载荷
	Payload []byte `bson:"payload,omitempty"`
}

func GetAllGifts

func GetAllGifts(ctx context.Context, appId string) (gifts []*Gift, err error)

func GetGiftByCode

func GetGiftByCode(ctx context.Context, appId, code string) (_ *Gift, err error)

func GetGiftById

func GetGiftById(ctx context.Context, appId, giftId string) (_ *Gift, err error)

func RedeemGiftCode

func RedeemGiftCode(ctx context.Context, appId, code string) (_ *Gift, err error)

func VerifyGiftCode

func VerifyGiftCode(ctx context.Context, appId, code string) (_ *Gift, err error)

type GiftCode

type GiftCode struct {
	// 礼包码
	Code string `bson:"_id,omitempty"`
	// 礼包ID
	GiftId string `bson:"gift_id,omitempty"`
	// 已兑换
	Redeemed bool `bson:"redeemed,omitempty"`
}

func GetCodesByGiftId

func GetCodesByGiftId(ctx context.Context, appId, giftId string) (_ []*GiftCode, err error)

type Permission

type Permission struct {
	Path   string `json:"path,omitempty" bson:"path,omitempty"`
	Prefix string `json:"prefix,omitempty" bson:"prefix,omitempty"`
	Regexp string `json:"regexp,omitempty" bson:"regexp,omitempty"`
	// contains filtered or unexported fields
}

type Role

type Role struct {
	Id string `bson:"_id"`
	// 角色序号,主要有一下几个用途
	// 1. 创建用户发生重试时保证只有唯一一个角色被成功创建
	// 2. 用来确定该用户的角色分类,比如分区分服
	Index uint32 `bson:"index,omitempty"`
	// 所属用户ID
	UserId string `bson:"user_id,omitempty"`
	// 创建时间
	CreateAt time.Time `bson:"create_at,omitempty"`
	// 上次登录时间
	SignInAt time.Time `bson:"sign_in_at,omitempty"`
	// 元数据
	Metadata map[string]string `bson:"metadata,omitempty"`
}

func AlterRoleIndex

func AlterRoleIndex(
	ctx context.Context, appId, roleId string, index uint32) (
	_ *Role, err error)

func CreateRole

func CreateRole(
	ctx context.Context, appId, userId string, index uint32) (
	_ *Role, err error)

func GetRole

func GetRole(
	ctx context.Context, appId, roleId string) (_ *Role, err error)

func GetRoles

func GetRoles(
	ctx context.Context, appId string, roleIds []string) (
	_ []*Role, err error)

func GetRolesByUserId

func GetRolesByUserId(
	ctx context.Context, appId string, userIds []string) (
	_ []*Role, err error)

func ListRoles

func ListRoles(
	ctx context.Context, appId, userId string) (_ []*Role, err error)

type Sess

type Sess struct {
	AppId  string   `msgpack:"-"`
	UserId string   `msgpack:"-"`
	Token  string   `msgpack:"token"`
	Data   SessData `msgpack:"data"`
	//// 中转数据
	App *App `msgpack:"-"`
}

func CheckToken

func CheckToken(ctx context.Context, token string) (_ *Sess, err error)

type SessData

type SessData struct {
	RoleId    string `msgpack:"roleId"`
	RoleIndex uint32 `msgpack:"roleIndex"`
}

会话缓存数据

type User

type User struct {
	Id string `bson:"_id"`
	// 用户账号列表,其中任意一个匹配都可以认定为该用户
	// 常见的用例为:
	// 1. 游客账号/正式账号
	// 2. 平台账号/第三方账号
	AcctIds []string `bson:"acct_ids,omitempty"`
	// 创建时间
	CreateAt time.Time `bson:"create_at,omitempty"`
	// 创建时IP
	CreateIp string `bson:"create_ip,omitempty"`
	// 上次登录时间
	LoginAt time.Time `bson:"login_at,omitempty"`
	// 上次登录时IP
	LoginIp string `bson:"login_ip,omitempty"`
	// 封号时间
	BanAt time.Time `bson:"ban_at,omitempty"`
	// 封号时间
	BanTo time.Time `bson:"ban_to,omitempty"`
	// 封号原因
	BanFor string `bson:"ban_for,omitempty"`
	// 元数据
	Metadata map[string]string `bson:"metadata,omitempty"`
}

func GetUser

func GetUser(
	ctx context.Context, appId, userId string) (_ *User, err error)

func GetUsers

func GetUsers(
	ctx context.Context, appId string, userIds []string) (
	_ []*User, err error)

func GetUsersByAcctId

func GetUsersByAcctId(
	ctx context.Context, appId string, acctIds ...string) (_ []*User, err error)

func GetUsersWithFields

func GetUsersWithFields(
	ctx context.Context, appId string, userIds, fields []string) (
	_ []*User, err error)

批量拉取时的性能考虑

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL