2016-11-16 13:47:49 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2016-12-20 21:31:09 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-04-05 18:15:26 +00:00
|
|
|
"strings"
|
2016-12-20 21:31:09 +00:00
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-04-05 18:15:26 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2016-12-20 21:31:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-16 13:47:49 +00:00
|
|
|
)
|
|
|
|
|
2021-04-06 18:09:28 +00:00
|
|
|
var inviteSearchColumns = []string{"name", "email"}
|
|
|
|
|
2020-10-22 17:51:26 +00:00
|
|
|
// NewInvite generates a new invitation.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) NewInvite(i *fleet.Invite) (*fleet.Invite, error) {
|
2020-10-22 17:51:26 +00:00
|
|
|
sqlStmt := `
|
2021-04-07 01:27:10 +00:00
|
|
|
INSERT INTO invites ( invited_by, email, name, position, token, sso_enabled, global_role )
|
|
|
|
VALUES ( ?, ?, ?, ?, ?, ?, ?)
|
2020-10-22 17:51:26 +00:00
|
|
|
`
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
result, err := d.db.Exec(sqlStmt, i.InvitedBy, i.Email,
|
2021-04-05 18:15:26 +00:00
|
|
|
i.Name, i.Position, i.Token, i.SSOEnabled, i.GlobalRole)
|
2017-01-18 15:40:51 +00:00
|
|
|
if err != nil && isDuplicate(err) {
|
2021-04-05 20:28:43 +00:00
|
|
|
return nil, alreadyExists("Invite", i.Email)
|
2017-01-18 15:40:51 +00:00
|
|
|
} 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)
|
|
|
|
|
2021-04-05 18:15:26 +00:00
|
|
|
if len(i.Teams) == 0 {
|
2021-06-06 22:07:29 +00:00
|
|
|
i.Teams = []fleet.UserTeam{}
|
2021-04-05 18:15:26 +00:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bulk insert teams
|
|
|
|
const valueStr = "(?,?,?),"
|
|
|
|
var args []interface{}
|
|
|
|
for _, userTeam := range i.Teams {
|
|
|
|
args = append(args, i.ID, userTeam.Team.ID, userTeam.Role)
|
|
|
|
}
|
|
|
|
sql := "INSERT INTO invite_teams (invite_id, team_id, role) VALUES " +
|
|
|
|
strings.Repeat(valueStr, len(i.Teams))
|
|
|
|
sql = strings.TrimSuffix(sql, ",")
|
|
|
|
if _, err := d.db.Exec(sql, args...); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "insert teams")
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 17:39:32 +00:00
|
|
|
// ListInvites lists all invites in the Fleet database. Supply query options
|
2021-06-06 22:07:29 +00:00
|
|
|
// using the opt parameter. See fleet.ListOptions
|
|
|
|
func (d *Datastore) ListInvites(opt fleet.ListOptions) ([]*fleet.Invite, error) {
|
|
|
|
invites := []*fleet.Invite{}
|
2021-04-06 18:09:28 +00:00
|
|
|
query := "SELECT * FROM invites WHERE true"
|
|
|
|
query, params := searchLike(query, nil, opt.MatchQuery, inviteSearchColumns...)
|
|
|
|
query = appendListOptionsToSQL(query, opt)
|
|
|
|
|
|
|
|
err := d.db.Select(&invites, query, params...)
|
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
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
|
|
|
|
if err := d.loadTeamsForInvites(invites); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "load teams")
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
return invites, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invite returns Invite identified by id.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) Invite(id uint) (*fleet.Invite, error) {
|
|
|
|
var invite fleet.Invite
|
2020-10-22 17:51:26 +00:00
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE id = ?", id)
|
2016-12-30 01:58:12 +00:00
|
|
|
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
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := d.loadTeamsForInvites([]*fleet.Invite{&invite}); err != nil {
|
2021-04-05 18:15:26 +00:00
|
|
|
return nil, errors.Wrap(err, "load teams")
|
|
|
|
}
|
|
|
|
|
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.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) InviteByEmail(email string) (*fleet.Invite, error) {
|
|
|
|
var invite fleet.Invite
|
2020-10-22 17:51:26 +00:00
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE email = ?", email)
|
2016-12-30 01:58:12 +00:00
|
|
|
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
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := d.loadTeamsForInvites([]*fleet.Invite{&invite}); err != nil {
|
2021-04-05 18:15:26 +00:00
|
|
|
return nil, errors.Wrap(err, "load teams")
|
|
|
|
}
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
return &invite, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InviteByToken finds an Invite with a particular token, if one exists.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) InviteByToken(token string) (*fleet.Invite, error) {
|
|
|
|
var invite fleet.Invite
|
2020-10-22 17:51:26 +00:00
|
|
|
err := d.db.Get(&invite, "SELECT * FROM invites WHERE token = ?", token)
|
2016-12-30 01:58:12 +00:00
|
|
|
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")
|
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
if err := d.loadTeamsForInvites([]*fleet.Invite{&invite}); err != nil {
|
2021-04-05 18:15:26 +00:00
|
|
|
return nil, errors.Wrap(err, "load teams")
|
|
|
|
}
|
|
|
|
|
2016-12-30 01:58:12 +00:00
|
|
|
return &invite, nil
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 18:15:26 +00:00
|
|
|
func (d *Datastore) DeleteInvite(id uint) error {
|
|
|
|
return d.deleteEntity("invites", id)
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (d *Datastore) loadTeamsForInvites(invites []*fleet.Invite) error {
|
2021-04-05 18:15:26 +00:00
|
|
|
inviteIDs := make([]uint, 0, len(invites)+1)
|
|
|
|
// Make sure the slice is never empty for IN by filling a nonexistent ID
|
|
|
|
inviteIDs = append(inviteIDs, 0)
|
2021-06-06 22:07:29 +00:00
|
|
|
idToInvite := make(map[uint]*fleet.Invite, len(invites))
|
2021-04-05 18:15:26 +00:00
|
|
|
for _, u := range invites {
|
|
|
|
// Initialize empty slice so we get an array in JSON responses instead
|
|
|
|
// of null if it is empty
|
2021-06-06 22:07:29 +00:00
|
|
|
u.Teams = []fleet.UserTeam{}
|
2021-04-05 18:15:26 +00:00
|
|
|
// Track IDs for queries and matching
|
|
|
|
inviteIDs = append(inviteIDs, u.ID)
|
|
|
|
idToInvite[u.ID] = u
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
sql := `
|
2021-04-05 18:15:26 +00:00
|
|
|
SELECT ut.team_id AS id, ut.invite_id, ut.role, t.name
|
|
|
|
FROM invite_teams ut INNER JOIN teams t ON ut.team_id = t.id
|
|
|
|
WHERE ut.invite_id IN (?)
|
|
|
|
ORDER BY invite_id, team_id
|
2016-11-16 13:47:49 +00:00
|
|
|
`
|
2021-04-05 18:15:26 +00:00
|
|
|
sql, args, err := sqlx.In(sql, inviteIDs)
|
2016-11-16 13:47:49 +00:00
|
|
|
if err != nil {
|
2021-04-05 18:15:26 +00:00
|
|
|
return errors.Wrap(err, "sqlx.In loadTeamsForInvites")
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
|
|
|
|
var rows []struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.UserTeam
|
2021-04-05 18:15:26 +00:00
|
|
|
InviteID uint `db:"invite_id"`
|
2017-03-30 22:03:48 +00:00
|
|
|
}
|
2021-04-05 18:15:26 +00:00
|
|
|
if err := d.db.Select(&rows, sql, args...); err != nil {
|
|
|
|
return errors.Wrap(err, "get loadTeamsForInvites")
|
2017-03-30 22:03:48 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2021-04-05 18:15:26 +00:00
|
|
|
// Map each row to the appropriate invite
|
|
|
|
for _, r := range rows {
|
|
|
|
invite := idToInvite[r.InviteID]
|
|
|
|
invite.Teams = append(invite.Teams, r.UserTeam)
|
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2021-04-05 18:15:26 +00:00
|
|
|
return nil
|
2016-11-16 13:47:49 +00:00
|
|
|
}
|