forked from bots-go-framework/bots-fw
/
context_auth.go
94 lines (90 loc) · 3.28 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
93
94
package bots
import (
"context"
"github.com/pkg/errors"
"github.com/strongo/log"
)
// SetAccessGranted marks current context as authenticated
func SetAccessGranted(whc WebhookContext, value bool) (err error) {
c := whc.Context()
log.Debugf(c, "SetAccessGranted(value=%v)", value)
chatEntity := whc.ChatEntity()
if chatEntity != nil {
if chatEntity.IsAccessGranted() == value {
log.Infof(c, "No need to change chatEntity.AccessGranted, as already is: %v", value)
} else {
if err = whc.RunInTransaction(c, func(c context.Context) (err error) {
var chatID string
if chatID, err = whc.BotChatID(); err != nil {
return
}
if chatEntity, err = whc.GetBotChatEntityByID(c, whc.GetBotCode(), chatID); err != nil {
return
}
if changed := chatEntity.SetAccessGranted(value); changed {
if err = whc.SaveBotChat(c, whc.GetBotCode(), chatID, chatEntity); err != nil {
err = errors.Wrap(err, "Failed to save bot chat entity to db")
}
}
return
}, nil); err != nil {
return
}
}
}
botUserID := whc.GetSender().GetID()
log.Debugf(c, "SetAccessGranted(): whc.GetSender().GetID() = %v", botUserID)
if botUser, err := whc.GetBotUserByID(c, botUserID); err != nil {
return errors.Wrapf(err, "Failed to get bot user by id=%v", botUserID)
} else if botUser.IsAccessGranted() == value {
log.Infof(c, "No need to change botUser.AccessGranted, as already is: %v", value)
} else {
err = whc.RunInTransaction(c, func(c context.Context) error {
botUser.SetAccessGranted(value)
if botUser, err = whc.GetBotUserByID(c, botUserID); err != nil {
return errors.Wrapf(err, "Failed to get transactionally bot user by id=%v", botUserID)
}
if changed := botUser.SetAccessGranted(value); changed {
if err = whc.SaveBotUser(c, botUserID, botUser); err != nil {
err = errors.Wrapf(err, "Failed to call whc.SaveBotUser(botUserID=%v)", botUserID)
}
}
return err
}, nil)
}
return err
//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, chat := range chats {
// // if chat.AccessGranted != value {
// // chatKey, err := SaveTelegramChatEntity(ctx, whc.botSettings.code, chatKeys[i].IntID(), &chat)
// // if err != nil {
// // log.Warningf(ctx, "Failed to save %v to db", chatKey)
// // }
// // }
// //}
// return nil
//}
//