Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change account logic and add allowRegister config #583

Merged
merged 14 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions .github/workflows/docker-buildx.bak
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright © 2023 OpenIM open source community. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: Docker Buildx Images CI

on:
Expand Down Expand Up @@ -40,11 +26,12 @@ jobs:
install: true

- name: Cache Docker layers
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx-
restore-keys: |
${{ runner.os }}-buildx-

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
Expand Down
2 changes: 2 additions & 0 deletions config/chat-rpc-chat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ liveKit:
url: "ws://127.0.0.1:7880" # LIVEKIT_URL, LiveKit server address and port
key: "APIGPW3gnFTzqHH"
secret: "23ztfSqsfQ8hKkHzHTl3Z4bvaxro0snjk5jwbp5p6Q3"

allowRegister: true
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/openimsdk/gomake v0.0.14-alpha.5
github.com/openimsdk/protocol v0.0.72
github.com/openimsdk/tools v0.0.50-alpha.15
github.com/openimsdk/tools v0.0.50-alpha.20
github.com/redis/go-redis/v9 v9.5.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ github.com/openimsdk/gomake v0.0.14-alpha.5 h1:VY9c5x515lTfmdhhPjMvR3BBRrRquAUCF
github.com/openimsdk/gomake v0.0.14-alpha.5/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI=
github.com/openimsdk/protocol v0.0.72 h1:K+vslwaR7lDXyBzb07UuEQITaqsgighz7NyXVIWsu6A=
github.com/openimsdk/protocol v0.0.72/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/tools v0.0.50-alpha.15 h1:HV9aKZ4vvCZCGG4wFDsgUONkkdJeCcrFNn3BT52nUVQ=
github.com/openimsdk/tools v0.0.50-alpha.15/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4=
github.com/openimsdk/tools v0.0.50-alpha.20 h1:TUCZOwaea983Qj1MCbiGZUBlwBkTMwTF9SLS0WQcsJs=
github.com/openimsdk/tools v0.0.50-alpha.20/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pion/datachannel v1.5.5 h1:10ef4kwdjije+M9d7Xm9im2Y3O6A6ccQb0zcqZcJew8=
Expand Down
31 changes: 16 additions & 15 deletions internal/api/admin/admin.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package admin

