Skip to content

Commit

Permalink
feat: add notification config (#347)
Browse files Browse the repository at this point in the history
* feat: add some api about admin

* feat: add some api about admin

* fix: del the unuse method

* fix: Optimized code

* fix: fix the userID

* fix: del the config code getting from zk

* fix: fix the initRedis

* fix: add the get Evn method in Config

* fix: fix the config.yaml.template

* fix: fix the env config

* feat: add some conponent env

* fix: fix the openIMURL

* fix: find the zk don't connect error

* fix: find the zk error

* fix: test the jz error

* fix: delt the log

* fix: fix the component error

* fix: fix the error of get env  location

* fix: fix the error

* fix: fix the zk start error

* fix: fix the Config

* fix: testing

* fix: fix the config.Env error

* fix: fix the componentCheck

* fix: fix the config

* fix: find the error

* fix: del the flag

* fix: fix the error

* fix: fix the password error

* fix: del the proxyHeader field

* fix: fix the openIMurl

* feat: add chatAdmin
  • Loading branch information
luhaoling committed Dec 25, 2023
1 parent 2e3d72a commit add0c63
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 25 deletions.
9 changes: 7 additions & 2 deletions deployments/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions internal/rpc/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/chatrpcstart/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ var Config struct {
} `yaml:"verifyCode"`
ProxyHeader string `yaml:"proxyHeader"`
AdminList []Admin `yaml:"adminList"`
ChatAdmin []Admin `yaml:"chatAdmin"`
}

type Admin struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand Down
39 changes: 27 additions & 12 deletions pkg/common/db/model/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
}
2 changes: 1 addition & 1 deletion pkg/common/db/table/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

0 comments on commit add0c63

Please sign in to comment.