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"
|
|
|
|
|
2021-07-25 09:34:25 +08:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 == "" {
|
2021-08-08 11:06:45 +08:00
|
|
|
c.ResponseError("Please link first", value)
|
2021-04-18 23:14:46 +08:00
|
|
|
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, "")
|
2021-08-08 16:00:19 +08:00
|
|
|
c.ResponseOk()
|
2021-04-18 23:14:46 +08:00
|
|
|
}
|