60 lines
1.4 KiB
Go
Raw Normal View History

2021-04-18 23:14:46 +08:00
// 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/casbin/casdoor/object"
2021-04-18 23:14:46 +08:00
)
type LinkForm struct {
ProviderType string `json:"providerType"`
}
2021-08-07 22:02:56 +08:00
// Unlink ...
2021-04-18 23:14:46 +08:00
func (c *ApiController) Unlink() {
2021-05-17 23:25:28 +08:00
userId, ok := c.RequireSignedIn()
if !ok {
2021-04-18 23:14:46 +08:00
return
}
2021-05-17 23:25:28 +08:00
var resp Response
2021-04-18 23:14:46 +08:00
var form LinkForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
if err != nil {
panic(err)
}
providerType := form.ProviderType
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
}
2021-06-09 21:27:20 +08:00
object.ClearUserOAuthProperties(user, providerType)
2021-05-30 18:35:05 +08:00
2021-04-18 23:14:46 +08:00
object.LinkUserAccount(user, providerType, "")
resp = Response{Status: "ok", Msg: ""}
c.Data["json"] = resp
c.ServeJSON()
}