From 5d21158c7b99971040cb64ddc0784d6b88e080c6 Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 6 Jan 2020 12:13:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?SendVerifyCode=E6=97=B6=EF=BC=8C=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E6=98=AF=E7=AE=A1=E7=90=86=E5=91=98=EF=BC=8C=E8=BF=94?= =?UTF-8?q?=E5=9B=9EverifyCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/auth2/auth2.go | 18 +++++++++++++----- business/auth2/auth_info.go | 6 ++++++ business/auth2/authprovider/defauther.go | 4 ++-- business/auth2/authprovider/mobile/mobile.go | 12 ++++++------ business/model/user.go | 4 ++++ controllers/auth2.go | 2 +- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index f8632117c..d3a01beae 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -53,6 +53,7 @@ type IUser interface { GetEmail() string GetName() string GetAvatar() string + IsAdmin() bool } const ( @@ -76,7 +77,7 @@ type CaptchaInfo struct { } type IAuther interface { - SendVerifyCode(authID string) (err error) + SendVerifyCode(authID string) (verifyCode string, err error) // 负责验证secret,并找到相应的用户返回(password,email,mobile类型的不负责用户查找)如果找不到用户UserID为空 VerifySecret(authID, authSecret string) (authBindEx *AuthBindEx, err error) AddAuthBind(authBindEx *AuthBindEx, userName string) (err error) @@ -195,9 +196,13 @@ func CreateCaptcha(width, height, captchaLen int) (captchaInfo *CaptchaInfo, err return captchaInfo, err } -func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (err error) { +func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyCode string, err error) { + isReturnVerifyCode := false if authToken != "" { - _, err = GetTokenInfo(authToken) + authInfo, err2 := GetTokenInfo(authToken) + if err = err2; err == nil { + isReturnVerifyCode = authInfo.IsAdmin() + } } else if captchaID != "" && captchaValue != "" { if !(TestCaptchaMap[captchaID] == captchaValue || captcha.VerifyString(captchaID, captchaValue)) { err = ErrCaptchaIsNotOk @@ -210,10 +215,13 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (err erro if handler := authers[authType]; handler == nil { err = ErrIllegalAuthType } else { - err = handler.SendVerifyCode(authID) + code, err2 := handler.SendVerifyCode(authID) + if err = err2; err == nil && isReturnVerifyCode { + verfifyCode = code + } } } - return err + return verfifyCode, err } // 账号密码时:authIDType可能是:UserIDID,UserIDID2,UserIDMobile,UserIDEmail,authSecret是密码的sha1 diff --git a/business/auth2/auth_info.go b/business/auth2/auth_info.go index e145747d5..27e227bd5 100644 --- a/business/auth2/auth_info.go +++ b/business/auth2/auth_info.go @@ -19,6 +19,7 @@ type UserBasic struct { Email string `json:"email"` Name string `json:"name"` Avatar string `json:"avatar"` + isAdmin bool } func (u *UserBasic) GetID() string { @@ -44,6 +45,10 @@ func (u *UserBasic) GetAvatar() string { return u.Avatar } +func (u *UserBasic) IsAdmin() bool { + return u.isAdmin +} + func (u *UserBasic) UpdateByIUser(user IUser) { if user != nil { u.UserID = user.GetID() @@ -52,6 +57,7 @@ func (u *UserBasic) UpdateByIUser(user IUser) { u.Email = user.GetEmail() u.Name = user.GetName() u.Avatar = user.GetAvatar() + u.isAdmin = user.IsAdmin() } } diff --git a/business/auth2/authprovider/defauther.go b/business/auth2/authprovider/defauther.go index 80f026d29..517b5565d 100644 --- a/business/auth2/authprovider/defauther.go +++ b/business/auth2/authprovider/defauther.go @@ -45,8 +45,8 @@ func (a *DefAuther) UnbindAuth(userID, authType, userName string) (err error) { return err } -func (a *DefAuther) SendVerifyCode(authID string) error { - return errors.New("当前登录类型不支持此操作") +func (a *DefAuther) SendVerifyCode(authID string) (verifyCode string, err error) { + return "", errors.New("当前登录类型不支持此操作") } // 此函数为空 diff --git a/business/auth2/authprovider/mobile/mobile.go b/business/auth2/authprovider/mobile/mobile.go index 6f3a181ee..fb688d9e4 100644 --- a/business/auth2/authprovider/mobile/mobile.go +++ b/business/auth2/authprovider/mobile/mobile.go @@ -43,15 +43,15 @@ func init() { } // 特殊接口 -func (a *Auther) SendVerifyCode(mobileNumber string) error { - code := a.GenerateVerifyCode(mobileNumber) +func (a *Auther) SendVerifyCode(mobileNumber string) (verifyCode string, err error) { + verifyCode = a.GenerateVerifyCode(mobileNumber) smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/") response, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_175583158", string(utils.MustMarshal(map[string]interface{}{ - "code": code, + "code": verifyCode, }))) - a.SaveVerifyCode(mobileNumber, code) + a.SaveVerifyCode(mobileNumber, verifyCode) if err == nil && response.Code == aliyunsmsclient.ResponseCodeOk { - // a.SaveVerifyCode(mobileNumber, code) + // a.SaveVerifyCode(mobileNumber, verifyCode) } else { if err == nil { if warningMap[response.Code] == 1 { @@ -64,7 +64,7 @@ func (a *Auther) SendVerifyCode(mobileNumber string) error { globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err) } } - return err + return verifyCode, err } func (a *Auther) VerifySecret(mobileNumber, code string) (authBindEx *auth2.AuthBindEx, err error) { diff --git a/business/model/user.go b/business/model/user.go index 3613aaa72..1439611e0 100644 --- a/business/model/user.go +++ b/business/model/user.go @@ -85,6 +85,10 @@ func (user *User) GetAvatar() string { return user.Avatar } +func (user *User) IsAdmin() bool { + return user.Type&(UserTypeOperator|UserTypeBoss) != 0 +} + type StoreBoss struct { ModelIDCULD UserID string `orm:"size(48);column(user_id);unique" json:"userID"` // 内部唯一标识 diff --git a/controllers/auth2.go b/controllers/auth2.go index 272fbaa3b..6b2433908 100644 --- a/controllers/auth2.go +++ b/controllers/auth2.go @@ -66,7 +66,7 @@ func (c *Auth2Controller) CreateCaptcha() { // @router /SendVerifyCode [post] func (c *Auth2Controller) SendVerifyCode() { c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) { - err = auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID) + retVal, err = auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID) return retVal, "", err }) } From 483aa6df20406618f1020fa023ed364ee4ec13f8 Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 6 Jan 2020 15:24:59 +0800 Subject: [PATCH 2/3] IsAdmin --- business/auth2/auth_info.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/business/auth2/auth_info.go b/business/auth2/auth_info.go index 27e227bd5..eec9da9c5 100644 --- a/business/auth2/auth_info.go +++ b/business/auth2/auth_info.go @@ -13,13 +13,13 @@ const ( ) type UserBasic struct { - UserID string `json:"userID"` - UserID2 string `json:"userID2"` - Mobile string `json:"mobile"` - Email string `json:"email"` - Name string `json:"name"` - Avatar string `json:"avatar"` - isAdmin bool + UserID string `json:"userID"` + UserID2 string `json:"userID2"` + Mobile string `json:"mobile"` + Email string `json:"email"` + Name string `json:"name"` + Avatar string `json:"avatar"` + IsAdminData bool `json:"isAdmin"` } func (u *UserBasic) GetID() string { @@ -46,7 +46,7 @@ func (u *UserBasic) GetAvatar() string { } func (u *UserBasic) IsAdmin() bool { - return u.isAdmin + return u.IsAdminData } func (u *UserBasic) UpdateByIUser(user IUser) { @@ -57,7 +57,7 @@ func (u *UserBasic) UpdateByIUser(user IUser) { u.Email = user.GetEmail() u.Name = user.GetName() u.Avatar = user.GetAvatar() - u.isAdmin = user.IsAdmin() + u.IsAdminData = user.IsAdmin() } } From fda22fa2d470fd86b8af11b003158687b37233f9 Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 6 Jan 2020 15:36:33 +0800 Subject: [PATCH 3/3] SendVerifyCode --- business/auth2/auth2.go | 16 ++++------------ business/auth2/auth_info.go | 6 ------ business/model/user.go | 4 ---- controllers/auth2.go | 9 ++++++++- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index d3a01beae..ef5dd4298 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -53,7 +53,6 @@ type IUser interface { GetEmail() string GetName() string GetAvatar() string - IsAdmin() bool } const ( @@ -196,13 +195,9 @@ func CreateCaptcha(width, height, captchaLen int) (captchaInfo *CaptchaInfo, err return captchaInfo, err } -func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyCode string, err error) { - isReturnVerifyCode := false +func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyCode string, authInfo *AuthInfo, err error) { if authToken != "" { - authInfo, err2 := GetTokenInfo(authToken) - if err = err2; err == nil { - isReturnVerifyCode = authInfo.IsAdmin() - } + authInfo, err = GetTokenInfo(authToken) } else if captchaID != "" && captchaValue != "" { if !(TestCaptchaMap[captchaID] == captchaValue || captcha.VerifyString(captchaID, captchaValue)) { err = ErrCaptchaIsNotOk @@ -215,13 +210,10 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyC if handler := authers[authType]; handler == nil { err = ErrIllegalAuthType } else { - code, err2 := handler.SendVerifyCode(authID) - if err = err2; err == nil && isReturnVerifyCode { - verfifyCode = code - } + verfifyCode, err = handler.SendVerifyCode(authID) } } - return verfifyCode, err + return verfifyCode, authInfo, err } // 账号密码时:authIDType可能是:UserIDID,UserIDID2,UserIDMobile,UserIDEmail,authSecret是密码的sha1 diff --git a/business/auth2/auth_info.go b/business/auth2/auth_info.go index eec9da9c5..8f98df47e 100644 --- a/business/auth2/auth_info.go +++ b/business/auth2/auth_info.go @@ -19,7 +19,6 @@ type UserBasic struct { Email string `json:"email"` Name string `json:"name"` Avatar string `json:"avatar"` - IsAdminData bool `json:"isAdmin"` } func (u *UserBasic) GetID() string { @@ -45,10 +44,6 @@ func (u *UserBasic) GetAvatar() string { return u.Avatar } -func (u *UserBasic) IsAdmin() bool { - return u.IsAdminData -} - func (u *UserBasic) UpdateByIUser(user IUser) { if user != nil { u.UserID = user.GetID() @@ -57,7 +52,6 @@ func (u *UserBasic) UpdateByIUser(user IUser) { u.Email = user.GetEmail() u.Name = user.GetName() u.Avatar = user.GetAvatar() - u.IsAdminData = user.IsAdmin() } } diff --git a/business/model/user.go b/business/model/user.go index 1439611e0..3613aaa72 100644 --- a/business/model/user.go +++ b/business/model/user.go @@ -85,10 +85,6 @@ func (user *User) GetAvatar() string { return user.Avatar } -func (user *User) IsAdmin() bool { - return user.Type&(UserTypeOperator|UserTypeBoss) != 0 -} - type StoreBoss struct { ModelIDCULD UserID string `orm:"size(48);column(user_id);unique" json:"userID"` // 内部唯一标识 diff --git a/controllers/auth2.go b/controllers/auth2.go index 6b2433908..b8b544fd7 100644 --- a/controllers/auth2.go +++ b/controllers/auth2.go @@ -13,6 +13,7 @@ import ( "git.rosy.net.cn/jx-callback/business/auth2/authprovider/password" "git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin" "git.rosy.net.cn/jx-callback/business/model" + "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/globals" "github.com/astaxie/beego" ) @@ -66,7 +67,13 @@ func (c *Auth2Controller) CreateCaptcha() { // @router /SendVerifyCode [post] func (c *Auth2Controller) SendVerifyCode() { c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) { - retVal, err = auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID) + code, authInfo, err := auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID) + if err == nil && authInfo != nil { + user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", authInfo.GetID()) + if err2 == nil && user.Type&(model.UserTypeBoss|model.UserTypeOperator) != 0 { + retVal = code + } + } return retVal, "", err }) }