From 1e708239d0a98a7f96612c8439ceee265acc265b Mon Sep 17 00:00:00 2001 From: liangjies <1139629972@qq.com> Date: Sun, 21 Jul 2024 22:38:51 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add:=20=E5=8F=AA=E5=85=81=E8=AE=B8=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/api/v2/quest.go | 2 +- internal/app/assets/locale.json | 6 ++++-- internal/app/model/response/user.go | 5 +++++ internal/app/service/quest_v2.go | 8 ++++++++ internal/app/service/user.go | 10 +++++++--- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/app/api/v2/quest.go b/internal/app/api/v2/quest.go index ddc268b8..759f7ff0 100644 --- a/internal/app/api/v2/quest.go +++ b/internal/app/api/v2/quest.go @@ -13,7 +13,7 @@ func AddQuestV2(c *gin.Context) { } address := c.GetString("address") if list, err := srv.AddQuestV2(address, add); err != nil { - FailWithMessage(GetMessage(c, "FetchFailed"), c) + FailWithMessage(GetMessage(c, err.Error()), c) } else { OkWithData(list, c) } diff --git a/internal/app/assets/locale.json b/internal/app/assets/locale.json index d0817078..38335981 100644 --- a/internal/app/assets/locale.json +++ b/internal/app/assets/locale.json @@ -38,7 +38,8 @@ "EmailAlreadyLinked": "The email has already been linked to another address!", "GithubAlreadyLinked": "Github has been linked to another wallet address!", "AddressAlreadyLinkedGithub": "The wallet address has already been linked to Github. Please do not repeat the operation.", - "FailedObtainGithubInfo": "Failed to obtain Github information!" + "FailedObtainGithubInfo": "Failed to obtain Github information!", + "PermissionDenied": "Permission Denied!" }, "zh-CN": { "OperationSuccess": "操作成功!", @@ -79,6 +80,7 @@ "EmailAlreadyLinked": "邮箱已绑定其他钱包地址", "GithubAlreadyLinked": "Github 已绑定其他钱包地址", "AddressAlreadyLinkedGithub": "钱包地址已绑定Github,请勿重复操作", - "FailedObtainGithubInfo": "绑定 Github 信息失败!" + "FailedObtainGithubInfo": "绑定 Github 信息失败!", + "PermissionDenied": "权限不足!" } } \ No newline at end of file diff --git a/internal/app/model/response/user.go b/internal/app/model/response/user.go index 855fb9b5..fa5ac7ca 100644 --- a/internal/app/model/response/user.go +++ b/internal/app/model/response/user.go @@ -14,3 +14,8 @@ type QuestWithClaimed struct { func (QuestWithClaimed) TableName() string { return "quest" } + +type GetUserInfoRes struct { + model.Users + IsAdmin bool `json:"is_admin"` +} diff --git a/internal/app/service/quest_v2.go b/internal/app/service/quest_v2.go index 607439c5..831412b5 100644 --- a/internal/app/service/quest_v2.go +++ b/internal/app/service/quest_v2.go @@ -10,6 +10,14 @@ import ( ) func (s *Service) AddQuestV2(address string, add request.AddQuestV2Request) (res string, err error) { + // 是否管理员 + isAdmin, err := s.dao.IsAdmin(address) + if err != nil { + return + } + if !isAdmin { + return "", errors.New("PermissionDenied") + } privateKey, err := crypto.HexToECDSA(s.c.BlockChain.SignPrivateKey) if err != nil { return diff --git a/internal/app/service/user.go b/internal/app/service/user.go index 1c4d0288..eefeb318 100644 --- a/internal/app/service/user.go +++ b/internal/app/service/user.go @@ -3,6 +3,7 @@ package service import ( "backend-go/internal/app/model" "backend-go/internal/app/model/request" + "backend-go/internal/app/model/response" "backend-go/internal/app/utils" "backend-go/pkg/auth" "backend-go/pkg/log" @@ -20,7 +21,7 @@ import ( "strings" ) -func (s *Service) GetUserInfo(address string, loginAddress string) (res interface{}, err error) { +func (s *Service) GetUserInfo(address string, loginAddress string) (res response.GetUserInfoRes, err error) { var user model.Users if user, err = s.dao.GetUser(address); err != nil { return @@ -48,7 +49,9 @@ func (s *Service) GetUserInfo(address string, loginAddress string) (res interfac } } user.NickName = &showStr - return user, err + res.IsAdmin = true + res.Users = user + return res, err } // default nickname if user.NickName == nil || *user.NickName == "" { @@ -57,7 +60,8 @@ func (s *Service) GetUserInfo(address string, loginAddress string) (res interfac user.NickName = &nickName } } - return user, err + res.Users = user + return res, err } func (s *Service) UpdateUserInfo(address string, user request.UpdateUserInfo) (err error) { From f055a6c890908fe3964a0c7469d042c7ee94b207 Mon Sep 17 00:00:00 2001 From: liangjies <1139629972@qq.com> Date: Sun, 21 Jul 2024 22:47:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/api/v1/social.go | 19 +++++++++++++++++++ internal/app/dao/users.go | 11 +++++++++++ internal/app/model/request/social.go | 4 ++++ internal/app/router/social.go | 3 ++- internal/app/service/social.go | 9 +++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/internal/app/api/v1/social.go b/internal/app/api/v1/social.go index 68293ac5..35367887 100644 --- a/internal/app/api/v1/social.go +++ b/internal/app/api/v1/social.go @@ -194,3 +194,22 @@ func ConfirmBindChange(c *gin.Context) { Ok(c) } } + +// CancelBindChange 取消绑定变更 +func CancelBindChange(c *gin.Context) { + var r request.CancelBindChangeRequest + if err := c.ShouldBindJSON(&r); err != nil { + FailWithMessage(GetMessage(c, "ParameterError"), c) + return + } + address := c.GetString("address") + if address == "" { + Fail(c) + return + } + if err := srv.CancelBindChange(address, r.Type); err != nil { + FailWithMessage(GetMessage(c, err.Error()), c) + } else { + Ok(c) + } +} diff --git a/internal/app/dao/users.go b/internal/app/dao/users.go index b5aa1d40..e3379f29 100644 --- a/internal/app/dao/users.go +++ b/internal/app/dao/users.go @@ -162,6 +162,17 @@ func (d *Dao) ConfirmBindChange(address string, bindType string) (err error) { return d.redis.Del(context.Background(), key).Err() } +// CancelBindChange 取消绑定变更 +func (d *Dao) CancelBindChange(address string, bindType string) (err error) { + key := getRebindKey(bindType, address) + // 从 Redis 删除 + err = d.redis.Del(context.Background(), key).Err() + if err != nil { + return + } + return nil +} + // ParticleUpdateSocialsInfo 更新社交信息 func (d *Dao) ParticleUpdateSocialsInfo(address string, particleUserinfo datatypes.JSON) (err error) { provider := gjson.Get(particleUserinfo.String(), "thirdparty_user_info.provider").String() diff --git a/internal/app/model/request/social.go b/internal/app/model/request/social.go index 673d518d..ea429ab0 100644 --- a/internal/app/model/request/social.go +++ b/internal/app/model/request/social.go @@ -21,3 +21,7 @@ type BindSocialResultRequest struct { type ConfirmBindChangeRequest struct { Type string `json:"type" binding:"required"` } + +type CancelBindChangeRequest struct { + Type string `json:"type" binding:"required"` +} diff --git a/internal/app/router/social.go b/internal/app/router/social.go index 9bf166dc..ade5deee 100644 --- a/internal/app/router/social.go +++ b/internal/app/router/social.go @@ -27,7 +27,8 @@ func InitSocialRouter(Router *gin.RouterGroup) { } { routerAuth.POST("/bindSocialResult", v1.BindSocialResult) // 查询绑定结果 - routerAuth.POST("/confirmBindChange", v1.ConfirmBindChange) // 确认绑定 + routerAuth.POST("/confirmBindChange", v1.ConfirmBindChange) // 确认换绑 + routerAuth.POST("/cancelBindChange", v1.CancelBindChange) // 取消换绑 routerAuth.POST("/unbindSocial", v1.UnbindSocial) // 解绑 } } diff --git a/internal/app/service/social.go b/internal/app/service/social.go index 8bdaae2d..b5555a59 100644 --- a/internal/app/service/social.go +++ b/internal/app/service/social.go @@ -55,6 +55,7 @@ func (s *Service) WechatBindAddress(c *gin.Context, address, fromUserName string if err != nil { return errors.New("服务器内部错误") } + s.dao.CancelBindChange(address, "wechat") if isBinding { // 替换绑定 if replace { @@ -120,6 +121,7 @@ func (s *Service) DiscordCallback(address string, discordCallback interface{}, r if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "discord") if Binding { err = s.dao.SaveRebindInfo(address, "discord", bindingAddress) if err != nil { @@ -216,6 +218,7 @@ func (s *Service) EmailBindAddress(address, emailAddress, code string, replace b if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "email") if isBinding { err = s.dao.SaveRebindInfo(address, "email", bindingAddress) if err != nil { @@ -280,6 +283,7 @@ func (s *Service) GithubCallback(address string, githubCallback interface{}, rep if err != nil { return res, errors.New("UnexpectedError") } + s.dao.CancelBindChange(address, "github") if binding { err = s.dao.SaveRebindInfo(address, "github", bindingAddress) if err != nil { @@ -333,3 +337,8 @@ func (s *Service) BindSocialResult(address string, bindType string) (res respons func (s *Service) ConfirmBindChange(address, bindType string) (err error) { return s.dao.ConfirmBindChange(address, bindType) } + +// CancelBindChange 取消绑定变更 +func (s *Service) CancelBindChange(address, bindType string) (err error) { + return s.dao.CancelBindChange(address, bindType) +} From 26b3ef90fa4976f6dbbc8f75ade45ece95622cb3 Mon Sep 17 00:00:00 2001 From: liangjies <1139629972@qq.com> Date: Tue, 23 Jul 2024 09:04:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E5=BE=AE=E4=BF=A1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=88=A0=E9=99=A4=E7=BB=91=E5=AE=9A=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/service/social.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/app/service/social.go b/internal/app/service/social.go index b5555a59..41e9d69b 100644 --- a/internal/app/service/social.go +++ b/internal/app/service/social.go @@ -59,7 +59,7 @@ func (s *Service) WechatBindAddress(c *gin.Context, address, fromUserName string if isBinding { // 替换绑定 if replace { - err = s.dao.UnbindSocial(address, "wechat") + err = s.dao.UnbindSocial(bindingAddress, "wechat") if err != nil { return errors.New("UnexpectedError") }