From 6dc3fd0f45d9a06bb1aaa65dbdcf08701eca45a1 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Fri, 18 Jun 2021 23:25:24 +0800 Subject: [PATCH] Avoid linking the same account twice. --- controllers/auth.go | 11 +++++++++++ object/user_util.go | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/controllers/auth.go b/controllers/auth.go index 1647e318..9df14fb7 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -304,6 +304,17 @@ func (c *ApiController) Login() { return } + oldUser := object.GetUserByField(application.Organization, provider.Type, userInfo.Id) + if oldUser == nil { + oldUser = object.GetUserByField(application.Organization, provider.Type, userInfo.Username) + } + if oldUser != nil { + resp = &Response{Status: "error", Msg: fmt.Sprintf("The account for provider: %s and username: %s (%s) is already linked to another account", provider.Type, userInfo.Username, userInfo.DisplayName)} + c.Data["json"] = resp + c.ServeJSON() + return + } + user := object.GetUser(userId) // sync info from 3rd-party if possible diff --git a/object/user_util.go b/object/user_util.go index 5d1c5217..54956eca 100644 --- a/object/user_util.go +++ b/object/user_util.go @@ -24,6 +24,10 @@ import ( ) func GetUserByField(organizationName string, field string, value string) *User { + if field == "" || value == "" { + return nil + } + user := User{Owner: organizationName} existed, err := adapter.Engine.Where(fmt.Sprintf("%s=?", field), value).Get(&user) if err != nil {