- jxcontext.New

This commit is contained in:
gazebo
2019-03-05 11:18:30 +08:00
parent 9179ad127d
commit a8e92dd92e
5 changed files with 58 additions and 12 deletions

View File

@@ -86,7 +86,6 @@ var (
var (
ErrInternalErrror = errors.New("内部错误")
ErrTokenIsInvalid = errors.New("Token非法")
ErrUserAlreadyExist = errors.New("用户已经存在")
ErrUserMobileAlreadyExist = errors.New("用户手机已经存在")
ErrUserID2AlreadyExist = errors.New("用户标识已经存在")
@@ -114,7 +113,6 @@ func RegisterAuther(authType string, handler IAuther) {
func CreateAuthInfo(user IUser, authBindInfo *model.AuthBind, userData interface{}) (authInfo *AuthInfo) {
token, tokenType := createToken(user)
authInfo = &AuthInfo{
IUser: user,
AuthBindInfo: authBindInfo,
LoginTime: time.Now(),
@@ -124,6 +122,7 @@ func CreateAuthInfo(user IUser, authBindInfo *model.AuthBind, userData interface
UserData: userData,
}
if user != nil {
authInfo.UpdateByIUser(user)
globals.SugarLogger.Debugf("CreateAuthInfo id:%s, id2:%s, authInfo:%s", authInfo.GetID(), authInfo.GetID2(), authInfo.GetMobile(), utils.Format4Output(authInfo, true))
} else {
globals.SugarLogger.Debugf("CreateAuthInfo authInfo:%s", utils.Format4Output(authInfo, true))
@@ -215,7 +214,7 @@ func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err erro
if inauthInfo == nil || user == nil {
return nil, ErrInternalErrror
}
if inauthInfo.IUser != nil {
if !inauthInfo.IsUserEmpty() {
return nil, ErrUserAlreadyExist
}
if handler := authers[inauthInfo.AuthBindInfo.Type]; handler != nil {
@@ -235,7 +234,7 @@ func AddAuthBind(authInfo *AuthInfo, newAuthInfo *AuthInfo) (err error) {
if authInfo == nil || newAuthInfo == nil {
return ErrInternalErrror
}
if newAuthInfo.IUser != nil {
if !newAuthInfo.IsUserEmpty() {
return ErrAuthTypeAlreadyExist
}
RemoveUserInfo(newAuthInfo.Token)
@@ -279,7 +278,7 @@ func GetTokenInfo(token string) (authInfo *AuthInfo, err error) {
if err = api.Cacher.GetAs(token, authInfo); err == nil {
return authInfo, nil
}
return nil, ErrTokenIsInvalid
return nil, model.ErrTokenIsInvalid
}
func SetUserInfo(token string, authInfo *AuthInfo, duration time.Duration) {

View File

@@ -12,8 +12,49 @@ const (
TokenTypeOnlyAuth = 2
)
type UserBasic struct {
UserID string
UserID2 string
Mobile string
Email string
Name string
}
func (u *UserBasic) GetID() string {
return u.UserID
}
func (u *UserBasic) GetID2() string {
return u.UserID2
}
func (u *UserBasic) GetMobile() string {
return u.Mobile
}
func (u *UserBasic) GetEmail() string {
return u.Email
}
func (u *UserBasic) GetName() string {
return u.Name
}
func (u *UserBasic) UpdateByIUser(user IUser) {
if user != nil {
u.UserID = user.GetID()
u.UserID2 = user.GetID2()
u.Mobile = user.GetMobile()
u.Email = user.GetEmail()
u.Name = user.GetName()
}
}
func (u *UserBasic) IsUserEmpty() bool {
return u.UserID == ""
}
type AuthInfo struct {
IUser
UserBasic
AuthBindInfo *model.AuthBind
LoginTime time.Time

View File

@@ -67,7 +67,7 @@ func RegisterUser(user *model.User, mobileVerifyCode string, inAuthInfo *auth2.A
}
mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.Mobile, auth2.UserIDMobile, mobileVerifyCode)
if err = err2; err == nil {
if mobileAuth.IUser != nil {
if !mobileAuth.IsUserEmpty() {
return nil, model.ErrCodeUserAlreadyExist, auth2.ErrUserMobileAlreadyExist
}
dao.WrapAddIDCULDEntity(user, "RegisterUser")

View File

@@ -80,11 +80,17 @@ func New(rootTask tasksch.ITask, token string, w http.ResponseWriter, r *http.Re
ctx.userInfo = userInfo
}
}
if err != nil && beego.BConfig.RunMode == "prod" {
globals.SugarLogger.Debugf("token is invalid, token:%s", token)
return nil, model.ErrCodeTokenIsInvalid, err
if err == model.ErrTokenIsInvalid {
if beego.BConfig.RunMode != "prod" {
err = nil
} else {
errCode = model.ErrCodeTokenIsInvalid
}
}
return ctx, "", nil
if err == model.ErrTokenIsInvalid {
globals.SugarLogger.Debugf("token is invalid, token:%s", token)
}
return ctx, errCode, err
}
func (ctx *Context) GetUserName() string {

View File

@@ -177,7 +177,7 @@ func (c *Auth2Controller) AddAuthBind() {
func (c *Auth2Controller) RemoveAuthBind() {
c.callRemoveAuthBind(func(params *tAuth2RemoveAuthBindParams) (retVal interface{}, errCode string, err error) {
authInfo, err2 := params.Ctx.GetV2AuthInfo()
if err := err2; err == nil {
if err = err2; err == nil {
err = auth2.UnbindAuth(authInfo, params.AuthType)
}
return retVal, "", err