fleet/server/service/endpoint_users.go

175 lines
5.0 KiB
Go
Raw Normal View History

2016-09-26 18:48:55 +00:00
package service
2016-08-28 03:59:17 +00:00
import (
"net/http"
2016-08-28 03:59:17 +00:00
"github.com/go-kit/kit/endpoint"
2016-09-26 18:48:55 +00:00
"github.com/kolide/kolide-ose/server/kolide"
"golang.org/x/net/context"
2016-08-28 03:59:17 +00:00
)
////////////////////////////////////////////////////////////////////////////////
// Create User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type createUserRequest struct {
payload kolide.UserPayload
}
type createUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r createUserResponse) error() error { return r.Err }
func makeCreateUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createUserRequest)
user, err := svc.NewUser(ctx, req.payload)
2016-08-29 17:04:07 +00:00
if err != nil {
return createUserResponse{Err: err}, nil
}
return createUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// Get User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type getUserRequest struct {
ID uint `json:"id"`
}
type getUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r getUserResponse) error() error { return r.Err }
func makeGetUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(getUserRequest)
user, err := svc.User(ctx, req.ID)
if err != nil {
return getUserResponse{Err: err}, nil
}
return getUserResponse{User: user}, nil
}
}
func makeGetSessionUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
user, err := svc.AuthenticatedUser(ctx)
if err != nil {
return getUserResponse{Err: err}, nil
}
return getUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// List Users
////////////////////////////////////////////////////////////////////////////////
type listUsersResponse struct {
Users []kolide.User `json:"users"`
Err error `json:"error,omitempty"`
}
func (r listUsersResponse) error() error { return r.Err }
func makeListUsersEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
users, err := svc.Users(ctx)
if err != nil {
return listUsersResponse{Err: err}, nil
}
resp := listUsersResponse{Users: []kolide.User{}}
for _, user := range users {
resp.Users = append(resp.Users, *user)
}
return resp, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Reset Password
////////////////////////////////////////////////////////////////////////////////
type resetPasswordRequest struct {
PasswordResetToken string `json:"password_reset_token"`
NewPassword string `json:"new_password"`
}
type resetPasswordResponse struct {
Err error `json:"error,omitempty"`
}
func (r resetPasswordResponse) error() error { return r.Err }
func makeResetPasswordEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(resetPasswordRequest)
err := svc.ResetPassword(ctx, req.PasswordResetToken, req.NewPassword)
return resetPasswordResponse{Err: err}, nil
}
}
////////////////////////////////////////////////////////////////////////////////
// Modify User
////////////////////////////////////////////////////////////////////////////////
2016-08-28 03:59:17 +00:00
type modifyUserRequest struct {
ID uint
payload kolide.UserPayload
}
type modifyUserResponse struct {
User *kolide.User `json:"user,omitempty"`
Err error `json:"error,omitempty"`
2016-08-28 03:59:17 +00:00
}
func (r modifyUserResponse) error() error { return r.Err }
func makeModifyUserEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(modifyUserRequest)
user, err := svc.ModifyUser(ctx, req.ID, req.payload)
if err != nil {
return modifyUserResponse{Err: err}, nil
}
2016-08-28 03:59:17 +00:00
return modifyUserResponse{User: user}, nil
2016-08-28 03:59:17 +00:00
}
}
////////////////////////////////////////////////////////////////////////////////
// Forgot Password
////////////////////////////////////////////////////////////////////////////////
type forgotPasswordRequest struct {
Email string `json:"email"`
}
type forgotPasswordResponse struct {
Err error `json:"error,omitempty"`
}
func (r forgotPasswordResponse) error() error { return r.Err }
func (r forgotPasswordResponse) status() int { return http.StatusAccepted }
func makeForgotPasswordEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(forgotPasswordRequest)
err := svc.RequestPasswordReset(ctx, req.Email)
if err != nil {
return forgotPasswordResponse{Err: err}, nil
}
return forgotPasswordResponse{}, nil
}
}