2016-09-29 02:44:05 +00:00
|
|
|
package kolide
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"html/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InviteStore contains the methods for
|
|
|
|
// managing user invites in a datastore.
|
|
|
|
type InviteStore interface {
|
|
|
|
// NewInvite creates and stores a new invitation in a DB.
|
|
|
|
NewInvite(i *Invite) (*Invite, error)
|
|
|
|
|
|
|
|
// Invites lists all invites in the datastore.
|
|
|
|
Invites() ([]*Invite, error)
|
|
|
|
|
|
|
|
// Invite retrieves an invite by it's ID.
|
|
|
|
Invite(id uint) (*Invite, error)
|
|
|
|
|
|
|
|
// InviteByEmail retrieves an invite for a specific email address.
|
|
|
|
InviteByEmail(email string) (*Invite, error)
|
|
|
|
|
|
|
|
// SaveInvite saves an invitation in the datastore.
|
|
|
|
SaveInvite(i *Invite) error
|
|
|
|
|
|
|
|
// DeleteInvite deletes an invitation.
|
|
|
|
DeleteInvite(i *Invite) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// InviteService contains methods for a service which deals with
|
|
|
|
// user invites.
|
|
|
|
type InviteService interface {
|
|
|
|
// InviteNewUser creates an invite for a new user to join Kolide.
|
|
|
|
InviteNewUser(ctx context.Context, payload InvitePayload) (invite *Invite, err error)
|
|
|
|
|
|
|
|
// DeleteInvite removes an invite.
|
|
|
|
DeleteInvite(ctx context.Context, id uint) (err error)
|
|
|
|
|
|
|
|
// Invites returns a list of all invites.
|
|
|
|
Invites(ctx context.Context) (invites []*Invite, err error)
|
|
|
|
|
|
|
|
// VerifyInvite verifies that an invite exists and that it matches the
|
|
|
|
// invite token.
|
|
|
|
VerifyInvite(ctx context.Context, email, token string) (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvitePayload contains fields required to create a new user invite.
|
|
|
|
type InvitePayload struct {
|
|
|
|
InvitedBy *uint `json:"invited_by"`
|
|
|
|
Email *string
|
|
|
|
Admin *bool
|
|
|
|
Name *string
|
|
|
|
Position *string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invite represents an invitation for a user to join Kolide.
|
|
|
|
type Invite struct {
|
2016-10-11 16:22:11 +00:00
|
|
|
ID uint `json:"id" gorm:"primary_key"`
|
|
|
|
CreatedAt time.Time `json:"-"`
|
|
|
|
InvitedBy uint `json:"invited_by" gorm:"not null"`
|
|
|
|
Email string `json:"email" gorm:"not null;unique_index:idx_invite_unique_email"`
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Position string `json:"position,omitempty"`
|
|
|
|
Token string `json:"-" gorm:"not null;unique_index:idx_invite_unique_key"`
|
2016-09-29 02:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fixme
|
|
|
|
// this is not the right way to generate emails at all
|
|
|
|
const inviteEmailTempate = `
|
2016-10-11 16:22:11 +00:00
|
|
|
{{.InvitedBy}} invited you to join Kolide.,
|
2016-09-29 02:44:05 +00:00
|
|
|
http://localhost:8080/signup?token={{.Token}}
|
|
|
|
`
|
|
|
|
|
|
|
|
func (i Invite) Message() ([]byte, error) {
|
|
|
|
var msg bytes.Buffer
|
|
|
|
var err error
|
|
|
|
t := template.New(inviteEmailTempate)
|
|
|
|
if t, err = t.Parse(inviteEmailTempate); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = t.Execute(&msg, i); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return msg.Bytes(), nil
|
|
|
|
}
|