2016-09-29 02:44:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2016-12-07 15:42:58 +00:00
|
|
|
"encoding/base64"
|
2016-12-28 16:55:03 +00:00
|
|
|
"html/template"
|
2016-09-29 02:44:05 +00:00
|
|
|
|
2017-02-01 17:20:50 +00:00
|
|
|
"github.com/kolide/kolide/server/kolide"
|
2016-09-29 02:44:05 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (svc service) InviteNewUser(ctx context.Context, payload kolide.InvitePayload) (*kolide.Invite, error) {
|
|
|
|
// verify that the user with the given email does not already exist
|
|
|
|
_, err := svc.ds.UserByEmail(*payload.Email)
|
|
|
|
if err == nil {
|
|
|
|
return nil, newInvalidArgumentError("email", "a user with this account already exists")
|
|
|
|
}
|
2016-12-20 18:35:22 +00:00
|
|
|
|
|
|
|
if _, ok := err.(kolide.NotFoundError); !ok {
|
2016-09-29 02:44:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the user who created the invite
|
|
|
|
inviter, err := svc.User(ctx, *payload.InvitedBy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-07 15:42:58 +00:00
|
|
|
random, err := kolide.RandomText(svc.config.App.TokenKeySize)
|
2016-09-29 02:44:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-07 15:42:58 +00:00
|
|
|
token := base64.URLEncoding.EncodeToString([]byte(random))
|
2016-09-29 02:44:05 +00:00
|
|
|
|
|
|
|
invite := &kolide.Invite{
|
|
|
|
Email: *payload.Email,
|
|
|
|
Admin: *payload.Admin,
|
|
|
|
InvitedBy: inviter.ID,
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
if payload.Position != nil {
|
|
|
|
invite.Position = *payload.Position
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
invite.Name = *payload.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
invite, err = svc.ds.NewInvite(invite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:54:30 +00:00
|
|
|
config, err := svc.AppConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:37:00 +00:00
|
|
|
invitedBy := inviter.Name
|
|
|
|
if invitedBy == "" {
|
|
|
|
invitedBy = inviter.Username
|
|
|
|
}
|
2016-09-29 02:44:05 +00:00
|
|
|
inviteEmail := kolide.Email{
|
2016-12-20 21:54:30 +00:00
|
|
|
Subject: "You're Invited to Kolide",
|
|
|
|
To: []string{invite.Email},
|
|
|
|
Config: config,
|
2016-12-28 16:55:03 +00:00
|
|
|
Mailer: &kolide.InviteMailer{
|
|
|
|
Invite: invite,
|
|
|
|
KolideServerURL: template.URL(config.KolideServerURL),
|
2017-01-17 21:37:00 +00:00
|
|
|
OrgName: config.OrgName,
|
|
|
|
InvitedByUsername: invitedBy,
|
2016-12-28 16:55:03 +00:00
|
|
|
},
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
2016-12-20 21:54:30 +00:00
|
|
|
|
2016-09-29 02:44:05 +00:00
|
|
|
err = svc.mailService.SendEmail(inviteEmail)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return invite, nil
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
func (svc service) ListInvites(ctx context.Context, opt kolide.ListOptions) ([]*kolide.Invite, error) {
|
2016-10-14 15:59:27 +00:00
|
|
|
return svc.ds.ListInvites(opt)
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
func (svc service) VerifyInvite(ctx context.Context, token string) (*kolide.Invite, error) {
|
|
|
|
invite, err := svc.ds.InviteByToken(token)
|
2016-09-29 02:44:05 +00:00
|
|
|
if err != nil {
|
2016-12-30 01:58:12 +00:00
|
|
|
return nil, err
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if invite.Token != token {
|
2016-12-30 01:58:12 +00:00
|
|
|
return nil, newInvalidArgumentError("invite_token", "Invite Token does not match Email Address.")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expiresAt := invite.CreatedAt.Add(svc.config.App.InviteTokenValidityPeriod)
|
|
|
|
if svc.clock.Now().After(expiresAt) {
|
2016-12-30 01:58:12 +00:00
|
|
|
return nil, newInvalidArgumentError("invite_token", "Invite token has expired.")
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
return invite, nil
|
2016-09-29 02:44:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc service) DeleteInvite(ctx context.Context, id uint) error {
|
2017-01-04 18:18:21 +00:00
|
|
|
return svc.ds.DeleteInvite(id)
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|