[오승석] unlink & linkinfo 최신화
- KD쪽 코드 가져옴
This commit is contained in:
@ -1,7 +1,6 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -455,6 +454,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
sType := queryvals.Get("stype")
|
sType := queryvals.Get("stype")
|
||||||
sId := queryvals.Get("sid")
|
sId := queryvals.Get("sid")
|
||||||
sk := queryvals.Get("sk")
|
sk := queryvals.Get("sk")
|
||||||
|
targetType := queryvals.Get("ttype")
|
||||||
|
|
||||||
authInfo, err := sh.sessionProvider.Query(sk)
|
authInfo, err := sh.sessionProvider.Query(sk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -471,44 +471,51 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
// fmt.Println(authInfo.Uid)
|
// fmt.Println(authInfo.Uid)
|
||||||
// fmt.Println("=================")
|
// fmt.Println("=================")
|
||||||
|
|
||||||
|
sType, sId, err = sh.getProviderInfo(sType, sId)
|
||||||
|
if err != nil {
|
||||||
|
logger.Println("getProviderInfo failed :", err)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if authInfo.Uid != sId || authInfo.Platform != sType {
|
if authInfo.Uid != sId || authInfo.Platform != sType {
|
||||||
logger.Println("unlink failed. session key is not correct :", authInfo, queryvals)
|
logger.Println("unlink failed. session key is not correct :", authInfo, queryvals)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
numRecord, err := sh.mongoClient.Collection(CollectionAccount).CountDocuments(context.Background(), bson.M{
|
accDocs, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{
|
||||||
"accid": authInfo.Account,
|
"accid": authInfo.Account,
|
||||||
}, options.Count().SetLimit(2))
|
}, options.Find().SetProjection(bson.M{
|
||||||
|
"_id": 1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("unlink failed, fail to count accounts :", err)
|
logger.Error("unlink failed, fail to count accounts :", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRecord <= 1 {
|
if len(accDocs) <= 1 {
|
||||||
logger.Println("unlink failed. At least one link must be maintained. :", r.URL.Query())
|
logger.Println("unlink failed. At least one link must be maintained. :", r.URL.Query())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sType, sId, err = sh.getProviderInfo(sType, sId)
|
var ids primitive.A
|
||||||
if err != nil {
|
for _, accDoc := range accDocs {
|
||||||
logger.Error("getProviderInfo failed :", err)
|
ids = append(ids, accDoc["_id"].(primitive.ObjectID))
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := sh.mongoClient.FindOne(CollectionLink, bson.M{
|
link, err := sh.mongoClient.FindOneAndDelete(CollectionLink, bson.M{
|
||||||
"platform": sType,
|
"platform": targetType,
|
||||||
"uid": sId,
|
"_id": bson.M{"$in": ids},
|
||||||
}, options.FindOne().SetProjection(bson.M{"_id": 1}))
|
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("link failed. FindOneAndUpdate link err:", err)
|
logger.Error("link failed. FindOneAndUpdate link err:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{
|
preid, err := sh.mongoClient.FindOneAndDelete(CollectionAccount, bson.M{
|
||||||
"_id": link["_id"].(primitive.ObjectID),
|
"_id": link["_id"].(primitive.ObjectID),
|
||||||
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -517,9 +524,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// newid가 있어야 한다. 그래야 기존 서비스 계정이 없는 상태이다.
|
if preid == nil {
|
||||||
if newid == nil {
|
|
||||||
// 이미 계정이 있네?
|
|
||||||
logger.Println("unlink failed. service account not found:", r.URL.Query())
|
logger.Println("unlink failed. service account not found:", r.URL.Query())
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
@ -528,7 +533,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
|||||||
logger.Println("unlink success :", r.URL.Query())
|
logger.Println("unlink success :", r.URL.Query())
|
||||||
}
|
}
|
||||||
|
|
||||||
// == 연결된 계정 정보(숫자) 전달하는 API
|
// == 연결된 계정 정보(platform) 전달하는 API
|
||||||
func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||||
defer func() {
|
defer func() {
|
||||||
s := recover()
|
s := recover()
|
||||||
@ -562,6 +567,13 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
// fmt.Println(authInfo.Uid)
|
// fmt.Println(authInfo.Uid)
|
||||||
// fmt.Println("=================")
|
// fmt.Println("=================")
|
||||||
|
|
||||||
|
sType, sId, err = sh.getProviderInfo(sType, sId)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("getProviderInfo failed :", err)
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//if oldAuth.Token != oldToken || oldAuth.Uid != oldId || oldAuth.Platform != oldType {
|
//if oldAuth.Token != oldToken || oldAuth.Uid != oldId || oldAuth.Platform != oldType {
|
||||||
if authInfo.Uid != sId || authInfo.Platform != sType {
|
if authInfo.Uid != sId || authInfo.Platform != sType {
|
||||||
logger.Println("linkinfo failed. session key is not correct :", authInfo, queryvals)
|
logger.Println("linkinfo failed. session key is not correct :", authInfo, queryvals)
|
||||||
@ -569,19 +581,46 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
numRecord, err := sh.mongoClient.Collection(CollectionAccount).CountDocuments(context.Background(), bson.M{
|
platformName := "platform"
|
||||||
"accid": authInfo.Account,
|
accDocs, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{"accid": authInfo.Account}, options.Find().SetLimit(sh.MaximumNumLinkAccount).SetProjection(bson.M{
|
||||||
}, options.Count().SetLimit(sh.MaximumNumLinkAccount))
|
"_id": 1,
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("linkinfo failed. CountDocuments err :", err)
|
logger.Error("linkinfo failed. CountDocuments err :", err)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Println("linkinfo :", numRecord)
|
var ids primitive.A
|
||||||
w.Write([]byte(fmt.Sprintf(`{"num_linked_account":"%d"}`, numRecord)))
|
for _, accDoc := range accDocs {
|
||||||
|
ids = append(ids, accDoc["_id"].(primitive.ObjectID))
|
||||||
|
}
|
||||||
|
|
||||||
|
links, err := sh.mongoClient.FindAll(CollectionLink, bson.M{
|
||||||
|
"_id": bson.M{"$in": ids},
|
||||||
|
}, options.Find().SetLimit(sh.MaximumNumLinkAccount).SetProjection(bson.M{
|
||||||
|
platformName: 1,
|
||||||
|
}))
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("linkinfo failed. FindAll returns err :", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var linkstrs []string
|
||||||
|
for _, link := range links {
|
||||||
|
linkstrs = append(linkstrs, link[platformName].(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
linkbytes, err := json.Marshal(linkstrs)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("linkinfo failed. json marshal fail :", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Println("linkinfo :", linkstrs)
|
||||||
|
w.Write(linkbytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
// == 계정 이메일 조회
|
// == 계정 이메일 조회
|
||||||
|
|||||||
Reference in New Issue
Block a user