refactor: extract nested structures in the config.

pull/2100/head
Gordon 2 years ago
parent bf5ed62b53
commit a2ff3de7e4

@ -38,10 +38,15 @@ type MessageApi struct {
*rpcclient.Message *rpcclient.Message
validate *validator.Validate validate *validator.Validate
userRpcClient *rpcclient.UserRpcClient userRpcClient *rpcclient.UserRpcClient
manager *config.Manager
imAdmin *config.IMAdmin
} }
func NewMessageApi(msgRpcClient *rpcclient.Message, userRpcClient *rpcclient.User) MessageApi { func NewMessageApi(msgRpcClient *rpcclient.Message, userRpcClient *rpcclient.User, manager *config.Manager,
return MessageApi{Message: msgRpcClient, validate: validator.New(), userRpcClient: rpcclient.NewUserRpcClientByUser(userRpcClient)} imAdmin *config.IMAdmin) MessageApi {
return MessageApi{Message: msgRpcClient, validate: validator.New(),
userRpcClient: rpcclient.NewUserRpcClientByUser(userRpcClient),
manager: manager, imAdmin: imAdmin}
} }
func (MessageApi) SetOptions(options map[string]bool, value bool) { func (MessageApi) SetOptions(options map[string]bool, value bool) {
@ -199,7 +204,7 @@ func (m *MessageApi) SendMessage(c *gin.Context) {
} }
// Check if the user has the app manager role. // Check if the user has the app manager role.
if !authverify.IsAppManagerUid(c, m.Config) { if !authverify.IsAppManagerUid(c, m.manager, m.imAdmin) {
// Respond with a permission error if the user is not an app manager. // Respond with a permission error if the user is not an app manager.
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
return return
@ -257,7 +262,7 @@ func (m *MessageApi) SendBusinessNotification(c *gin.Context) {
return return
} }
if !authverify.IsAppManagerUid(c, m.Config) { if !authverify.IsAppManagerUid(c, m.manager, m.imAdmin) {
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
return return
} }
@ -301,7 +306,7 @@ func (m *MessageApi) BatchSendMsg(c *gin.Context) {
return return
} }
log.ZInfo(c, "BatchSendMsg", "req", req) log.ZInfo(c, "BatchSendMsg", "req", req)
if err := authverify.CheckAdmin(c, m.Config); err != nil { if err := authverify.CheckAdmin(c, m.manager, m.imAdmin); err != nil {
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
return return
} }

@ -147,7 +147,7 @@ func newGinRouter(disCov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
thirdRpc := rpcclient.NewThird(disCov, config.RpcRegisterName.OpenImThirdName, config.Prometheus.GrafanaUrl) thirdRpc := rpcclient.NewThird(disCov, config.RpcRegisterName.OpenImThirdName, config.Prometheus.GrafanaUrl)
u := NewUserApi(*userRpc) u := NewUserApi(*userRpc)
m := NewMessageApi(messageRpc, userRpc) m := NewMessageApi(messageRpc, userRpc, &config.Manager, &config.IMAdmin)
ParseToken := GinParseToken(rdb, config) ParseToken := GinParseToken(rdb, config)
userRouterGroup := r.Group("/user") userRouterGroup := r.Group("/user")
{ {

@ -90,7 +90,7 @@ func (s *Server) GetUsersOnlineStatus(
ctx context.Context, ctx context.Context,
req *msggateway.GetUsersOnlineStatusReq, req *msggateway.GetUsersOnlineStatusReq,
) (*msggateway.GetUsersOnlineStatusResp, error) { ) (*msggateway.GetUsersOnlineStatusResp, error) {
if !authverify.IsAppManagerUid(ctx, s.config) { if !authverify.IsAppManagerUid(ctx, &s.config.Manager, &s.config.IMAdmin) {
return nil, errs.ErrNoPermission.Wrap("only app manager") return nil, errs.ErrNoPermission.Wrap("only app manager")
} }
var resp msggateway.GetUsersOnlineStatusResp var resp msggateway.GetUsersOnlineStatusResp

@ -47,16 +47,16 @@ func CheckAccessV3(ctx context.Context, ownerUserID string, config *config.Globa
return errs.ErrNoPermission.Wrap("ownerUserID", ownerUserID) return errs.ErrNoPermission.Wrap("ownerUserID", ownerUserID)
} }
func IsAppManagerUid(ctx context.Context, config *config.GlobalConfig) bool { func IsAppManagerUid(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) bool {
return (len(config.Manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), config.Manager.UserID)) || return (len(manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), manager.UserID)) ||
utils.IsContain(mcontext.GetOpUserID(ctx), config.IMAdmin.UserID) utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID)
} }
func CheckAdmin(ctx context.Context, config *config.GlobalConfig) error { func CheckAdmin(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) error {
if len(config.Manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), config.Manager.UserID) { if len(manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), manager.UserID) {
return nil return nil
} }
if utils.IsContain(mcontext.GetOpUserID(ctx), config.IMAdmin.UserID) { if utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID) {
return nil return nil
} }
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))

@ -248,6 +248,16 @@ type Push struct {
Jpns Jpns `yaml:"jpns"` Jpns Jpns `yaml:"jpns"`
} }
type Manager struct {
UserID []string `yaml:"userID"`
Nickname []string `yaml:"nickname"`
}
type IMAdmin struct {
UserID []string `yaml:"userID"`
Nickname []string `yaml:"nickname"`
}
type Prometheus struct { type Prometheus struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
GrafanaUrl string `yaml:"grafanaUrl"` GrafanaUrl string `yaml:"grafanaUrl"`
@ -345,16 +355,10 @@ type GlobalConfig struct {
LongConnSvr LongConnSvr `yaml:"longConnSvr"` LongConnSvr LongConnSvr `yaml:"longConnSvr"`
Push Push `yaml:"push"` Push Push `yaml:"push"`
Manager struct { Manager Manager `yaml:"manager"`
UserID []string `yaml:"userID"`
Nickname []string `yaml:"nickname"`
} `yaml:"manager"`
IMAdmin struct { IMAdmin IMAdmin `yaml:"im-admin"`
UserID []string `yaml:"userID"`
Nickname []string `yaml:"nickname"`
} `yaml:"im-admin"`
MultiLoginPolicy int `yaml:"multiLoginPolicy"` MultiLoginPolicy int `yaml:"multiLoginPolicy"`
MsgCacheTimeout int `yaml:"msgCacheTimeout"` MsgCacheTimeout int `yaml:"msgCacheTimeout"`

Loading…
Cancel
Save