-
Notifications
You must be signed in to change notification settings - Fork 25
/
context_auth.go
92 lines (87 loc) · 3.27 KB
/
context_auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package botsfw
import (
"fmt"
"github.com/bots-go-framework/bots-fw-store/botsfwmodels"
"github.com/bots-go-framework/bots-fw/botsfw/botsdal"
"time"
)
// SetAccessGranted marks current context as authenticated
func SetAccessGranted(whc WebhookContext, value bool) (err error) {
c := whc.Context()
log.Debugf(c, "SetAccessGranted(value=%v)", value)
botID := whc.GetBotCode()
chatData := whc.ChatData()
if chatData != nil {
if chatData.IsAccessGranted() == value {
log.Infof(c, "No need to change chatData.AccessGranted, as already is: %v", value)
} else {
chatKey := botsfwmodels.ChatKey{
BotID: botID,
}
if chatKey.ChatID, err = whc.BotChatID(); err != nil {
return err
}
if changed := chatData.SetAccessGranted(value); changed {
now := time.Now()
chatDataBase := chatData.Base()
chatDataBase.DtUpdated = now
chatDataBase.SetDtLastInteraction(now) // Must set DtLastInteraction through wrapper
if err = whc.SaveBotChat(c); err != nil {
err = fmt.Errorf("failed to save bot botChat entity to db: %w", err)
return err
}
}
}
}
botUserID := whc.GetSender().GetID()
botUserStrID := fmt.Sprintf("%v", botUserID)
log.Debugf(c, "SetAccessGranted(): whc.GetSender().GetID() = %v", botUserID)
tx := whc.Tx()
platformID := whc.BotPlatform().ID()
botSettings := whc.BotContext().BotSettings
if botUser, err := botsdal.GetBotUser(c, tx, platformID, botSettings.Code, botUserStrID, botSettings.Profile.NewBotUserData); err != nil {
return fmt.Errorf("failed to get bot user by id=%v: %w", botUserID, err)
} else if botUser.Data.IsAccessGranted() == value {
log.Infof(c, "No need to change botUser.AccessGranted, as already is: %v", value)
} else if changed := botUser.Data.SetAccessGranted(value); changed {
if err = tx.Set(c, botUser.Record); err != nil {
err = fmt.Errorf("failed to save bot user record (key=%v): %w", botUser.Key, err)
return err
}
}
return nil
//return SetAccessGrantedForAllUserChats(whc, whc.BotUserKey, value) // TODO: Call in deferrer
}
//func SetAccessGrantedForAllUserChats(whcb *WebhookContextBase, botUserKey *datastore.Key, value bool) error {
// //ctx := whc.Context()
// //var telegramUserEntity TelegramUser
// //if err := whc.GetOrCreateTelegramUserEntity(&telegramUserEntity); err != nil {
// // if err == datastore.ErrNoSuchEntity {
// // telegramUserEntity.AccessGranted = !value // We'll update it down the road
// // } else {
// // return err
// // }
// //}
// //if telegramUserEntity.AccessGranted == value {
// // log.Infof(ctx, "No need to update TelegramUser entity as AccessGranted is already: %v", value)
// //} else {
// // if _, err := SaveTelegramUserEntity(ctx, whc.GetSender().GetID(), &telegramUserEntity); err != nil {
// // return err
// // }
// //}
// //var chats []TelegramChat
// //chatKeys, err := datastore.NewQuery(TelegramChatKind).Filter("TelegramUserID =", telegramUserID).Filter("AccessGranted =", !value).GetAll(ctx, &chats)
// //if err != nil {
// // return err
// //}
// //for i, botChat := range chats {
// // if botChat.AccessGranted != value {
// // chatKey, err := SaveTelegramChatEntity(ctx, whc.botSettings.code, chatKeys[i].IntID(), &botChat)
// // if err != nil {
// // log.Warningf(ctx, "Failed to save %v to db", chatKey)
// // }
// // }
// //}
// return nil
//}
//