mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
用户表增加注册IP 登陆IP
1、用户表增加注册IP 登陆IP 2、新增ip_limit表、user_ip_limit表 3、用户创建需要增加createIp参数同步 用户登录需要增加loginIp参数同步 login_limit 限制说明 0:读取ip_limits表 限制用户注册+用户登陆IP 1:读取user_ip_limits表 限制用户在指定IP登陆 2:读取black_lists表 限制用户在限制时间不能登陆
This commit is contained in:
parent
600c5243dd
commit
9358aa1bd8
@ -3,6 +3,7 @@ package apiAuth
|
||||
import (
|
||||
api "Open_IM/pkg/base_info"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||
@ -10,10 +11,11 @@ import (
|
||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Summary 用户注册
|
||||
@ -65,11 +67,15 @@ func UserRegister(c *gin.Context) {
|
||||
if reply.CommonResp.ErrCode != 0 {
|
||||
errMsg := req.OperationID + " " + " UserRegister failed " + reply.CommonResp.ErrMsg + req.String()
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
if reply.CommonResp.ErrCode == constant.RegisterLimit {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterLimit, "errMsg": "用户注册被限制"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
pbDataToken := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
|
||||
pbDataToken := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID, LoginIp: params.CreateIp}
|
||||
replyToken, err := client.UserToken(context.Background(), pbDataToken)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + " client.UserToken failed " + err.Error() + pbDataToken.String()
|
||||
@ -110,7 +116,7 @@ func UserToken(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
req := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID}
|
||||
req := &rpc.UserTokenReq{Platform: params.Platform, FromUserID: params.UserID, OperationID: params.OperationID, LoginIp: params.LoginIp}
|
||||
log.NewInfo(req.OperationID, "UserToken args ", req.String())
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName, req.OperationID)
|
||||
if etcdConn == nil {
|
||||
|
@ -10,8 +10,9 @@ import (
|
||||
"Open_IM/pkg/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ParamsLogin struct {
|
||||
@ -62,6 +63,11 @@ func Login(c *gin.Context) {
|
||||
openIMGetUserToken.Platform = params.Platform
|
||||
openIMGetUserToken.Secret = config.Config.Secret
|
||||
openIMGetUserToken.UserID = userID
|
||||
loginIp := c.Request.Header.Get("X-Forward-For")
|
||||
if loginIp == "" {
|
||||
loginIp = c.ClientIP()
|
||||
}
|
||||
openIMGetUserToken.LoginIp = loginIp
|
||||
openIMGetUserTokenResp := api.UserTokenResp{}
|
||||
bMsg, err := http2.Post(url, openIMGetUserToken, 2)
|
||||
if err != nil {
|
||||
@ -72,7 +78,11 @@ func Login(c *gin.Context) {
|
||||
err = json.Unmarshal(bMsg, &openIMGetUserTokenResp)
|
||||
if err != nil || openIMGetUserTokenResp.ErrCode != 0 {
|
||||
log.NewError(params.OperationID, "request get user token", account, "err", "")
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": ""})
|
||||
if openIMGetUserTokenResp.ErrCode == constant.LoginLimit {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.LoginLimit, "errMsg": "用户登录被限制"})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": ""})
|
||||
}
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMGetUserTokenResp.UserToken})
|
||||
|
@ -10,11 +10,12 @@ import (
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/utils"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ParamsSetPassword struct {
|
||||
@ -81,6 +82,11 @@ func SetPassword(c *gin.Context) {
|
||||
openIMRegisterReq.Nickname = params.Nickname
|
||||
openIMRegisterReq.Secret = config.Config.Secret
|
||||
openIMRegisterReq.FaceURL = params.FaceURL
|
||||
createIp := c.Request.Header.Get("X-Forward-For")
|
||||
if createIp == "" {
|
||||
createIp = c.ClientIP()
|
||||
}
|
||||
openIMRegisterReq.CreateIp = createIp
|
||||
openIMRegisterResp := api.UserRegisterResp{}
|
||||
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "register req:", openIMRegisterReq)
|
||||
bMsg, err := http2.Post(url, openIMRegisterReq, 2)
|
||||
@ -95,7 +101,11 @@ func SetPassword(c *gin.Context) {
|
||||
if err != nil {
|
||||
log.NewError(params.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: " + openIMRegisterResp.ErrMsg})
|
||||
if openIMRegisterResp.ErrCode == constant.RegisterLimit {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterLimit, "errMsg": "用户注册被限制"})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: " + openIMRegisterResp.ErrMsg})
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Info(params.OperationID, "begin store mysql", account, params.Password, "info", params.FaceURL, params.Nickname)
|
||||
|
@ -15,6 +15,7 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"Open_IM/pkg/common/config"
|
||||
|
||||
@ -29,6 +30,13 @@ func (rpc *rpcAuth) UserRegister(_ context.Context, req *pbAuth.UserRegisterReq)
|
||||
user.Birth = utils.UnixSecondToTime(int64(req.UserInfo.Birth))
|
||||
}
|
||||
log.Debug(req.OperationID, "copy ", user, req.UserInfo)
|
||||
Limited, LimitError := imdb.IsLimitRegisterIp(req.UserInfo.CreateIp)
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserRegisterResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.RegisterLimit, ErrMsg: "Register Limit"}}, nil
|
||||
}
|
||||
err := imdb.UserRegister(user)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " imdb.UserRegister failed " + err.Error() + user.UserID
|
||||
@ -42,20 +50,43 @@ func (rpc *rpcAuth) UserRegister(_ context.Context, req *pbAuth.UserRegisterReq)
|
||||
|
||||
func (rpc *rpcAuth) UserToken(_ context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||
_, err := imdb.GetUserByUserID(req.FromUserID)
|
||||
user, err := imdb.GetUserByUserID(req.FromUserID)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " imdb.GetUserByUserID failed " + err.Error() + req.FromUserID
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
||||
}
|
||||
|
||||
var Limited bool
|
||||
var LimitError error
|
||||
if user.LoginLimit == 0 {
|
||||
Limited, LimitError = imdb.IsLimitLoginIp(req.LoginIp)
|
||||
} else if user.LoginLimit == 1 {
|
||||
Limited, LimitError = imdb.IsLimitUserLoginIp(user.UserID, req.LoginIp)
|
||||
} else if user.LoginLimit == 2 {
|
||||
Limited, LimitError = imdb.UserIsBlock(user.UserID)
|
||||
}
|
||||
if LimitError != nil {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: LimitError.Error()}}, nil
|
||||
}
|
||||
if Limited {
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.LoginLimit, ErrMsg: "用户被限制"}}, nil
|
||||
}
|
||||
tokens, expTime, err := token_verify.CreateToken(req.FromUserID, int(req.Platform))
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " token_verify.CreateToken failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
||||
}
|
||||
|
||||
//增加用户登录信息
|
||||
user.LoginTimes = user.LoginTimes + 1
|
||||
user.LastLoginIp = req.LoginIp
|
||||
user.LastLoginTime = time.Now()
|
||||
err = imdb.UpdateUserInfo(*user)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " imdb.UpdateUserInfo failed " + err.Error() + req.FromUserID
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime})
|
||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime}, nil
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db"
|
||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||
"Open_IM/pkg/common/db/rocks_cache"
|
||||
rocksCache "Open_IM/pkg/common/db/rocks_cache"
|
||||
errors "Open_IM/pkg/common/http"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
@ -539,11 +539,20 @@ func (s *userServer) GetUsersByName(ctx context.Context, req *pbUser.GetUsersByN
|
||||
continue
|
||||
}
|
||||
resp.Users = append(resp.Users, &pbUser.User{
|
||||
ProfilePhoto: user.FaceURL,
|
||||
Nickname: user.Nickname,
|
||||
UserId: user.UserID,
|
||||
CreateTime: user.CreateTime.String(),
|
||||
IsBlock: isBlock,
|
||||
ProfilePhoto: user.FaceURL,
|
||||
Nickname: user.Nickname,
|
||||
UserId: user.UserID,
|
||||
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
CreateIp: user.CreateIp,
|
||||
IsBlock: isBlock,
|
||||
Birth: user.Birth.Format("2006-01-02"),
|
||||
PhoneNumber: user.PhoneNumber,
|
||||
Email: user.Email,
|
||||
LastLoginIp: user.LastLoginIp,
|
||||
LastLoginTime: user.LastLoginTime.Format("2006-01-02 15:04:05"),
|
||||
LoginTimes: user.LoginTimes,
|
||||
Gender: user.Gender,
|
||||
LoginLimit: user.LoginLimit,
|
||||
})
|
||||
}
|
||||
user := db.User{Nickname: req.UserName}
|
||||
@ -575,11 +584,20 @@ func (s *userServer) GetUserById(ctx context.Context, req *pbUser.GetUserByIdReq
|
||||
return resp, errors.WrapError(constant.ErrDB)
|
||||
}
|
||||
resp.User = &pbUser.User{
|
||||
ProfilePhoto: user.FaceURL,
|
||||
Nickname: user.Nickname,
|
||||
UserId: user.UserID,
|
||||
CreateTime: user.CreateTime.String(),
|
||||
IsBlock: isBlock,
|
||||
ProfilePhoto: user.FaceURL,
|
||||
Nickname: user.Nickname,
|
||||
UserId: user.UserID,
|
||||
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
CreateIp: user.CreateIp,
|
||||
IsBlock: isBlock,
|
||||
Birth: user.Birth.Format("2006-01-02"),
|
||||
PhoneNumber: user.PhoneNumber,
|
||||
Email: user.Email,
|
||||
LastLoginIp: user.LastLoginIp,
|
||||
LastLoginTime: user.LastLoginTime.Format("2006-01-02 15:04:05"),
|
||||
LoginTimes: user.LoginTimes,
|
||||
Gender: user.Gender,
|
||||
LoginLimit: user.LoginLimit,
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
||||
return resp, nil
|
||||
@ -597,11 +615,20 @@ func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pb
|
||||
isBlock, err := imdb.UserIsBlock(v.UserID)
|
||||
if err == nil {
|
||||
user := &pbUser.User{
|
||||
ProfilePhoto: v.FaceURL,
|
||||
UserId: v.UserID,
|
||||
CreateTime: v.CreateTime.String(),
|
||||
Nickname: v.Nickname,
|
||||
IsBlock: isBlock,
|
||||
ProfilePhoto: v.FaceURL,
|
||||
UserId: v.UserID,
|
||||
CreateTime: v.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
CreateIp: v.CreateIp,
|
||||
Nickname: v.Nickname,
|
||||
Birth: v.Birth.Format("2006-01-02"),
|
||||
PhoneNumber: v.PhoneNumber,
|
||||
Email: v.Email,
|
||||
IsBlock: isBlock,
|
||||
LastLoginIp: v.LastLoginIp,
|
||||
LastLoginTime: v.LastLoginTime.Format("2006-01-02 15:04:05"),
|
||||
LoginTimes: v.LoginTimes,
|
||||
Gender: v.Gender,
|
||||
LoginLimit: v.LoginLimit,
|
||||
}
|
||||
resp.User = append(resp.User, user)
|
||||
} else {
|
||||
|
@ -30,6 +30,7 @@ type UserTokenReq struct {
|
||||
Secret string `json:"secret" binding:"required,max=32"`
|
||||
Platform int32 `json:"platform" binding:"required,min=1,max=8"`
|
||||
UserID string `json:"userID" binding:"required,min=1,max=64"`
|
||||
LoginIp string `json:"loginIp" binding:"required,max=15"`
|
||||
OperationID string `json:"operationID" binding:"required"`
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,26 @@
|
||||
package base_info
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiUserInfo struct {
|
||||
UserID string `json:"userID" binding:"required,min=1,max=64" swaggo:"true,用户ID,"`
|
||||
Nickname string `json:"nickname" binding:"omitempty,min=1,max=64" swaggo:"true,my id,19"`
|
||||
FaceURL string `json:"faceURL" binding:"omitempty,max=1024"`
|
||||
Gender int32 `json:"gender" binding:"omitempty,oneof=0 1 2"`
|
||||
PhoneNumber string `json:"phoneNumber" binding:"omitempty,max=32"`
|
||||
Birth uint32 `json:"birth" binding:"omitempty"`
|
||||
Email string `json:"email" binding:"omitempty,max=64"`
|
||||
Ex string `json:"ex" binding:"omitempty,max=1024"`
|
||||
UserID string `json:"userID" binding:"required,min=1,max=64" swaggo:"true,用户ID,"`
|
||||
Nickname string `json:"nickname" binding:"omitempty,min=1,max=64" swaggo:"true,my id,19"`
|
||||
FaceURL string `json:"faceURL" binding:"omitempty,max=1024"`
|
||||
Gender int32 `json:"gender" binding:"omitempty,oneof=0 1 2"`
|
||||
PhoneNumber string `json:"phoneNumber" binding:"omitempty,max=32"`
|
||||
Birth uint32 `json:"birth" binding:"omitempty"`
|
||||
Email string `json:"email" binding:"omitempty,max=64"`
|
||||
CreateIp string `json:"createIp" binding:"omitempty,max=15"`
|
||||
CreateTime int64 `json:"createTime"`
|
||||
LastLoginIp string `json:"LastLoginIp" binding:"omitempty,max=15"`
|
||||
LastLoginTime int64 `json:"lastLoginTime"`
|
||||
LoginTimes int32 `json:"loginTimes" binding:"omitempty"`
|
||||
LoginLimit int32 `json:"loginLimit" binding:"omitempty"`
|
||||
Ex string `json:"ex" binding:"omitempty,max=1024"`
|
||||
}
|
||||
|
||||
//type Conversation struct {
|
||||
|
@ -1,11 +1,20 @@
|
||||
package cms_api_struct
|
||||
|
||||
type UserResponse struct {
|
||||
ProfilePhoto string `json:"profile_photo"`
|
||||
Nickname string `json:"nick_name"`
|
||||
UserId string `json:"user_id"`
|
||||
CreateTime string `json:"create_time,omitempty"`
|
||||
IsBlock bool `json:"is_block"`
|
||||
ProfilePhoto string `json:"profile_photo"`
|
||||
Nickname string `json:"nick_name"`
|
||||
UserId string `json:"user_id"`
|
||||
CreateTime string `json:"create_time,omitempty"`
|
||||
CreateIp string `json:"create_ip,omitempty"`
|
||||
LastLoginTime string `json:"last_login_time,omitempty"`
|
||||
LastLoginIp string `json:"last_login_ip,omitempty"`
|
||||
LoginTimes int32 `json:"login_times"`
|
||||
LoginLimit int32 `json:"login_limit"`
|
||||
IsBlock bool `json:"is_block"`
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
Email string `json:"email"`
|
||||
Birth string `json:"birth"`
|
||||
Gender int `json:"gender"`
|
||||
}
|
||||
|
||||
type GetUserRequest struct {
|
||||
|
@ -67,6 +67,8 @@ const (
|
||||
CodeInvalidOrExpired = 10009
|
||||
RegisterFailed = 10010
|
||||
ResetPasswordFailed = 10011
|
||||
RegisterLimit = 10012
|
||||
LoginLimit = 10013
|
||||
DatabaseError = 10002
|
||||
ServerError = 10004
|
||||
HttpError = 10005
|
||||
|
@ -153,8 +153,9 @@ type GroupRequest struct {
|
||||
//string Birth = 6;
|
||||
//string Email = 7;
|
||||
//string Ex = 8;
|
||||
//int64 CreateTime = 9;
|
||||
//int32 AppMangerLevel = 10;
|
||||
//string CreateIp = 9;
|
||||
//int64 CreateTime = 10;
|
||||
//int32 AppMangerLevel = 11;
|
||||
//open_im_sdk.User == imdb.User
|
||||
type User struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
@ -166,11 +167,30 @@ type User struct {
|
||||
Email string `gorm:"column:email;size:64"`
|
||||
Ex string `gorm:"column:ex;size:1024"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
CreateIp string `gorm:"column:create_ip;size:15"`
|
||||
LastLoginTime time.Time `gorm:"column:last_login_time"`
|
||||
LastLoginIp string `gorm:"column:last_login_ip;size:15"`
|
||||
LoginTimes int32 `gorm:"column:login_times"`
|
||||
LoginLimit int32 `gorm:"column:login_limit"`
|
||||
AppMangerLevel int32 `gorm:"column:app_manger_level"`
|
||||
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"`
|
||||
status int32 `gorm:"column:status"`
|
||||
}
|
||||
|
||||
type IpLimit struct {
|
||||
Ip string `gorm:"column:ip;primary_key;size:15"`
|
||||
LimitRegister int32 `gorm:"column:limit_register;size:1"`
|
||||
LimitLogin int32 `gorm:"column:limit_login;size:1"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
LimitTime time.Time `gorm:"column:limit_time"`
|
||||
}
|
||||
|
||||
type UserIpLimit struct {
|
||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||
Ip string `gorm:"column:ip;primary_key;size:15"`
|
||||
CreateTime time.Time `gorm:"column:create_time"`
|
||||
}
|
||||
|
||||
//message BlackInfo{
|
||||
//string OwnerUserID = 1;
|
||||
//int64 CreateTime = 2;
|
||||
|
@ -2,14 +2,13 @@ package db
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type mysqlDB struct {
|
||||
@ -79,7 +78,7 @@ func initMysqlDB() {
|
||||
&GroupMember{},
|
||||
&GroupRequest{},
|
||||
&User{},
|
||||
&Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}, &Department{})
|
||||
&Black{}, &ChatLog{}, &Register{}, &Conversation{}, &AppVersion{}, &Department{}, &BlackList{}, &IpLimit{}, &UserIpLimit{})
|
||||
db.Set("gorm:table_options", "CHARSET=utf8")
|
||||
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
||||
|
||||
@ -143,6 +142,18 @@ func initMysqlDB() {
|
||||
fmt.Println("CreateTable DepartmentMember")
|
||||
db.Migrator().CreateTable(&AppVersion{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&BlackList{}) {
|
||||
fmt.Println("CreateTable BlackList")
|
||||
db.Migrator().CreateTable(&BlackList{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&IpLimit{}) {
|
||||
fmt.Println("CreateTable IpLimit")
|
||||
db.Migrator().CreateTable(&IpLimit{})
|
||||
}
|
||||
if !db.Migrator().HasTable(&UserIpLimit{}) {
|
||||
fmt.Println("CreateTable UserIpLimit")
|
||||
db.Migrator().CreateTable(&UserIpLimit{})
|
||||
}
|
||||
DB.MysqlDB.db = db
|
||||
return
|
||||
}
|
||||
|
@ -42,6 +42,9 @@ func UserRegister(user db.User) error {
|
||||
if user.Birth.Unix() < 0 {
|
||||
user.Birth = utils.UnixSecondToTime(0)
|
||||
}
|
||||
user.LastLoginTime = time.Now()
|
||||
user.LoginTimes = 0
|
||||
user.LastLoginIp = user.CreateIp
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Create(&user).Error
|
||||
if err != nil {
|
||||
return err
|
||||
@ -130,7 +133,7 @@ func UserIsBlock(userId string) (bool, error) {
|
||||
var user db.BlackList
|
||||
rows := db.DB.MysqlDB.DefaultGormDB().Table("black_lists").Where("uid=?", userId).First(&user).RowsAffected
|
||||
if rows >= 1 {
|
||||
return true, nil
|
||||
return user.EndDisableTime.After(time.Now()), nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@ -151,6 +154,9 @@ func BlockUser(userId, endDisableTime string) error {
|
||||
db.DB.MysqlDB.DefaultGormDB().Table("black_lists").Where("uid=?", userId).First(&blockUser)
|
||||
if blockUser.UserId != "" {
|
||||
db.DB.MysqlDB.DefaultGormDB().Model(&blockUser).Where("uid=?", blockUser.UserId).Update("end_disable_time", end)
|
||||
if user.LoginLimit != 2 {
|
||||
db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", blockUser.UserId).Update("login_limit", 2)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
blockUser = db.BlackList{
|
||||
@ -159,11 +165,20 @@ func BlockUser(userId, endDisableTime string) error {
|
||||
EndDisableTime: end,
|
||||
}
|
||||
result := db.DB.MysqlDB.DefaultGormDB().Create(&blockUser)
|
||||
if result.Error == nil {
|
||||
if user.LoginLimit != 2 {
|
||||
db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", blockUser.UserId).Update("login_limit", 2)
|
||||
}
|
||||
}
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func UnBlockUser(userId string) error {
|
||||
return db.DB.MysqlDB.DefaultGormDB().Where("uid=?", userId).Delete(&db.BlackList{}).Error
|
||||
err := db.DB.MysqlDB.DefaultGormDB().Where("uid=?", userId).Delete(&db.BlackList{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.DB.MysqlDB.DefaultGormDB().Table("users").Where("user_id=?", userId).Update("login_limit", 0).Error
|
||||
}
|
||||
|
||||
type BlockUserInfo struct {
|
||||
@ -238,3 +253,30 @@ func GetBlockUsersNumCount() (int32, error) {
|
||||
}
|
||||
return int32(count), nil
|
||||
}
|
||||
|
||||
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_register=? and limit_time>now()", RegisterIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitLoginIp(LoginIp string) (bool, error) {
|
||||
//如果已经存在则限制
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("ip_limits").Where("ip=? and limit_login=? and limit_time>now()", LoginIp, 1).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func IsLimitUserLoginIp(userID string, LoginIp string) (bool, error) {
|
||||
//如果已经存在则放行
|
||||
var count int64
|
||||
if err := db.DB.MysqlDB.DefaultGormDB().Table("user_ip_limits").Where("ip=? and user_id=?", LoginIp, userID).Count(&count).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return count == 0, nil
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -22,6 +22,7 @@ message UserTokenReq {
|
||||
string FromUserID = 2;
|
||||
string OpUserID = 3;
|
||||
string OperationID = 4;
|
||||
string LoginIp = 5;
|
||||
}
|
||||
message UserTokenResp {
|
||||
CommonResp CommonResp = 1;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -72,9 +72,14 @@ message UserInfo{
|
||||
uint32 birth = 6;
|
||||
string email = 7;
|
||||
string ex = 8;
|
||||
uint32 createTime = 9;
|
||||
int32 appMangerLevel = 10;
|
||||
int32 globalRecvMsgOpt = 11;
|
||||
string createIp = 9;
|
||||
uint32 createTime = 10;
|
||||
string LastLoginIp =11;
|
||||
uint32 LastLoginTime = 12;
|
||||
int32 LoginTimes = 13;
|
||||
int32 LoginLimit = 14;
|
||||
int32 appMangerLevel = 15;
|
||||
int32 globalRecvMsgOpt = 16;
|
||||
}
|
||||
|
||||
message FriendInfo{
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -178,7 +178,16 @@ message User{
|
||||
string Nickname = 2;
|
||||
string UserId = 3;
|
||||
string CreateTime = 4;
|
||||
bool IsBlock = 5;
|
||||
string PhoneNumber = 5;
|
||||
string Email = 6;
|
||||
string Birth = 7;
|
||||
string CreateIp = 8;
|
||||
string LastLoginTime = 9;
|
||||
string LastLoginIp = 10;
|
||||
int32 LoginTimes = 11;
|
||||
int32 Gender = 12;
|
||||
int32 LoginLimit = 13;
|
||||
bool IsBlock = 14;
|
||||
}
|
||||
|
||||
message GetUserByIdResp{
|
||||
|
Loading…
x
Reference in New Issue
Block a user