2016-11-16 13:47:49 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2016-12-20 21:31:09 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2016-12-20 21:31:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewInvite generates a new invitation
|
|
|
|
func (d *Datastore) NewInvite(i *kolide.Invite) (*kolide.Invite, error) {
|
2017-01-18 15:40:51 +00:00
|
|
|
var (
|
|
|
|
deletedInvite kolide.Invite
|
|
|
|
sqlStmt string
|
|
|
|
)
|
|
|
|
err := d.db.Get(&deletedInvite, "SELECT * FROM invites WHERE email = ? AND deleted", i.Email)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
sqlStmt = `
|
2017-05-10 16:26:05 +00:00
|
|
|
REPLACE INTO invites ( invited_by, email, admin, name, position, token, deleted, sso_enabled)
|
|
|
|
VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)
|
2017-01-18 15:40:51 +00:00
|
|
|
`
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
sqlStmt = `
|
2017-05-10 16:26:05 +00:00
|
|
|
INSERT INTO invites ( invited_by, email, admin, name, position, token, deleted, sso_enabled)
|
|
|
|
VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)
|
2017-01-18 15:40:51 +00:00
|
|
|
`
|
|
|
|
default:
|
|
|
|
return nil, errors.Wrap(err, "check for existing invite")
|
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2017-01-18 15:40:51 +00:00
|
|
|
deleted := false
|
|
|
|
result, err := d.db.Exec(sqlStmt, i.InvitedBy, i.Email, i.Admin,
|
2017-05-10 16:26:05 +00:00
|
|
|
i.Name, i.Position, i.Token, deleted, i.SSOEnabled)
|
2017-01-18 15:40:51 +00:00
|
|
|
if err != nil && isDuplicate(err) {
|
|
|
|
return nil, alreadyExists("Invite", 0)
|
|
|
|
} else if err != nil {
|
2016-12-20 21:31:09 +00:00
|
|
|
return nil, errors.Wrap(err, "create invite")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id, _ := result.LastInsertId()
|
|
|
|
i.ID = uint(id)
|
|
|
|
|
|
|
|
return i, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListInvites lists all invites in the Kolide database. Supply query options
|
|
|
|
// using the opt parameter. See kolide.ListOptions
|
|
|
|
func (d *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, error) {
|
|
|
|
|
|
|
|
invites := []*kolide.Invite{}
|
|
|
|
|
2016-12-20 21:31:09 +00:00
|
|
|
query := appendListOptionsToSQL("SELECT * FROM invites WHERE NOT deleted", opt)
|
|
|
|
err := d.db.Select(&invites, query)
|
2016-12-30 01:58:12 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2016-12-20 21:31:09 +00:00
|
|
|
return nil, notFound("Invite")
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "select invite by ID")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
return invites, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invite returns Invite identified by id.
|
|
|
|
func (d *Datastore) Invite(id uint) (*kolide.Invite, error) {
|
2016-12-30 01:58:12 +00:00
|
|
|
var invite kolide.Invite
|
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE id = ? AND NOT deleted", id)
|
|
|
|
if err == sql.ErrNoRows {
|
2016-12-20 21:31:09 +00:00
|
|
|
return nil, notFound("Invite").WithID(id)
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "select invite by ID")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
2016-12-30 01:58:12 +00:00
|
|
|
return &invite, nil
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InviteByEmail finds an Invite with a particular email, if one exists.
|
|
|
|
func (d *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
|
2016-12-30 01:58:12 +00:00
|
|
|
var invite kolide.Invite
|
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE email = ? AND NOT deleted", email)
|
|
|
|
if err == sql.ErrNoRows {
|
2016-12-20 21:31:09 +00:00
|
|
|
return nil, notFound("Invite").
|
|
|
|
WithMessage(fmt.Sprintf("with email %s", email))
|
|
|
|
} else if err != nil {
|
2016-12-30 01:58:12 +00:00
|
|
|
return nil, errors.Wrap(err, "sqlx get invite by email")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
2016-12-30 01:58:12 +00:00
|
|
|
return &invite, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InviteByToken finds an Invite with a particular token, if one exists.
|
|
|
|
func (d *Datastore) InviteByToken(token string) (*kolide.Invite, error) {
|
|
|
|
var invite kolide.Invite
|
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE token = ? AND NOT deleted", token)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, notFound("Invite").
|
|
|
|
WithMessage(fmt.Sprintf("with token %s", token))
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "sqlx get invite by token")
|
|
|
|
}
|
|
|
|
return &invite, nil
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveInvite modifies existing Invite
|
|
|
|
func (d *Datastore) SaveInvite(i *kolide.Invite) error {
|
|
|
|
sql := `
|
|
|
|
UPDATE invites SET invited_by = ?, email = ?, admin = ?,
|
2017-05-10 16:26:05 +00:00
|
|
|
name = ?, position = ?, token = ?, sso_enabled = ?
|
2016-11-16 13:47:49 +00:00
|
|
|
WHERE id = ? AND NOT deleted
|
|
|
|
`
|
2017-03-30 22:03:48 +00:00
|
|
|
results, err := d.db.Exec(sql, i.InvitedBy, i.Email,
|
2017-05-10 16:26:05 +00:00
|
|
|
i.Admin, i.Name, i.Position, i.Token, i.SSOEnabled, i.ID,
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2016-12-20 21:31:09 +00:00
|
|
|
return errors.Wrap(err, "save invite")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
2017-03-30 22:03:48 +00:00
|
|
|
rowsAffected, err := results.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "rows affected updating invite")
|
|
|
|
}
|
|
|
|
if rowsAffected == 0 {
|
|
|
|
return notFound("Invite").WithID(i.ID)
|
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-04 18:18:21 +00:00
|
|
|
func (d *Datastore) DeleteInvite(id uint) error {
|
|
|
|
return d.deleteEntity("invites", id)
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|