2016-09-05 20:03:58 +00:00
|
|
|
package server
|
2016-09-01 04:51:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kolide/kolide-ose/kolide"
|
2016-09-03 17:25:16 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-09-01 04:51:38 +00:00
|
|
|
)
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
func (mw loggingMiddleware) NewUser(ctx context.Context, p kolide.UserPayload) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
username = "none"
|
|
|
|
)
|
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
vc, err := viewerContextFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
|
|
|
"method", "NewUser",
|
|
|
|
"user", username,
|
|
|
|
"created_by", vc.user.Username,
|
|
|
|
"err", err,
|
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.NewUser(ctx, p)
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw loggingMiddleware) ModifyUser(ctx context.Context, userID uint, p kolide.UserPayload) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
username = "none"
|
|
|
|
)
|
|
|
|
|
|
|
|
vc, err := viewerContextFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
|
|
|
"method", "ModifyUser",
|
|
|
|
"user", username,
|
|
|
|
"modified_by", vc.user.Username,
|
|
|
|
"err", err,
|
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.ModifyUser(ctx, userID, p)
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, err
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
func (mw loggingMiddleware) User(ctx context.Context, id uint) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
username = "none"
|
|
|
|
)
|
2016-09-01 04:51:38 +00:00
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
|
|
|
"method", "User",
|
|
|
|
"user", username,
|
|
|
|
"err", err,
|
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.User(ctx, id)
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
return user, err
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 04:35:52 +00:00
|
|
|
func (mw loggingMiddleware) AuthenticatedUser(ctx context.Context) (*kolide.User, error) {
|
|
|
|
var (
|
|
|
|
user *kolide.User
|
|
|
|
err error
|
|
|
|
username = "none"
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
|
|
|
"method", "User",
|
|
|
|
"user", username,
|
|
|
|
"err", err,
|
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
user, err = mw.Service.AuthenticatedUser(ctx)
|
|
|
|
|
|
|
|
if user != nil {
|
|
|
|
username = user.Username
|
|
|
|
}
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
func (mw loggingMiddleware) ResetPassword(ctx context.Context, token, password string) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
|
|
|
"method", "ChangePassword",
|
|
|
|
"err", err,
|
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
|
|
|
err = mw.Service.ResetPassword(ctx, token, password)
|
|
|
|
return err
|
|
|
|
}
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
func (mw loggingMiddleware) RequestPasswordReset(ctx context.Context, email string) error {
|
|
|
|
var (
|
|
|
|
requestedBy = "unauthenticated"
|
|
|
|
err error
|
|
|
|
)
|
2016-09-01 04:51:38 +00:00
|
|
|
vc, err := viewerContextFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
if vc.IsLoggedIn() {
|
|
|
|
requestedBy = vc.user.Username
|
|
|
|
}
|
2016-09-01 04:51:38 +00:00
|
|
|
|
|
|
|
defer func(begin time.Time) {
|
|
|
|
_ = mw.logger.Log(
|
2016-09-15 14:52:17 +00:00
|
|
|
"method", "RequestPasswordReset",
|
|
|
|
"email", email,
|
2016-09-01 04:51:38 +00:00
|
|
|
"err", err,
|
2016-09-15 14:52:17 +00:00
|
|
|
"requested_by", requestedBy,
|
2016-09-01 04:51:38 +00:00
|
|
|
"took", time.Since(begin),
|
|
|
|
)
|
|
|
|
}(time.Now())
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
err = mw.Service.RequestPasswordReset(ctx, email)
|
|
|
|
return err
|
2016-09-01 04:51:38 +00:00
|
|
|
}
|