2016-09-28 11:35:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-28 11:35:15 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
2016-09-28 11:35:15 +00:00
|
|
|
)
|
|
|
|
|
2017-01-17 16:43:59 +00:00
|
|
|
func (mw metricsMiddleware) ChangeUserAdmin(ctx context.Context, id uint, isAdmin bool) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "ChangeUserAdmin", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.ChangeUserAdmin(ctx, id, isAdmin)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2020-11-05 01:06:55 +00:00
|
|
|
func (mw metricsMiddleware) CreateUserWithInvite(ctx context.Context, p kolide.UserPayload) (*kolide.User, error) {
|
2016-09-28 11:35:15 +00:00
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
2020-11-05 01:06:55 +00:00
|
|
|
lvs := []string{"method", "CreateUserWithInvite", "error", fmt.Sprint(err != nil)}
|
2016-09-28 11:35:15 +00:00
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2020-11-05 01:06:55 +00:00
|
|
|
user, err = mw.Service.CreateUserWithInvite(ctx, p)
|
2016-09-28 11:35:15 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw metricsMiddleware) ModifyUser(ctx context.Context, userID uint, p kolide.UserPayload) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "ModifyUser", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.ModifyUser(ctx, userID, p)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw metricsMiddleware) User(ctx context.Context, id uint) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "User", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.User(ctx, id)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2021-04-22 03:54:09 +00:00
|
|
|
func (mw metricsMiddleware) ListUsers(ctx context.Context, opt kolide.UserListOptions) ([]*kolide.User, error) {
|
2016-09-28 11:35:15 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
users []*kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "Users", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
users, err = mw.Service.ListUsers(ctx, opt)
|
2016-09-28 11:35:15 +00:00
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw metricsMiddleware) AuthenticatedUser(ctx context.Context) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "AuthenticatedUser", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.AuthenticatedUser(ctx)
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:11:43 +00:00
|
|
|
func (mw metricsMiddleware) ChangePassword(ctx context.Context, oldPass, newPass string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "ChangePassword", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
err = mw.Service.ChangePassword(ctx, oldPass, newPass)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-28 11:35:15 +00:00
|
|
|
func (mw metricsMiddleware) ResetPassword(ctx context.Context, token, password string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "ResetPassword", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
err = mw.Service.ResetPassword(ctx, token, password)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw metricsMiddleware) RequestPasswordReset(ctx context.Context, email string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
lvs := []string{"method", "RequestPasswordReset", "error", fmt.Sprint(err != nil)}
|
|
|
|
mw.requestCount.With(lvs...).Add(1)
|
|
|
|
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds())
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
err = mw.Service.RequestPasswordReset(ctx, email)
|
|
|
|
return err
|
|
|
|
}
|