import (
Expand Down Expand Up @@ -72,7 +58,7 @@ func (o *Api) AdminLogin(c *gin.Context) {
return
}
imAdminUserID := o.GetDefaultIMAdminUserID()
imToken, err := o.imApiCaller.GetAdminToken(c, imAdminUserID)
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -173,6 +159,12 @@ func (o *Api) AddUserAccount(c *gin.Context) {
return
}

if resp, err := o.adminClient.FindDefaultFriend(c, &admin.FindDefaultFriendReq{}); err == nil {
_ = o.imApiCaller.ImportFriend(c, req.User.UserID, resp.UserIDs)
}
if resp, err := o.adminClient.FindDefaultGroup(c, &admin.FindDefaultGroupReq{}); err == nil {
_ = o.imApiCaller.InviteToGroup(c, req.User.UserID, resp.GroupIDs)
}
apiresp.GinSuccess(c, nil)

}
Expand Down Expand Up @@ -547,6 +539,7 @@ func (o *Api) registerChatUser(ctx context.Context, ip string, users []*chat.Reg
if err = o.imApiCaller.RegisterUser(ctx, []*sdkws.UserInfo{userInfo}); err != nil {
return err
}

if resp, err := o.adminClient.FindDefaultFriend(ctx, &admin.FindDefaultFriendReq{}); err == nil {
_ = o.imApiCaller.ImportFriend(ctx, respRegisterUser.UserID, resp.UserIDs)
}
Expand All @@ -571,3 +564,11 @@ func (o *Api) BatchImportTemplate(c *gin.Context) {
c.Header("ETag", md5Val)
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", config.ImportTemplate)
}

func (o *Api) SetAllowRegister(c *gin.Context) {
a2r.Call(chat.ChatClient.SetAllowRegister, o.chatClient, c)
}

func (o *Api) GetAllowRegister(c *gin.Context) {
a2r.Call(chat.ChatClient.GetAllowRegister, o.chatClient, c)
}
4 changes: 4 additions & 0 deletions internal/api/admin/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func SetAdminRoute(router gin.IRouter, admin *Api, mw *chatmw.MW) {
importGroup.POST("/xlsx", mw.CheckAdmin, admin.ImportUserByXlsx)
importGroup.GET("/xlsx", admin.BatchImportTemplate)

allowRegisterGroup := router.Group("/user/allow_register", mw.CheckAdmin)
allowRegisterGroup.POST("/get", admin.GetAllowRegister)
allowRegisterGroup.POST("/set", admin.SetAllowRegister)

defaultRouter := router.Group("/default", mw.CheckAdmin)
defaultUserRouter := defaultRouter.Group("/user")
defaultUserRouter.POST("/add", admin.AddDefaultFriend) // Add default friend at registration
Expand Down
2 changes: 1 addition & 1 deletion internal/rpc/chat/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (o *chatSvr) OpenIMCallback(ctx context.Context, req *chat.OpenIMCallbackRe
if err := json.Unmarshal([]byte(req.Body), &data); err != nil {
return nil, errs.Wrap(err)
}
user, err := o.Database.GetAttribute(ctx, data.ToUserID)
user, err := o.Database.TakeAttributeByUserID(ctx, data.ToUserID)
if err != nil {
return nil, err
}
Expand Down
136 changes: 57 additions & 79 deletions internal/rpc/chat/login.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package chat

import (
Expand Down Expand Up @@ -50,7 +36,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
if req.AreaCode == "" || req.PhoneNumber == "" {
return nil, errs.ErrArgs.WrapMsg("area code or phone number is empty")
}
if req.AreaCode[0] != '+' {
if !strings.HasPrefix(req.AreaCode, "+") {
req.AreaCode = "+" + req.AreaCode
}
if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil {
Expand All @@ -59,22 +45,10 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe
if _, err := strconv.ParseUint(req.PhoneNumber, 10, 64); err != nil {
return nil, errs.ErrArgs.WrapMsg("phone number must be number")
}
_, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if err == nil {
return nil, eerrs.ErrPhoneAlreadyRegister.WrapMsg("phone already register")
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
} else {
if err := chat.EmailCheck(req.Email); err != nil {
return nil, errs.ErrArgs.WrapMsg("email must be right")
}
_, err := o.Database.TakeAttributeByEmail(ctx, req.Email)
if err == nil {
return nil, eerrs.ErrEmailAlreadyRegister.WrapMsg("email already register")
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
}
conf, err := o.Admin.GetConfig(ctx)
if err != nil {
Expand Down Expand Up @@ -245,16 +219,14 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
if err != nil {
return nil, err
}
if req.User == nil {
return nil, errs.ErrArgs.WrapMsg("user is nil")
}
if req.User.Email == "" {
if (req.User.AreaCode == "" && req.User.PhoneNumber != "") || (req.User.AreaCode != "" && req.User.PhoneNumber == "") {
return nil, errs.ErrArgs.WrapMsg("area code or phone number error, no email provide")
}
if err = o.checkRegisterInfo(ctx, req.User, isAdmin); err != nil {
return nil, err
}
var usedInvitationCode bool
if !isAdmin {
if !o.AllowRegister {
return nil, errs.ErrNoPermission.WrapMsg("register user is disabled")
}
if req.User.UserID != "" {
return nil, errs.ErrNoPermission.WrapMsg("only admin can set user id")
}
Expand Down Expand Up @@ -308,43 +280,39 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
return nil, err
}
}
var registerType int32
var (
credentials []*chatdb.Credential
registerType int32
)

if req.User.PhoneNumber != "" {
if req.User.AreaCode[0] != '+' {
req.User.AreaCode = "+" + req.User.AreaCode
}
if _, err := strconv.ParseUint(req.User.AreaCode[1:], 10, 64); err != nil {
return nil, errs.ErrArgs.WrapMsg("area code must be number")
}
if _, err := strconv.ParseUint(req.User.PhoneNumber, 10, 64); err != nil {
return nil, errs.ErrArgs.WrapMsg("phone number must be number")
}
_, err := o.Database.TakeAttributeByPhone(ctx, req.User.AreaCode, req.User.PhoneNumber)
if err == nil {
return nil, eerrs.ErrPhoneAlreadyRegister.Wrap()
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
registerType = constant.PhoneRegister
credentials = append(credentials, &chatdb.Credential{
UserID: req.User.UserID,
Account: BuildCredentialPhone(req.User.AreaCode, req.User.PhoneNumber),
Type: constant.CredentialPhone,
AllowChange: true,
})
}

if req.User.Account != "" {
_, err := o.Database.TakeAttributeByAccount(ctx, req.User.Account)
if err == nil {
return nil, eerrs.ErrAccountAlreadyRegister.Wrap()
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
credentials = append(credentials, &chatdb.Credential{
UserID: req.User.UserID,
Account: req.User.Account,
Type: constant.CredentialAccount,
AllowChange: true,
})
registerType = constant.AccountRegister
}

if req.User.Email != "" {
_, err := o.Database.TakeAttributeByEmail(ctx, req.User.Email)
registerType = constant.EmailRegister
if err == nil {
return nil, eerrs.ErrEmailAlreadyRegister.Wrap()
} else if !dbutil.IsDBNotFound(err) {
return nil, err
}
credentials = append(credentials, &chatdb.Credential{
UserID: req.User.UserID,
Account: req.User.Email,
Type: constant.CredentialEmail,
AllowChange: true,
})
}
register := &chatdb.Register{
UserID: req.User.UserID,
Expand All @@ -362,6 +330,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
ChangeTime: register.CreateTime,
CreateTime: register.CreateTime,
}

attribute := &chatdb.Attribute{
UserID: req.User.UserID,
Account: req.User.Account,
Expand All @@ -379,7 +348,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) (
AllowAddFriend: constant.DefaultAllowAddFriend,
RegisterType: registerType,
}
if err := o.Database.RegisterUser(ctx, register, account, attribute); err != nil {
if err := o.Database.RegisterUser(ctx, register, account, attribute, credentials); err != nil {
return nil, err
}
if usedInvitationCode {
Expand All @@ -405,33 +374,42 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
if req.Password == "" && req.VerifyCode == "" {
return nil, errs.ErrArgs.WrapMsg("password or code must be set")
}
var err error
var attribute *chatdb.Attribute
if req.Account != "" {
attribute, err = o.Database.GetAttributeByAccount(ctx, req.Account)
} else if req.PhoneNumber != "" {
if req.Password == "" {
return nil, errs.ErrArgs.WrapMsg("password must be set")
}
var (
err error
credential *chatdb.Credential
acc string
)

switch {
case req.Account != "":
acc = req.Account
case req.PhoneNumber != "":
if req.AreaCode == "" {
return nil, errs.ErrArgs.WrapMsg("area code must")
}
if req.AreaCode[0] != '+' {
if !strings.HasPrefix(req.AreaCode, "+") {
req.AreaCode = "+" + req.AreaCode
}
if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil {
return nil, errs.ErrArgs.WrapMsg("area code must be number")
}
attribute, err = o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
} else if req.Email != "" {
attribute, err = o.Database.GetAttributeByEmail(ctx, req.Email)
} else {
err = errs.ErrArgs.WrapMsg("account or phone number or email must be set")
acc = BuildCredentialPhone(req.AreaCode, req.PhoneNumber)
case req.Email != "":
acc = req.Email
default:
return nil, errs.ErrArgs.WrapMsg("account or phone number or email must be set")
}
credential, err = o.Database.TakeCredentialByAccount(ctx, acc)
if err != nil {
if dbutil.IsDBNotFound(err) {
return nil, eerrs.ErrAccountNotFound.WrapMsg("user unregistered")
}
return nil, err
}
if err := o.Admin.CheckLogin(ctx, attribute.UserID, req.Ip); err != nil {
if err := o.Admin.CheckLogin(ctx, credential.UserID, req.Ip); err != nil {
return nil, err
}
var verifyCodeID *string
Expand All @@ -450,20 +428,20 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
verifyCodeID = &id
}
} else {
account, err := o.Database.GetAccount(ctx, attribute.UserID)
account, err := o.Database.TakeAccount(ctx, credential.UserID)
if err != nil {
return nil, err
}
if account.Password != req.Password {
return nil, eerrs.ErrPassword.Wrap()
}
}
chatToken, err := o.Admin.CreateToken(ctx, attribute.UserID, constant.NormalUser)
chatToken, err := o.Admin.CreateToken(ctx, credential.UserID, constant.NormalUser)
if err != nil {
return nil, err
}
record := &chatdb.UserLoginRecord{
UserID: attribute.UserID,
UserID: credential.UserID,
LoginTime: time.Now(),
IP: req.Ip,
DeviceID: req.DeviceID,
Expand All @@ -477,7 +455,7 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes
return nil, err
}
}
resp.UserID = attribute.UserID
resp.UserID = credential.UserID
resp.ChatToken = chatToken.Token
return resp, nil
}
4 changes: 2 additions & 2 deletions internal/rpc/chat/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ func (o *chatSvr) ResetPassword(ctx context.Context, req *chat.ResetPasswordReq)
}

if req.Email == "" {
attribute, err := o.Database.GetAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
attribute, err := o.Database.TakeAttributeByPhone(ctx, req.AreaCode, req.PhoneNumber)
if err != nil {
return nil, err
}
err = o.Database.UpdatePasswordAndDeleteVerifyCode(ctx, attribute.UserID, req.Password, verifyCodeID)
} else {
attribute, err := o.Database.GetAttributeByEmail(ctx, req.Email)
attribute, err := o.Database.TakeAttributeByEmail(ctx, req.Email)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading