作者 yangfu

增加配置 多端登录控制

@@ -351,10 +351,12 @@ func CenterCompanys(header *protocol.RequestHeader, request *protocolx.CenterCom @@ -351,10 +351,12 @@ func CenterCompanys(header *protocol.RequestHeader, request *protocolx.CenterCom
351 break 351 break
352 case protocol.LoginByCredentials: 352 case protocol.LoginByCredentials:
353 //凭证是否存在 353 //凭证是否存在
354 - if credential, e := userAuth.NewRedisUserCredential(request.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {  
355 - log.Debug("凭证过期或者已失效:", request.Phone)  
356 - err = protocol.NewErrWithMessage(4140, err)  
357 - return 354 + if constant.DISENABLE_MULTI_DEVICE_LOGIN {
  355 + if credential, e := userAuth.NewRedisUserCredential(request.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
  356 + log.Debug("凭证过期或者已失效:", request.Phone)
  357 + err = protocol.NewErrWithMessage(4140, err)
  358 + return
  359 + }
358 } 360 }
359 if _, err = utils.ParseJWTToken(request.Credentials); err != nil { 361 if _, err = utils.ParseJWTToken(request.Credentials); err != nil {
360 err = protocol.NewErrWithMessage(4140, err) 362 err = protocol.NewErrWithMessage(4140, err)
@@ -419,10 +421,12 @@ func Companys(header *protocol.RequestHeader, request *protocolx.CompanysRequest @@ -419,10 +421,12 @@ func Companys(header *protocol.RequestHeader, request *protocolx.CompanysRequest
419 return 421 return
420 } 422 }
421 //凭证是否存在 423 //凭证是否存在
422 - if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {  
423 - log.Debug("凭证过期或者已失效:", claim.Phone)  
424 - err = protocol.NewErrWithMessage(4140, err)  
425 - return 424 + if constant.DISENABLE_MULTI_DEVICE_LOGIN {
  425 + if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
  426 + log.Debug("凭证过期或者已失效:", claim.Phone)
  427 + err = protocol.NewErrWithMessage(4140, err)
  428 + return
  429 + }
426 } 430 }
427 loginSvr.Init(claim.Phone) 431 loginSvr.Init(claim.Phone)
428 rsp.Partner, _ = loginSvr.PartnerStaticInfo() 432 rsp.Partner, _ = loginSvr.PartnerStaticInfo()
@@ -458,11 +462,13 @@ func LoginV2(header *protocol.RequestHeader, request *protocol.LoginRequestV2) ( @@ -458,11 +462,13 @@ func LoginV2(header *protocol.RequestHeader, request *protocol.LoginRequestV2) (
458 err = protocol.NewErrWithMessage(4140, err) 462 err = protocol.NewErrWithMessage(4140, err)
459 return 463 return
460 } 464 }
461 - //凭证是否存在  
462 - if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {  
463 - log.Debug("凭证过期或者已失效:", claim.Phone)  
464 - err = protocol.NewErrWithMessage(4140, err)  
465 - return 465 + if constant.DISENABLE_MULTI_DEVICE_LOGIN {
  466 + //凭证是否存在
  467 + if credential, e := userAuth.NewRedisUserCredential(claim.Phone).GetAuth(); e != nil || !strings.EqualFold(credential, request.Credentials) {
  468 + log.Debug("凭证过期或者已失效:", claim.Phone)
  469 + err = protocol.NewErrWithMessage(4140, err)
  470 + return
  471 + }
466 } 472 }
467 if company, e := CompanyRepository.FindOne(map[string]interface{}{"id": request.Cid, "enable": 1}); e != nil || company == nil { 473 if company, e := CompanyRepository.FindOne(map[string]interface{}{"id": request.Cid, "enable": 1}); e != nil || company == nil {
468 err = protocol.NewErrWithMessage(10007, err) 474 err = protocol.NewErrWithMessage(10007, err)
@@ -3,10 +3,13 @@ package event @@ -3,10 +3,13 @@ package event
3 import ( 3 import (
4 "github.com/tiptok/gocomm/xa/eda" 4 "github.com/tiptok/gocomm/xa/eda"
5 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event/subscriber" 5 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/event/subscriber"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
6 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event" 7 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain/event"
7 ) 8 )
8 9
9 func InitEventCenter() { 10 func InitEventCenter() {
10 - eda.RegisterSubscribe(&event.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{})  
11 - eda.RegisterSubscribe(&event.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{}) 11 + if constant.DISENABLE_MULTI_DEVICE_LOGIN {
  12 + eda.RegisterSubscribe(&event.AccessTokenEvent{}, &subscriber.AuthCacheSubscribe{})
  13 + eda.RegisterSubscribe(&event.RefreshTokenEvent{}, &subscriber.AuthCacheSubscribe{})
  14 + }
12 } 15 }
@@ -21,6 +21,9 @@ var DEFAULT_GUEST_COMPANY int = 10011 @@ -21,6 +21,9 @@ var DEFAULT_GUEST_COMPANY int = 10011
21 21
22 var SHARE_SHOP_PREVIEW_IMADGE = "https://media.fjmaimaimai.com/image/default/3E0C7050C13147CE8C7AF86C75F904E9-6-2.jpg" //分享店铺预览图 22 var SHARE_SHOP_PREVIEW_IMADGE = "https://media.fjmaimaimai.com/image/default/3E0C7050C13147CE8C7AF86C75F904E9-6-2.jpg" //分享店铺预览图
23 var WEHAT_MINI_PROGRAM_VERSION_TYPE = 2 //0:正式版 1:开发版 2:体验版 23 var WEHAT_MINI_PROGRAM_VERSION_TYPE = 2 //0:正式版 1:开发版 2:体验版
  24 +
  25 +var DISENABLE_MULTI_DEVICE_LOGIN = false //不允许多端登录 (注:正式环境需要为true 测试环境设为false,方便多个设备测试)
  26 +
24 func init() { 27 func init() {
25 if os.Getenv("LOG_LEVEL") != "" { 28 if os.Getenv("LOG_LEVEL") != "" {
26 LOG_LEVEL = os.Getenv("LOG_LEVEL") 29 LOG_LEVEL = os.Getenv("LOG_LEVEL")
@@ -32,4 +32,7 @@ func init() { @@ -32,4 +32,7 @@ func init() {
32 if os.Getenv("DISABLE_SQL_GENERATE_PRINT") != "" { 32 if os.Getenv("DISABLE_SQL_GENERATE_PRINT") != "" {
33 DISABLE_SQL_GENERATE_PRINT = true 33 DISABLE_SQL_GENERATE_PRINT = true
34 } 34 }
  35 + if POSTGRESQL_DB_NAME == "partner" {
  36 + DISENABLE_MULTI_DEVICE_LOGIN = true
  37 + }
35 } 38 }
@@ -3,6 +3,7 @@ package middleware @@ -3,6 +3,7 @@ package middleware
3 import ( 3 import (
4 "github.com/astaxie/beego/context" 4 "github.com/astaxie/beego/context"
5 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth" 5 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/userAuth"
  6 + "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/constant"
6 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils" 7 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
7 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol" 8 "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
8 "strconv" 9 "strconv"
@@ -39,15 +40,17 @@ func CheckJWTToken(ctx *context.Context) { @@ -39,15 +40,17 @@ func CheckJWTToken(ctx *context.Context) {
39 } else { 40 } else {
40 ctx.Input.SetData("UserId", u.UserId) 41 ctx.Input.SetData("UserId", u.UserId)
41 42
42 - // valid token  
43 - userPhone, _ := strconv.Atoi(u.Phone)  
44 - tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)))  
45 - err := tokenAuth.Check(  
46 - userAuth.NewOptions(userAuth.WithAccessToken(token)),  
47 - )  
48 - if err != nil {  
49 - msg = protocol.NewMesage(4141)  
50 - return 43 + if constant.DISENABLE_MULTI_DEVICE_LOGIN {
  44 + // valid token
  45 + userPhone, _ := strconv.Atoi(u.Phone)
  46 + tokenAuth := userAuth.NewRedisUserAuth(userAuth.WithUserId(int64(userPhone)))
  47 + err := tokenAuth.Check(
  48 + userAuth.NewOptions(userAuth.WithAccessToken(token)),
  49 + )
  50 + if err != nil {
  51 + msg = protocol.NewMesage(4141)
  52 + return
  53 + }
51 } 54 }
52 } 55 }
53 return 56 return