|
|
|
@@ -2,6 +2,8 @@ package wechat |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.gitea.io/gitea/models" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"fmt" |
|
|
|
) |
|
|
|
|
|
|
|
type QRCode4BindCache struct { |
|
|
|
@@ -16,10 +18,49 @@ const ( |
|
|
|
BIND_STATUS_EXPIRED = 9 |
|
|
|
) |
|
|
|
|
|
|
|
type WechatAccountUsedError struct { |
|
|
|
} |
|
|
|
|
|
|
|
func (err WechatAccountUsedError) Error() string { |
|
|
|
return fmt.Sprint("wechat account has been used") |
|
|
|
} |
|
|
|
|
|
|
|
type OpenIAccountUsedError struct { |
|
|
|
} |
|
|
|
|
|
|
|
func (err OpenIAccountUsedError) Error() string { |
|
|
|
return fmt.Sprint("openI account has been used") |
|
|
|
} |
|
|
|
|
|
|
|
func BindWechat(userId int64, wechatOpenId string) error { |
|
|
|
if !IsWechatAccountAvailable(userId, wechatOpenId) { |
|
|
|
log.Error("bind wechat failed, because user use wrong wechat account to bind,userId=%d wechatOpenId=%s", userId, wechatOpenId) |
|
|
|
return WechatAccountUsedError{} |
|
|
|
} |
|
|
|
if !IsUserAvailableForWechatBind(userId, wechatOpenId) { |
|
|
|
log.Error("openI account has been used,userId=%d wechatOpenId=%s", userId, wechatOpenId) |
|
|
|
return OpenIAccountUsedError{} |
|
|
|
} |
|
|
|
return models.BindWechatOpenId(userId, wechatOpenId) |
|
|
|
} |
|
|
|
|
|
|
|
func UnbindWechat(userId int64, oldWechatOpenId string) error { |
|
|
|
return models.UnbindWechatOpenId(userId, oldWechatOpenId) |
|
|
|
} |
|
|
|
|
|
|
|
//IsUserAvailableForWechatBind if user has bound wechat and the bound openId is not the given wechatOpenId,return false |
|
|
|
//otherwise,return true |
|
|
|
func IsUserAvailableForWechatBind(userId int64, wechatOpenId string) bool { |
|
|
|
currentOpenId := models.GetUserWechatOpenId(userId) |
|
|
|
return currentOpenId == "" || currentOpenId == wechatOpenId |
|
|
|
} |
|
|
|
|
|
|
|
//IsWechatAccountAvailable if wechat account used by another account,return false |
|
|
|
//if wechat account not used or used by the given user,return true |
|
|
|
func IsWechatAccountAvailable(userId int64, wechatOpenId string) bool { |
|
|
|
user := models.GetUserByWechatOpenId(wechatOpenId) |
|
|
|
if user != nil && user.WechatOpenId != "" && user.ID != userId { |
|
|
|
return false |
|
|
|
} |
|
|
|
return true |
|
|
|
} |