Files
open-im-server/internal/demo/register/ip_limit.go
T

201 lines
6.5 KiB
Go
Raw Normal View History

2022-08-15 16:51:41 +08:00
package register
import (
//api "Open_IM/pkg/base_info"
2022-08-15 16:51:41 +08:00
"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/log"
"Open_IM/pkg/utils"
"github.com/gin-gonic/gin"
2022-08-15 20:16:15 +08:00
"github.com/jinzhu/gorm"
2022-08-15 16:51:41 +08:00
"net/http"
"time"
)
2022-08-15 18:39:34 +08:00
type QueryIPRegisterReq struct {
2022-08-15 16:51:41 +08:00
OperationID string `json:"operationID"`
IP string `json:"ip"`
}
2022-08-15 18:39:34 +08:00
type QueryIPRegisterResp struct {
2022-08-15 16:51:41 +08:00
IP string `json:"ip"`
RegisterNum int `json:"num"`
2022-08-15 18:39:34 +08:00
Status int `json:"status"`
2022-08-15 16:51:41 +08:00
UserIDList []string `json:"userIDList"`
}
2022-08-15 18:39:34 +08:00
func QueryIPRegister(c *gin.Context) {
req := QueryIPRegisterReq{}
resp := QueryIPRegisterResp{}
2022-08-15 16:51:41 +08:00
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
userIDList, err := imdb.GetRegisterUserNum(req.IP)
2022-08-15 16:51:41 +08:00
if err != nil {
log.NewError(req.OperationID, "GetInvitationCode failed", req.IP)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "GetRegisterUserNum error!"})
2022-08-15 16:51:41 +08:00
return
}
resp.IP = req.IP
resp.RegisterNum = len(userIDList)
resp.UserIDList = userIDList
2022-08-15 20:16:15 +08:00
_, err = imdb.QueryIPLimits(req.IP)
if err != nil {
2022-08-15 20:16:15 +08:00
if gorm.IsRecordNotFoundError(err) {
resp.Status = 0
} else {
log.NewError(req.OperationID, "QueryIPLimits failed", req.IP, err.Error())
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "QueryIPLimits error!"})
return
}
} else {
2022-08-15 16:51:41 +08:00
resp.Status = 1
}
2022-08-15 20:16:15 +08:00
2022-08-15 16:51:41 +08:00
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp)
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
}
type AddIPLimitReq struct {
OperationID string `json:"operationID"`
IP string `json:"ip"`
LimitTime int32 `json:"limitTime"`
}
type AddIPLimitResp struct {
}
func AddIPLimit(c *gin.Context) {
req := AddIPLimitReq{}
//resp := AddIPLimitResp{}
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
if err := imdb.InsertOneIntoIpLimits(db.IpLimit{
Ip: req.IP,
LimitRegister: 1,
LimitLogin: 1,
CreateTime: time.Now(),
2022-08-15 19:50:13 +08:00
LimitTime: utils.UnixSecondToTime(int64(req.LimitTime)),
2022-08-15 16:51:41 +08:00
}); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.IP, req.LimitTime)
2022-08-15 18:34:03 +08:00
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "InsertOneIntoIpLimits error!"})
2022-08-15 16:51:41 +08:00
return
}
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
}
type RemoveIPLimitReq struct {
OperationID string `json:"operationID"`
IP string `json:"ip"`
2022-08-15 16:51:41 +08:00
}
type RemoveIPLimitResp struct {
}
func RemoveIPLimit(c *gin.Context) {
2022-08-15 19:50:13 +08:00
req := RemoveIPLimitReq{}
//resp := AddIPLimitResp{}
if err := c.BindJSON(&req); err != nil {
2022-08-15 19:50:13 +08:00
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrArgs, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
if err := imdb.DeleteOneFromIpLimits(req.IP); err != nil {
2022-08-15 19:50:13 +08:00
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.IP)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "InsertOneIntoIpLimits error!"})
return
}
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
2022-08-15 16:51:41 +08:00
}
2022-08-15 17:52:45 +08:00
// ===========================================sk ==========================
2022-08-15 16:51:41 +08:00
2022-08-15 17:52:45 +08:00
type QueryUserIDIPLimitLoginReq struct {
UserID string `json:"userID" binding:"required"`
OperationID string `json:"operationID" binding:"required"`
2022-08-15 16:51:41 +08:00
}
2022-08-15 17:52:45 +08:00
//type QueryUserIDIPLimitLoginResp struct {
// UserIpLimit []db.UserIpLimit `json:"userIpLimit"`
//}
2022-08-15 16:51:41 +08:00
2022-08-15 19:50:13 +08:00
func QueryUserIDLimitLogin(c *gin.Context) {
2022-08-15 17:52:45 +08:00
req := QueryUserIDIPLimitLoginReq{}
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
resp, err := imdb.GetIpLimitsLoginByUserID(req.UserID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
2022-08-15 18:34:03 +08:00
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "GetIpLimitsByUserID error!"})
2022-08-15 17:52:45 +08:00
return
}
2022-08-15 18:43:12 +08:00
if len(resp) > 0 {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp)
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
return
}
2022-08-15 19:50:13 +08:00
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
2022-08-15 16:51:41 +08:00
}
2022-08-15 17:52:45 +08:00
type AddUserIPLimitLoginReq struct {
UserID string `json:"userID" binding:"required"`
OperationID string `json:"operationID" binding:"required"`
IP string `json:"ip"`
2022-08-15 16:51:41 +08:00
}
2022-08-15 17:52:45 +08:00
type AddUserIPLimitLoginResp struct {
2022-08-15 16:51:41 +08:00
}
// 添加ip 特定用户才能登录 user_ip_limits 表
2022-08-15 17:52:45 +08:00
func AddUserIPLimitLogin(c *gin.Context) {
req := AddUserIPLimitLoginReq{}
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
userIp := db.UserIpLimit{UserID: req.UserID, Ip: req.IP}
err := imdb.InsertUserIpLimitsLogin(&userIp)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "InsertUserIpLimitsLogin error!"})
return
}
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
2022-08-15 16:51:41 +08:00
}
type RemoveUserIPLimitReq struct {
2022-08-15 17:52:45 +08:00
UserID string `json:"userID" binding:"required"`
OperationID string `json:"operationID" binding:"required"`
IP string `json:"ip"`
2022-08-15 16:51:41 +08:00
}
type RemoveUserIPLimitResp struct {
}
// 删除ip 特定用户才能登录 user_ip_limits 表
2022-08-15 17:52:45 +08:00
func RemoveUserIPLimitLogin(c *gin.Context) {
req := RemoveUserIPLimitReq{}
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
return
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
err := imdb.DeleteUserIpLimitsLogin(req.UserID, req.IP)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "DeleteUserIpLimitsLogin error!"})
return
}
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
2022-08-15 16:51:41 +08:00
}