diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index f8c52b26e..5eb6d7101 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -435,5 +435,5 @@ func DisableUser(userID, operatorUserName string) (err error) { } func GetUserBindAuthInfo(userID string) (authList []*model.AuthBind, err error) { - return dao.GetUserBindAuthInfo(dao.GetDB(), userID) + return dao.GetUserBindAuthInfo(dao.GetDB(), userID, model.AuthBindTypeAuth, "", nil) } diff --git a/business/auth2/authprovider/defauther.go b/business/auth2/authprovider/defauther.go index fb536c639..d2d108e08 100644 --- a/business/auth2/authprovider/defauther.go +++ b/business/auth2/authprovider/defauther.go @@ -61,7 +61,7 @@ func (a *DefAuther) GetUserType() (userType int8) { func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []string, openID, unionID string, authDetail interface{}) (authBindEx *auth2.AuthBindEx, err error) { db := dao.GetDB() var authBind *model.AuthBind - if authBind, err = dao.GetAuthBind(db, "", curAuthType, openID); err == nil { // 直接找到了 + if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, curAuthType, openID); err == nil { // 直接找到了 authBindEx = &auth2.AuthBindEx{ AuthBind: *authBind, } @@ -71,7 +71,7 @@ func (a *DefAuther) UnionFindAuthBind(curAuthType string, unionAuthTypeList []st } else if dao.IsNoRowsError(err) { // 直接找不到,尝试unionID if unionID != "" { // 且有unionID var authBindList []*model.AuthBind - if authBindList, err = dao.GetAuthBindsByAuthID2(db, unionID, unionAuthTypeList); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式 + if authBindList, err = dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeAuth, unionID, unionAuthTypeList); err == nil && len(authBindList) > 0 { // 通过unionID找到至少一个认证方式 authBind = authBindList[0] authBind.Type = curAuthType authBind.AuthID = openID diff --git a/business/auth2/authprovider/password/password.go b/business/auth2/authprovider/password/password.go index 780eae3ba..26f26732d 100644 --- a/business/auth2/authprovider/password/password.go +++ b/business/auth2/authprovider/password/password.go @@ -37,7 +37,7 @@ func init() { func (a *Auther) VerifySecret(userID, passMD5 string) (authBindEx *auth2.AuthBindEx, err error) { globals.SugarLogger.Debugf("localpass VerifySecret userID:%s", userID) var authBind *model.AuthBind - if authBind, err = dao.GetAuthBind(dao.GetDB(), "", AuthType, userID); err == nil { + if authBind, err = dao.GetAuthBind(dao.GetDB(), "", model.AuthBindTypeAuth, AuthType, userID); err == nil { if err = a.checkPassword(authBind, passMD5); err == nil { authBindEx = &auth2.AuthBindEx{ AuthBind: *authBind, @@ -55,7 +55,7 @@ func (a *Auther) ChangePassword(userID, oldPassMD5, newPassMD5 string) (err erro db := dao.GetDB() salt := utils.GetUUID() encryptPwd := a.encryptPassword(newPassMD5, salt) - if authBind, err = dao.GetAuthBind(db, "", AuthType, userID); err == nil { + if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, AuthType, userID); err == nil { if err = a.checkPassword(authBind, oldPassMD5); err == nil || authBind.AuthSecret == "" { // 如果原密码为空,不判断原密码,代表重置密码 _, err = dao.UpdateEntityLogically(db, authBind, map[string]interface{}{ "AuthSecret": encryptPwd, diff --git a/business/jxstore/cms/authz.go b/business/jxstore/cms/authz.go index 7798e533a..482d4d942 100644 --- a/business/jxstore/cms/authz.go +++ b/business/jxstore/cms/authz.go @@ -1,6 +1,7 @@ package cms import ( + "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/auth2" "git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin" "git.rosy.net.cn/jx-callback/business/authz" @@ -65,12 +66,12 @@ func TransferLegacyWeixins(mobile string) (err error) { user := &model.User{ UserID2: v.Tel, Name: v.NickName, - Mobile: v.Tel, + Mobile: &v.Tel, Type: model.UserTypeStoreBoss, Remark: remark4Transfer, } if user.Name == "" { - user.Name = user.Mobile + user.Name = utils.Pointer2String(user.Mobile) } userList, _, err2 := dao.GetUsers(db, 0, "", nil, "", v.Tel, 0, -1) if err = err2; err != nil { diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index 0887db897..732d15b96 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -102,7 +102,7 @@ func init() { } func RegisterUser(user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) { - mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDMobile, mobileVerifyCode) + mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, utils.Pointer2String(user.Mobile), auth2.UserIDMobile, mobileVerifyCode) if err = err2; err == nil { if !mobileAuth.IsUserEmpty() { return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist) @@ -133,7 +133,7 @@ func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, er } func CreateUser(user *model.User, creatorName string) (err error) { - if user == nil || user.UserID2 == "" || user.Name == "" || user.Mobile == "" { + if user == nil || user.UserID2 == "" || user.Name == "" || utils.Pointer2String(user.Mobile) == "" { return ErrUserIDAndNameMustGiven } dao.WrapAddIDCULDEntity(user, creatorName) @@ -165,7 +165,7 @@ func OnDingDingMsg(msg map[string]interface{}) (callbackResponse *dingdingapi.Ca for _, userID := range msg[dingdingapi.KeyUserID].([]interface{}) { userIDStr := utils.Interface2String(userID) globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s left company", userIDStr) - if authBind, err = dao.GetAuthBind(db, "", dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了 + if authBind, err = dao.GetAuthBind(db, "", model.AuthBindTypeAuth, dingding.AuthTypeStaff, userIDStr); err == nil { // 直接找到了 globals.SugarLogger.Debugf("OnDingDingMsg dingding user:%s, userID:%s left company", userIDStr, authBind.UserID) if err = DisableUser(jxcontext.AdminCtx, authBind.UserID); err != nil { globals.SugarLogger.Errorf("OnDingDingMsg failed with error:%v", err) diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index 30681bb07..b8590ff84 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -449,7 +449,7 @@ func HandleUserWXRemark(db *dao.DaoDB, mobile string, mobileIsUerID bool) (err e } } if userID != "" { - authBind, err2 := dao.GetAuthBind(db, userID, weixin.AuthTypeMP, "") + authBind, err2 := dao.GetAuthBind(db, userID, model.AuthBindTypeAuth, weixin.AuthTypeMP, "") if err = err2; err == nil { openID = authBind.AuthID } diff --git a/business/jxutils/weixinmsg/weixinmsg.go b/business/jxutils/weixinmsg/weixinmsg.go index 4fe329ea3..23b766383 100644 --- a/business/jxutils/weixinmsg/weixinmsg.go +++ b/business/jxutils/weixinmsg/weixinmsg.go @@ -118,7 +118,7 @@ func GetWeixinOpenIDsFromStoreID(storeID int) (retVal []string) { if globals.EnableWXAuth2 { if userIDList, err2 := api2.RoleMan.GetRoleUserList(autils.NewRole(authz.StoreRoleBoss, storeID)); err2 == nil { for _, v := range userIDList { - if authInfo, err2 := dao.GetAuthBind(db, v, weixin.AuthTypeMP, ""); err2 == nil { + if authInfo, err2 := dao.GetAuthBind(db, v, model.AuthBindTypeAuth, weixin.AuthTypeMP, ""); err2 == nil { retVal = append(retVal, authInfo.AuthID) openIDMap[authInfo.AuthID] = 1 } diff --git a/business/model/auth2.go b/business/model/auth2.go index 6d68d5902..7ac38b378 100644 --- a/business/model/auth2.go +++ b/business/model/auth2.go @@ -7,14 +7,19 @@ const ( const ( AdminName = "jxadmin" + + AuthBindTypeAll = -1 + AuthBindTypeAuth = 0 // 绑定类型为认证 + AuthBindTypeID = 1 // 绑定类型为用户标识,不做为认证 ) type AuthBind struct { ModelIDCULD - UserID string `orm:"size(48);column(user_id)" json:"userID"` - Type string `orm:"size(16)" json:"type"` - Status int8 `json:"status"` + UserID string `orm:"size(48);column(user_id)" json:"userID"` + BindType int8 `json:"bindType"` + Type string `orm:"size(16)" json:"type"` + Status int8 `json:"status"` AuthID string `orm:"size(48);column(auth_id)" json:"authID"` AuthID2 string `orm:"size(48);column(auth_id2);index" json:"authID2"` diff --git a/business/model/dao/dao_auth2.go b/business/model/dao/dao_auth2.go index 29e0fa56c..dd1b5d6b4 100644 --- a/business/model/dao/dao_auth2.go +++ b/business/model/dao/dao_auth2.go @@ -7,7 +7,7 @@ import ( "git.rosy.net.cn/jx-callback/business/model" ) -func GetAuthBind(db *DaoDB, userID, authType, authID string) (authBind *model.AuthBind, err error) { +func GetAuthBind(db *DaoDB, userID string, bindType int, authType, authID string) (authBind *model.AuthBind, err error) { if userID == "" && authID == "" { return nil, errors.New("userID, authID, authID2不能全为空") } @@ -24,6 +24,10 @@ func GetAuthBind(db *DaoDB, userID, authType, authID string) (authBind *model.Au sql += " AND t1.user_id = ?" sqlParams = append(sqlParams, userID) } + if bindType != model.AuthBindTypeAll { + sql += " AND t1.bind_type = ?" + sqlParams = append(sqlParams, bindType) + } if authType != "" { sql += " AND t1.type = ?" sqlParams = append(sqlParams, authType) @@ -37,32 +41,32 @@ func GetAuthBind(db *DaoDB, userID, authType, authID string) (authBind *model.Au return authBind, err } -func GetAuthBindsByAuthID2(db *DaoDB, authID2 string, typeList []string) (authBinds []*model.AuthBind, err error) { +func GetUserBindAuthInfo(db *DaoDB, userID string, bindType int, authID2 string, typeList []string) (authList []*model.AuthBind, err error) { sql := ` SELECT * FROM auth_bind t1 - WHERE t1.deleted_at = ? AND t1.status = ? AND t1.auth_id2 = ? AND t1.type IN (` + GenQuestionMarks(len(typeList)) + ")" - sqlParams := []interface{}{ - utils.DefaultTimeValue, - model.AuthBindStatusNormal, - authID2, - typeList, - } - err = GetRows(db, &authBinds, sql, sqlParams...) - return authBinds, err -} - -func GetUserBindAuthInfo(db *DaoDB, userID string) (authList []*model.AuthBind, err error) { - sql := ` - SELECT * - FROM auth_bind t1 - WHERE t1.deleted_at = ? AND t1.status = ? AND t1.user_id = ? - ` + WHERE t1.deleted_at = ? AND t1.status = ?` sqlParams := []interface{}{ utils.DefaultTimeValue, model.UserStatusNormal, - userID, } + if userID != "" { + sql += " AND t1.user_id = ?" + sqlParams = append(sqlParams, userID) + } + if bindType != model.AuthBindTypeAll { + sql += " AND t1.bind_type = ?" + sqlParams = append(sqlParams, bindType) + } + if authID2 != "" { + sql += " AND t1.auth_id2 = ?" + sqlParams = append(sqlParams, authID2) + } + if len(typeList) > 0 { + sql += " AND t1.type IN (" + GenQuestionMarks(len(typeList)) + ")" + sqlParams = append(sqlParams, typeList) + } + err = GetRows(db, &authList, sql, sqlParams...) return authList, err } diff --git a/business/model/user.go b/business/model/user.go index 5b01f1dec..d506eade2 100644 --- a/business/model/user.go +++ b/business/model/user.go @@ -24,24 +24,23 @@ var ( type User struct { ModelIDCULD - UserID string `orm:"size(48);column(user_id)" json:"userID" compact:"userID"` // 内部唯一标识 - UserID2 string `orm:"size(48);column(user_id2)" json:"userID2" compact:"userID2"` // 外部唯一标识(一般用于登录) - Name string `orm:"size(48);index" json:"name" compact:"name"` // 外部唯一显示 标识(一般用于显示) - Mobile string `orm:"size(32)" json:"mobile" compact:"mobile"` - Email string `orm:"size(32);index" json:"email" compact:"email"` - Status int8 `json:"status" compact:"status"` - Type int8 `json:"type" compact:"type"` // 用户类型 - IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 - Remark string `orm:"size(255)" json:"remark"` + UserID string `orm:"size(48);column(user_id)" json:"userID" compact:"userID"` // 内部唯一标识 + UserID2 string `orm:"size(48);column(user_id2)" json:"userID2" compact:"userID2"` // 外部唯一标识(一般用于登录) + Name string `orm:"size(48);index" json:"name" compact:"name"` // 外部显示标识(当前可以重复) + Mobile *string `orm:"size(32);null" json:"mobile" compact:"mobile"` + Email *string `orm:"size(32);null" json:"email" compact:"email"` + Status int8 `json:"status" compact:"status"` + Type int8 `json:"type" compact:"type"` // 用户类型 + IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 + Remark string `orm:"size(255)" json:"remark"` } func (*User) TableUnique() [][]string { return [][]string{ []string{"UserID"}, []string{"UserID2", "DeletedAt"}, - // []string{"Name", "DeletedAt"}, - []string{"Mobile"}, - // []string{"Email", "DeletedAt"}, + []string{"Mobile", "DeletedAt"}, + []string{"Email", "DeletedAt"}, // []string{"IDCardNo", "DeletedAt"}, } } @@ -55,11 +54,11 @@ func (user *User) GetID2() string { } func (user *User) GetMobile() string { - return user.Mobile + return *user.Mobile } func (user *User) GetEmail() string { - return user.Email + return *user.Email } func (user *User) GetName() string {