Add Unlink API.

This commit is contained in:
Yang Luo
2021-04-18 23:14:46 +08:00
parent 2934d6bdeb
commit 6774b0379c
8 changed files with 171 additions and 28 deletions

View File

@ -195,7 +195,8 @@ func (c *ApiController) Login() {
return
}
isLinked := object.LinkUserAccount(userId, provider.Type, userInfo.Username)
user := object.GetUser(userId)
isLinked := object.LinkUserAccount(user, provider.Type, userInfo.Username)
if isLinked {
resp = &Response{Status: "ok", Msg: "", Data: isLinked}
} else {

59
controllers/link.go Normal file
View File

@ -0,0 +1,59 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"encoding/json"
"github.com/casdoor/casdoor/object"
)
type LinkForm struct {
ProviderType string `json:"providerType"`
}
func (c *ApiController) Unlink() {
var resp Response
if c.GetSessionUser() == "" {
resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()}
c.Data["json"] = resp
c.ServeJSON()
return
}
var form LinkForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
if err != nil {
panic(err)
}
providerType := form.ProviderType
userId := c.GetSessionUser()
user := object.GetUser(userId)
value := object.GetUserField(user, providerType)
if value == "" {
resp = Response{Status: "error", Msg: "Please link first", Data: value}
c.Data["json"] = resp
c.ServeJSON()
return
}
object.LinkUserAccount(user, providerType, "")
resp = Response{Status: "ok", Msg: ""}
c.Data["json"] = resp
c.ServeJSON()
}