-
Notifications
You must be signed in to change notification settings - Fork 302
/
invitation_registry.go
128 lines (116 loc) · 4.09 KB
/
invitation_registry.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 identityserver
import (
"context"
"time"
"github.com/gogo/protobuf/types"
"github.com/jinzhu/gorm"
"go.thethings.network/lorawan-stack/v3/pkg/auth"
"go.thethings.network/lorawan-stack/v3/pkg/email"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/events"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/emails"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
var evtCreateInvitation = events.Define("invitation.create", "create invitation")
var errNoInviteRights = errors.DefinePermissionDenied(
"no_invite_rights",
"no rights for inviting users",
)
func (is *IdentityServer) sendInvitation(ctx context.Context, in *ttnpb.SendInvitationRequest) (*ttnpb.Invitation, error) {
authInfo, err := is.authInfo(ctx)
if err != nil {
return nil, err
}
if !authInfo.GetUniversalRights().IncludesAll(ttnpb.RIGHT_SEND_INVITES) {
return nil, errNoInviteRights.New()
}
token, err := auth.GenerateKey(ctx)
if err != nil {
return nil, err
}
invitation := &ttnpb.Invitation{
Email: in.Email,
Token: token,
ExpiresAt: time.Now().Add(is.configFromContext(ctx).UserRegistration.Invitation.TokenTTL),
}
err = is.withDatabase(ctx, func(db *gorm.DB) (err error) {
invitation, err = store.GetInvitationStore(db).CreateInvitation(ctx, invitation)
return err
})
if err != nil {
return nil, err
}
events.Publish(evtCreateInvitation.NewWithIdentifiersAndData(ctx, nil, invitation))
err = is.SendEmail(ctx, func(data emails.Data) email.MessageData {
data.User.Email = in.Email
return &emails.Invitation{
Data: data,
InvitationToken: invitation.Token,
}
})
if err != nil {
log.FromContext(ctx).WithError(err).Error("Could not send invitation email")
}
return invitation, nil
}
func (is *IdentityServer) listInvitations(ctx context.Context, req *ttnpb.ListInvitationsRequest) (invitations *ttnpb.Invitations, err error) {
authInfo, err := is.authInfo(ctx)
if err != nil {
return nil, err
}
if !authInfo.GetUniversalRights().IncludesAll(ttnpb.RIGHT_SEND_INVITES) {
return nil, errNoInviteRights.New()
}
invitations = &ttnpb.Invitations{}
err = is.withDatabase(ctx, func(db *gorm.DB) (err error) {
invitations.Invitations, err = store.GetInvitationStore(db).FindInvitations(ctx)
return err
})
if err != nil {
return nil, err
}
return invitations, nil
}
func (is *IdentityServer) deleteInvitation(ctx context.Context, in *ttnpb.DeleteInvitationRequest) (*types.Empty, error) {
authInfo, err := is.authInfo(ctx)
if err != nil {
return nil, err
}
if !authInfo.GetUniversalRights().IncludesAll(ttnpb.RIGHT_SEND_INVITES) {
return nil, errNoInviteRights.New()
}
err = is.withDatabase(ctx, func(db *gorm.DB) error {
return store.GetInvitationStore(db).DeleteInvitation(ctx, in.Email)
})
if err != nil {
return nil, err
}
return ttnpb.Empty, nil
}
type invitationRegistry struct {
*IdentityServer
}
func (ir *invitationRegistry) Send(ctx context.Context, req *ttnpb.SendInvitationRequest) (*ttnpb.Invitation, error) {
return ir.sendInvitation(ctx, req)
}
func (ir *invitationRegistry) List(ctx context.Context, req *ttnpb.ListInvitationsRequest) (*ttnpb.Invitations, error) {
return ir.listInvitations(ctx, req)
}
func (ir *invitationRegistry) Delete(ctx context.Context, req *ttnpb.DeleteInvitationRequest) (*types.Empty, error) {
return ir.deleteInvitation(ctx, req)
}