diff --git a/deployments/templates/config.yaml b/deployments/templates/config.yaml index 56d105292..4f68683b4 100644 --- a/deployments/templates/config.yaml +++ b/deployments/templates/config.yaml @@ -72,8 +72,7 @@ log: withStack: false # Whether to include stack trace in logs # Secret key for secure communication -secret: openIM123 # SECRET, Secret OpenIM key for encryption and secure communication -chatSecret: openIM123 # ChatSecret, ChatSecret chat key for encryption and secure communication +secret: openIM123 # SECRET, Secret key for encryption and secure communication # Token policy configuration tokenPolicy: @@ -118,6 +117,12 @@ adminList: nickname: chat3 imAdmin: openIMAdmin +# chatAdmin, use for send notification +chatAdmin: + - adminID: chatAdmin + nickname: chatAdmin + imAdmin: imAdmin + # URL for OpenIM server openIMUrl: "http://172.28.0.1:10002" # OPENIM_SERVER_ADDRESS:API_OPENIM_PORT, URL of the OpenIM server diff --git a/internal/api/router.go b/internal/api/router.go index aa6581624..1c3b7c1ec 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -80,6 +80,7 @@ func NewAdminRoute(router gin.IRouter, discov discoveryregistry.SvcDiscoveryRegi adminRouterGroup.POST("/add_user", mw.CheckAdmin, admin.AddUserAccount) // Add user account adminRouterGroup.POST("/del_admin", mw.CheckAdmin, admin.DelAdminAccount) // Delete admin adminRouterGroup.POST("/search", mw.CheckAdmin, admin.SearchAdminAccount) // Get admin list + //account.POST("/add_notification_account") importGroup := router.Group("/user/import") importGroup.POST("/json", mw.CheckAdminOrNil, admin.ImportUserByJson) diff --git a/internal/rpc/admin/admin.go b/internal/rpc/admin/admin.go index 4e28b980e..d16f28f85 100644 --- a/internal/rpc/admin/admin.go +++ b/internal/rpc/admin/admin.go @@ -16,8 +16,6 @@ package admin import ( "context" - "crypto/md5" - "encoding/hex" "fmt" "github.com/OpenIMSDK/chat/pkg/common/db/cache" "github.com/OpenIMSDK/tools/discoveryregistry" @@ -108,11 +106,11 @@ func (o *adminServer) ChangeAdminPassword(ctx context.Context, req *admin.Change return nil, err } - if user.Password != o.passwordEncryption(req.CurrentPassword) { + if user.Password != req.CurrentPassword { return nil, errs.ErrInternalServer.Wrap("password error") } - if err := o.Database.ChangePassword(ctx, req.UserID, o.passwordEncryption(req.NewPassword)); err != nil { + if err := o.Database.ChangePassword(ctx, req.UserID, req.NewPassword); err != nil { return nil, err } return &admin.ChangeAdminPasswordResp{}, nil @@ -284,11 +282,6 @@ func (o *adminServer) genUserID() string { return string(data) } -func (o *adminServer) passwordEncryption(password string) string { - paswd := md5.Sum([]byte(password)) - return hex.EncodeToString(paswd[:]) -} - func (o *adminServer) CheckSuperAdmin(ctx context.Context) error { userID, err := mctx.CheckAdmin(ctx) if err != nil { diff --git a/pkg/common/chatrpcstart/start.go b/pkg/common/chatrpcstart/start.go index f06cf1208..7c93cd0c8 100644 --- a/pkg/common/chatrpcstart/start.go +++ b/pkg/common/chatrpcstart/start.go @@ -40,7 +40,7 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username, config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10), openKeeper.WithLogger(log.NewZkLogger()))*/if err != nil { - return errs.Wrap(err) + return errs.Wrap(err, fmt.Sprintf(";the addr is:%v", &config.Config.Zookeeper.ZkAddr)) } // defer zkClient.CloseZK() defer zkClient.Close() diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index c75817589..d73ec740d 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -108,6 +108,7 @@ var Config struct { } `yaml:"verifyCode"` ProxyHeader string `yaml:"proxyHeader"` AdminList []Admin `yaml:"adminList"` + ChatAdmin []Admin `yaml:"chatAdmin"` } type Admin struct { diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 8e7b3f6ba..aa7122b57 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -115,6 +115,18 @@ func GetIMAdmin(chatAdminID string) string { return admin.ImAdminID } } + if id := getChatAdmin(chatAdminID); id != "" { + return id + } + return "" +} + +func getChatAdmin(chatAdminID string) string { + for _, admin := range Config.ChatAdmin { + if admin.ImAdminID == chatAdminID { + return admin.ImAdminID + } + } return "" } diff --git a/pkg/common/db/model/admin/admin.go b/pkg/common/db/model/admin/admin.go index 03d7dd152..570769c69 100644 --- a/pkg/common/db/model/admin/admin.go +++ b/pkg/common/db/model/admin/admin.go @@ -91,26 +91,41 @@ func (o *Admin) InitAdmin(ctx context.Context) error { log.ZInfo(ctx, "AdminList is empty", "adminList", config.Config.AdminList) return nil } - now := time.Now() - admins := make([]*admin.Admin, 0, len(config.Config.AdminList)) - for _, adminChat := range config.Config.AdminList { - password := md5.Sum([]byte(adminChat.AdminID)) + + admins := make([]*admin.Admin, 0, len(config.Config.AdminList)+len(config.Config.ChatAdmin)) + o.createAdmins(&admins, config.Config.AdminList, true) + o.createAdmins(&admins, config.Config.ChatAdmin, false) + + if err := o.db.WithContext(ctx).Create(&admins).Error; err != nil { + return errs.Wrap(err) + } + return nil +} + +func (o *Admin) createAdmins(adminList *[]*admin.Admin, registerList []config.Admin, flag bool) { + // chatAdmin set the level to 50, this account use for send notification. + level := int32(50) + if flag { + level = 100 + } + for _, adminChat := range registerList { table := admin.Admin{ Account: adminChat.AdminID, UserID: adminChat.ImAdminID, - Password: hex.EncodeToString(password[:]), - Level: 100, - CreateTime: now, + Password: o.passwordEncryption(adminChat.AdminID), + Level: level, + CreateTime: time.Now(), } if adminChat.NickName != "" { table.Nickname = adminChat.NickName } else { table.Nickname = adminChat.AdminID } - admins = append(admins, &table) - } - if err := o.db.WithContext(ctx).Create(&admins).Error; err != nil { - return errs.Wrap(err) + *adminList = append(*adminList, &table) } - return nil +} + +func (o *Admin) passwordEncryption(password string) string { + paswd := md5.Sum([]byte(password)) + return hex.EncodeToString(paswd[:]) } diff --git a/pkg/common/db/table/admin/admin.go b/pkg/common/db/table/admin/admin.go index 2d4b07fb9..377db4cc6 100644 --- a/pkg/common/db/table/admin/admin.go +++ b/pkg/common/db/table/admin/admin.go @@ -26,7 +26,7 @@ type Admin struct { FaceURL string `gorm:"column:face_url;type:varchar(255)"` Nickname string `gorm:"column:nickname;type:varchar(64)"` UserID string `gorm:"column:user_id;type:varchar(64)"` // openIM userID - Level int32 `gorm:"column:level;default:1" ` + Level int32 `gorm:"column:level;default:1"` CreateTime time.Time `gorm:"column:create_time"` }