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 (
|
import (
|
||||||
api "Open_IM/pkg/base_info"
|
api "Open_IM/pkg/base_info"
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/config"
|
||||||
|
"Open_IM/pkg/common/constant"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
"Open_IM/pkg/common/token_verify"
|
"Open_IM/pkg/common/token_verify"
|
||||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||||
@ -10,10 +11,11 @@ import (
|
|||||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"context"
|
"context"
|
||||||
"github.com/fatih/structs"
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/structs"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @Summary 用户注册
|
// @Summary 用户注册
|
||||||
@ -65,11 +67,15 @@ func UserRegister(c *gin.Context) {
|
|||||||
if reply.CommonResp.ErrCode != 0 {
|
if reply.CommonResp.ErrCode != 0 {
|
||||||
errMsg := req.OperationID + " " + " UserRegister failed " + reply.CommonResp.ErrMsg + req.String()
|
errMsg := req.OperationID + " " + " UserRegister failed " + reply.CommonResp.ErrMsg + req.String()
|
||||||
log.NewError(req.OperationID, errMsg)
|
log.NewError(req.OperationID, 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})
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||||
|
}
|
||||||
return
|
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)
|
replyToken, err := client.UserToken(context.Background(), pbDataToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := req.OperationID + " " + " client.UserToken failed " + err.Error() + pbDataToken.String()
|
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})
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": 401, "errMsg": errMsg})
|
||||||
return
|
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())
|
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)
|
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImAuthName, req.OperationID)
|
||||||
if etcdConn == nil {
|
if etcdConn == nil {
|
||||||
|
@ -10,8 +10,9 @@ import (
|
|||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ParamsLogin struct {
|
type ParamsLogin struct {
|
||||||
@ -62,6 +63,11 @@ func Login(c *gin.Context) {
|
|||||||
openIMGetUserToken.Platform = params.Platform
|
openIMGetUserToken.Platform = params.Platform
|
||||||
openIMGetUserToken.Secret = config.Config.Secret
|
openIMGetUserToken.Secret = config.Config.Secret
|
||||||
openIMGetUserToken.UserID = userID
|
openIMGetUserToken.UserID = userID
|
||||||
|
loginIp := c.Request.Header.Get("X-Forward-For")
|
||||||
|
if loginIp == "" {
|
||||||
|
loginIp = c.ClientIP()
|
||||||
|
}
|
||||||
|
openIMGetUserToken.LoginIp = loginIp
|
||||||
openIMGetUserTokenResp := api.UserTokenResp{}
|
openIMGetUserTokenResp := api.UserTokenResp{}
|
||||||
bMsg, err := http2.Post(url, openIMGetUserToken, 2)
|
bMsg, err := http2.Post(url, openIMGetUserToken, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -72,7 +78,11 @@ func Login(c *gin.Context) {
|
|||||||
err = json.Unmarshal(bMsg, &openIMGetUserTokenResp)
|
err = json.Unmarshal(bMsg, &openIMGetUserTokenResp)
|
||||||
if err != nil || openIMGetUserTokenResp.ErrCode != 0 {
|
if err != nil || openIMGetUserTokenResp.ErrCode != 0 {
|
||||||
log.NewError(params.OperationID, "request get user token", account, "err", "")
|
log.NewError(params.OperationID, "request get user token", account, "err", "")
|
||||||
|
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": ""})
|
c.JSON(http.StatusOK, gin.H{"errCode": constant.GetIMTokenErr, "errMsg": ""})
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": constant.NoError, "errMsg": "", "data": openIMGetUserTokenResp.UserToken})
|
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/common/log"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ParamsSetPassword struct {
|
type ParamsSetPassword struct {
|
||||||
@ -81,6 +82,11 @@ func SetPassword(c *gin.Context) {
|
|||||||
openIMRegisterReq.Nickname = params.Nickname
|
openIMRegisterReq.Nickname = params.Nickname
|
||||||
openIMRegisterReq.Secret = config.Config.Secret
|
openIMRegisterReq.Secret = config.Config.Secret
|
||||||
openIMRegisterReq.FaceURL = params.FaceURL
|
openIMRegisterReq.FaceURL = params.FaceURL
|
||||||
|
createIp := c.Request.Header.Get("X-Forward-For")
|
||||||
|
if createIp == "" {
|
||||||
|
createIp = c.ClientIP()
|
||||||
|
}
|
||||||
|
openIMRegisterReq.CreateIp = createIp
|
||||||
openIMRegisterResp := api.UserRegisterResp{}
|
openIMRegisterResp := api.UserRegisterResp{}
|
||||||
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "register req:", openIMRegisterReq)
|
log.NewDebug(params.OperationID, utils.GetSelfFuncName(), "register req:", openIMRegisterReq)
|
||||||
bMsg, err := http2.Post(url, openIMRegisterReq, 2)
|
bMsg, err := http2.Post(url, openIMRegisterReq, 2)
|
||||||
@ -95,7 +101,11 @@ func SetPassword(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(params.OperationID, utils.GetSelfFuncName(), err.Error())
|
log.NewError(params.OperationID, utils.GetSelfFuncName(), err.Error())
|
||||||
}
|
}
|
||||||
|
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})
|
c.JSON(http.StatusOK, gin.H{"errCode": constant.RegisterFailed, "errMsg": "register failed: " + openIMRegisterResp.ErrMsg})
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Info(params.OperationID, "begin store mysql", account, params.Password, "info", params.FaceURL, params.Nickname)
|
log.Info(params.OperationID, "begin store mysql", account, params.Password, "info", params.FaceURL, params.Nickname)
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"Open_IM/pkg/common/config"
|
"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))
|
user.Birth = utils.UnixSecondToTime(int64(req.UserInfo.Birth))
|
||||||
}
|
}
|
||||||
log.Debug(req.OperationID, "copy ", user, req.UserInfo)
|
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)
|
err := imdb.UserRegister(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := req.OperationID + " imdb.UserRegister failed " + err.Error() + user.UserID
|
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) {
|
func (rpc *rpcAuth) UserToken(_ context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
|
||||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String())
|
||||||
_, err := imdb.GetUserByUserID(req.FromUserID)
|
user, err := imdb.GetUserByUserID(req.FromUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := req.OperationID + " imdb.GetUserByUserID failed " + err.Error() + req.FromUserID
|
errMsg := req.OperationID + " imdb.GetUserByUserID failed " + err.Error() + req.FromUserID
|
||||||
log.NewError(req.OperationID, errMsg)
|
log.NewError(req.OperationID, errMsg)
|
||||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
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))
|
tokens, expTime, err := token_verify.CreateToken(req.FromUserID, int(req.Platform))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errMsg := req.OperationID + " token_verify.CreateToken failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
|
errMsg := req.OperationID + " token_verify.CreateToken failed " + err.Error() + req.FromUserID + utils.Int32ToString(req.Platform)
|
||||||
log.NewError(req.OperationID, errMsg)
|
log.NewError(req.OperationID, errMsg)
|
||||||
return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: errMsg}}, nil
|
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})
|
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
|
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/constant"
|
||||||
"Open_IM/pkg/common/db"
|
"Open_IM/pkg/common/db"
|
||||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
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"
|
errors "Open_IM/pkg/common/http"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
"Open_IM/pkg/common/token_verify"
|
"Open_IM/pkg/common/token_verify"
|
||||||
@ -542,8 +542,17 @@ func (s *userServer) GetUsersByName(ctx context.Context, req *pbUser.GetUsersByN
|
|||||||
ProfilePhoto: user.FaceURL,
|
ProfilePhoto: user.FaceURL,
|
||||||
Nickname: user.Nickname,
|
Nickname: user.Nickname,
|
||||||
UserId: user.UserID,
|
UserId: user.UserID,
|
||||||
CreateTime: user.CreateTime.String(),
|
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
|
||||||
|
CreateIp: user.CreateIp,
|
||||||
IsBlock: isBlock,
|
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}
|
user := db.User{Nickname: req.UserName}
|
||||||
@ -578,8 +587,17 @@ func (s *userServer) GetUserById(ctx context.Context, req *pbUser.GetUserByIdReq
|
|||||||
ProfilePhoto: user.FaceURL,
|
ProfilePhoto: user.FaceURL,
|
||||||
Nickname: user.Nickname,
|
Nickname: user.Nickname,
|
||||||
UserId: user.UserID,
|
UserId: user.UserID,
|
||||||
CreateTime: user.CreateTime.String(),
|
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
|
||||||
|
CreateIp: user.CreateIp,
|
||||||
IsBlock: isBlock,
|
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())
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
|
||||||
return resp, nil
|
return resp, nil
|
||||||
@ -599,9 +617,18 @@ func (s *userServer) GetUsers(ctx context.Context, req *pbUser.GetUsersReq) (*pb
|
|||||||
user := &pbUser.User{
|
user := &pbUser.User{
|
||||||
ProfilePhoto: v.FaceURL,
|
ProfilePhoto: v.FaceURL,
|
||||||
UserId: v.UserID,
|
UserId: v.UserID,
|
||||||
CreateTime: v.CreateTime.String(),
|
CreateTime: v.CreateTime.Format("2006-01-02 15:04:05"),
|
||||||
|
CreateIp: v.CreateIp,
|
||||||
Nickname: v.Nickname,
|
Nickname: v.Nickname,
|
||||||
|
Birth: v.Birth.Format("2006-01-02"),
|
||||||
|
PhoneNumber: v.PhoneNumber,
|
||||||
|
Email: v.Email,
|
||||||
IsBlock: isBlock,
|
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)
|
resp.User = append(resp.User, user)
|
||||||
} else {
|
} else {
|
||||||
|
@ -30,6 +30,7 @@ type UserTokenReq struct {
|
|||||||
Secret string `json:"secret" binding:"required,max=32"`
|
Secret string `json:"secret" binding:"required,max=32"`
|
||||||
Platform int32 `json:"platform" binding:"required,min=1,max=8"`
|
Platform int32 `json:"platform" binding:"required,min=1,max=8"`
|
||||||
UserID string `json:"userID" binding:"required,min=1,max=64"`
|
UserID string `json:"userID" binding:"required,min=1,max=64"`
|
||||||
|
LoginIp string `json:"loginIp" binding:"required,max=15"`
|
||||||
OperationID string `json:"operationID" binding:"required"`
|
OperationID string `json:"operationID" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package base_info
|
package base_info
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiUserInfo struct {
|
type ApiUserInfo struct {
|
||||||
@ -13,6 +14,12 @@ type ApiUserInfo struct {
|
|||||||
PhoneNumber string `json:"phoneNumber" binding:"omitempty,max=32"`
|
PhoneNumber string `json:"phoneNumber" binding:"omitempty,max=32"`
|
||||||
Birth uint32 `json:"birth" binding:"omitempty"`
|
Birth uint32 `json:"birth" binding:"omitempty"`
|
||||||
Email string `json:"email" binding:"omitempty,max=64"`
|
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"`
|
Ex string `json:"ex" binding:"omitempty,max=1024"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,16 @@ type UserResponse struct {
|
|||||||
Nickname string `json:"nick_name"`
|
Nickname string `json:"nick_name"`
|
||||||
UserId string `json:"user_id"`
|
UserId string `json:"user_id"`
|
||||||
CreateTime string `json:"create_time,omitempty"`
|
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"`
|
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 {
|
type GetUserRequest struct {
|
||||||
|
@ -67,6 +67,8 @@ const (
|
|||||||
CodeInvalidOrExpired = 10009
|
CodeInvalidOrExpired = 10009
|
||||||
RegisterFailed = 10010
|
RegisterFailed = 10010
|
||||||
ResetPasswordFailed = 10011
|
ResetPasswordFailed = 10011
|
||||||
|
RegisterLimit = 10012
|
||||||
|
LoginLimit = 10013
|
||||||
DatabaseError = 10002
|
DatabaseError = 10002
|
||||||
ServerError = 10004
|
ServerError = 10004
|
||||||
HttpError = 10005
|
HttpError = 10005
|
||||||
|
@ -153,8 +153,9 @@ type GroupRequest struct {
|
|||||||
//string Birth = 6;
|
//string Birth = 6;
|
||||||
//string Email = 7;
|
//string Email = 7;
|
||||||
//string Ex = 8;
|
//string Ex = 8;
|
||||||
//int64 CreateTime = 9;
|
//string CreateIp = 9;
|
||||||
//int32 AppMangerLevel = 10;
|
//int64 CreateTime = 10;
|
||||||
|
//int32 AppMangerLevel = 11;
|
||||||
//open_im_sdk.User == imdb.User
|
//open_im_sdk.User == imdb.User
|
||||||
type User struct {
|
type User struct {
|
||||||
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
UserID string `gorm:"column:user_id;primary_key;size:64"`
|
||||||
@ -166,11 +167,30 @@ type User struct {
|
|||||||
Email string `gorm:"column:email;size:64"`
|
Email string `gorm:"column:email;size:64"`
|
||||||
Ex string `gorm:"column:ex;size:1024"`
|
Ex string `gorm:"column:ex;size:1024"`
|
||||||
CreateTime time.Time `gorm:"column:create_time"`
|
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"`
|
AppMangerLevel int32 `gorm:"column:app_manger_level"`
|
||||||
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"`
|
GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"`
|
||||||
status int32 `gorm:"column:status"`
|
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{
|
//message BlackInfo{
|
||||||
//string OwnerUserID = 1;
|
//string OwnerUserID = 1;
|
||||||
//int64 CreateTime = 2;
|
//int64 CreateTime = 2;
|
||||||
|
@ -2,14 +2,13 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/config"
|
||||||
"gorm.io/gorm/logger"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mysqlDB struct {
|
type mysqlDB struct {
|
||||||
@ -79,7 +78,7 @@ func initMysqlDB() {
|
|||||||
&GroupMember{},
|
&GroupMember{},
|
||||||
&GroupRequest{},
|
&GroupRequest{},
|
||||||
&User{},
|
&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", "CHARSET=utf8")
|
||||||
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
db.Set("gorm:table_options", "collation=utf8_unicode_ci")
|
||||||
|
|
||||||
@ -143,6 +142,18 @@ func initMysqlDB() {
|
|||||||
fmt.Println("CreateTable DepartmentMember")
|
fmt.Println("CreateTable DepartmentMember")
|
||||||
db.Migrator().CreateTable(&AppVersion{})
|
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
|
DB.MysqlDB.db = db
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,9 @@ func UserRegister(user db.User) error {
|
|||||||
if user.Birth.Unix() < 0 {
|
if user.Birth.Unix() < 0 {
|
||||||
user.Birth = utils.UnixSecondToTime(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
|
err := db.DB.MysqlDB.DefaultGormDB().Table("users").Create(&user).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -130,7 +133,7 @@ func UserIsBlock(userId string) (bool, error) {
|
|||||||
var user db.BlackList
|
var user db.BlackList
|
||||||
rows := db.DB.MysqlDB.DefaultGormDB().Table("black_lists").Where("uid=?", userId).First(&user).RowsAffected
|
rows := db.DB.MysqlDB.DefaultGormDB().Table("black_lists").Where("uid=?", userId).First(&user).RowsAffected
|
||||||
if rows >= 1 {
|
if rows >= 1 {
|
||||||
return true, nil
|
return user.EndDisableTime.After(time.Now()), nil
|
||||||
}
|
}
|
||||||
return false, 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)
|
db.DB.MysqlDB.DefaultGormDB().Table("black_lists").Where("uid=?", userId).First(&blockUser)
|
||||||
if blockUser.UserId != "" {
|
if blockUser.UserId != "" {
|
||||||
db.DB.MysqlDB.DefaultGormDB().Model(&blockUser).Where("uid=?", blockUser.UserId).Update("end_disable_time", end)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
blockUser = db.BlackList{
|
blockUser = db.BlackList{
|
||||||
@ -159,11 +165,20 @@ func BlockUser(userId, endDisableTime string) error {
|
|||||||
EndDisableTime: end,
|
EndDisableTime: end,
|
||||||
}
|
}
|
||||||
result := db.DB.MysqlDB.DefaultGormDB().Create(&blockUser)
|
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
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnBlockUser(userId string) 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 {
|
type BlockUserInfo struct {
|
||||||
@ -238,3 +253,30 @@ func GetBlockUsersNumCount() (int32, error) {
|
|||||||
}
|
}
|
||||||
return int32(count), nil
|
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 FromUserID = 2;
|
||||||
string OpUserID = 3;
|
string OpUserID = 3;
|
||||||
string OperationID = 4;
|
string OperationID = 4;
|
||||||
|
string LoginIp = 5;
|
||||||
}
|
}
|
||||||
message UserTokenResp {
|
message UserTokenResp {
|
||||||
CommonResp CommonResp = 1;
|
CommonResp CommonResp = 1;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -72,9 +72,14 @@ message UserInfo{
|
|||||||
uint32 birth = 6;
|
uint32 birth = 6;
|
||||||
string email = 7;
|
string email = 7;
|
||||||
string ex = 8;
|
string ex = 8;
|
||||||
uint32 createTime = 9;
|
string createIp = 9;
|
||||||
int32 appMangerLevel = 10;
|
uint32 createTime = 10;
|
||||||
int32 globalRecvMsgOpt = 11;
|
string LastLoginIp =11;
|
||||||
|
uint32 LastLoginTime = 12;
|
||||||
|
int32 LoginTimes = 13;
|
||||||
|
int32 LoginLimit = 14;
|
||||||
|
int32 appMangerLevel = 15;
|
||||||
|
int32 globalRecvMsgOpt = 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
message FriendInfo{
|
message FriendInfo{
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -178,7 +178,16 @@ message User{
|
|||||||
string Nickname = 2;
|
string Nickname = 2;
|
||||||
string UserId = 3;
|
string UserId = 3;
|
||||||
string CreateTime = 4;
|
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{
|
message GetUserByIdResp{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user